How to retrieve all keys (or values) from a std::map and put them into a vector?

This is one of the possible ways I come out: struct RetrieveKey { template <typename T> typename T::first_type operator()(T keyValuePair) const { return keyValuePair.first; } }; map<int, int> m; vector<int> keys; // Retrieve all keys transform(m.begin(), m.end(), back_inserter(keys), RetrieveKey()); // Dump all keys copy(keys.begin(), keys.end(), ostream_iterator<int>(cout, “\n”)); Of course, we can also retrieve all values … Read more

Initializing a static std::map in C++

What is the right way of initializing a static map? Do we need a static function that will initialize it? 12 s 12 Using C++11: #include <map> using namespace std; map<int, char> m = {{1, ‘a’}, {3, ‘b’}, {5, ‘c’}, {7, ‘d’}}; Using Boost.Assign: #include <map> #include “boost/assign.hpp” using namespace std; using namespace boost::assign; map<int, … Read more