New answers tagged

1

It might be overkill for just the task at hand, but I've found it useful to write an overload of operator>> to read and match a string literal: std::istream &operator>>(std::istream &is, char const *s) { while (*s && *s == is.peek()) { ++s; is.ignore(1); } if (*s != '\0') is.setstate(std::...


1

You don't check whether the input is invalid. Try to extract useful well-named functions, like int days(int month, int year) and bool is_leap_year(int year). You can extract the numbers directly from std::cin, the colon will be left behind. And the colon can thereafter be extracted like any other single character. As an aside, using scanf() would probably be ...


1

A few things might be better, but you will need to measure if they actually help. (untested code) The most costly thing in your program (except the I/O) is properly allocation for the strings. So to avoid continuous reallocation you could try result.reserve(word.size()); and constexpr int LargeBuffer { 4096 }; std::string word; word.reserve(LargeBuffer)...


5

First impressions: nicely presented code; good use of the appropriate standard library functions and classes. A minor suggestion would be to change the name, given that reduce is a well-known concept in functional programming (and is a function in <numeric>). Perhaps call it compress? I'd suggest extracting the constant 3 to give it a meaningful ...


2

Design I design my class exactly the same. The main difference I see is that you have a single friend void swap() standalone function where I break this into two separate functions. I have a member swap that does all the work and then a standalone swap that simply calls the member function. class X { public: void swap(X& rhs) noexcept ...


5

SimpleList should be named ForwardList or similar to signal that it is only singly-linked. Why is SimpleList::Node public? It's a bona-fide implementation-detail AFAICS. I prefer always putting the links first, as it marginally helps with uniformity, especially when getting to implement the iterator-interface. Putting the dtor in the middle of the ctors is ...


Top 50 recent answers are included