Is there a standard date/time class in C++?

Not part of STL but well known library is boost. I would go the way of using boost::date. Here are some examples: http://www.boost.org/doc/libs/1_55_0/doc/html/date_time/date_time_io.html#date_time.io_tutorial. If you did not try out boost yet I encourage you to do so as it saves you from a lot of nasty issues, as it masks most OS dependent things like … Read more

Can I have polymorphic containers with value semantics in C++?

Since the objects of different classes will have different sizes, you would end up running into the slicing problem if you store them as values. One reasonable solution is to store container safe smart pointers. I normally use boost::shared_ptr which is safe to store in a container. Note that std::auto_ptr is not. vector<shared_ptr<Parent>> vec; vec.push_back(shared_ptr<Parent>(new … Read more

How to check that the passed Iterator is a random access iterator?

If Iterator is a random access iterator, then std::iterator_traits<Iterator>::iterator_category will be std::random_access_iterator_tag. The cleanest way to implement this is probably to create a second function template and have Foo call it: template <typename Iterator> void FooImpl(Iterator first, Iterator last, std::random_access_iterator_tag) { // … } template <typename Iterator> void Foo(Iterator first, Iterator last) { typedef typename … Read more

C++, can I statically initialize a std::map at compile time?

It’s not exactly static initialization, but still, give it a try. If your compiler doesn’t support C++0x, I’d go for std::map’s iteration constructor: std::pair<int, std::string> map_data[] = { std::make_pair(1, “a”), std::make_pair(2, “b”), std::make_pair(3, “c”) }; std::map<int, std::string> my_map(map_data, map_data + sizeof map_data / sizeof map_data[0]); This is pretty readable, doesn’t require any extra libraries and … Read more

Vector Iterators Incompatible

The reason you are getting this, is that the iterators are from two (or more) different copies of myVec. You are returning a copy of the vector with each call to myFoo.getVec(). So the iterators are incompatible. Some solutions: Return a const reference to the std::vector<int> : const std::vector<int> & getVec(){return myVec;} //other stuff omitted … Read more

std::mutex performance compared to win32 CRITICAL_SECTION

Please see my updates at the end of the answer, the situation has dramatically changed since Visual Studio 2015. The original answer is below. I made a very simple test and according to my measurements the std::mutex is around 50-70x slower than CRITICAL_SECTION. std::mutex: 18140574us CRITICAL_SECTION: 296874us Edit: After some more tests it turned out … Read more

How can I make the map::find operation case insensitive?

It does not by default. You will have to provide a custom comparator as a third argument. Following snippet will help you… /************************************************************************/ /* Comparator for case-insensitive comparison in STL assos. containers */ /************************************************************************/ struct ci_less : std::binary_function<std::string, std::string, bool> { // case-independent (ci) compare_less binary function struct nocase_compare : public std::binary_function<unsigned char,unsigned char,bool> { … Read more

Pointers as keys in map C++ STL

The default implementation will compare the addresses stored by the pointers, so different objects will be considered as different keys. However, the logical state of the object will not be considered. For example, if you use std::string * as the key, two different std::string objects with the same text of “Hello” would be considered a … Read more