List iterator not dereferencable?

Could you have a race-condition? If the list were empty, then I’d expect a problem when trying to dereference begin(), but you check for empty. Do you have another thread adding or removing items from list in parallel? Your code snippets works for me on VS 2008 (assuming I typedef Counted_message_reader to int).

Displaying contents of a vector container in C++

There is an idiomatic way for printing a vector out. This way is safe and doesn’t require you to keep track of the vectors size or anything like that. It is also easily recognisable to other C++ developers. This method works on other container types too that do not allow random access. This works both … Read more

C++ equivalent of StringBuffer/StringBuilder?

The C++ way would be to use std::stringstream or just plain string concatenations. C++ strings are mutable so the performance considerations of concatenation are less of a concern. with regards to formatting, you can do all the same formatting on a stream, but in a different way, similar to cout. or you can use a strongly typed functor which … Read more

std::string length() and size() member functions

As per the documentation, these are just synonyms. size() is there to be consistent with other STL containers (like vector, map, etc.) and length() is to be consistent with most peoples’ intuitive notion of character strings. People usually talk about a word, sentence or paragraph’s length, not its size, so length() is there to make … Read more

Use the auto keyword in C++ STL

The auto keyword is simply asking the compiler to deduce the type of the variable from the initialization. Even a pre-C++0x compiler knows what the type of an (initialization) expression is, and more often than not, you can see that type in error messages. The auto keyword simply allows you to take advantage of this … Read more

How to get current time in milliseconds?

Simply use std::chrono. The general example below times the task “of printing 1000 stars”: Instead of printing the stars, you will place your sorting algorithm there and time measure it. Do not forget to enable the optimization flags for your compiler, if you intend to do some benchmarking, e.g. for g++, you need -O3. This is serious, check … Read more