snippetcppCritical
How to retrieve all keys (or values) from a std::map and put them into a vector?
Viewed 0 times
howfromkeysandstdretrieveputvectorthemvalues
Problem
This is one of the possible ways I come out:
Of course, we can also retrieve all values from the map by defining another functor RetrieveValues.
Is there any other way to achieve this easily? (I'm always wondering why
struct RetrieveKey
{
template
typename T::first_type operator()(T keyValuePair) const
{
return keyValuePair.first;
}
};
map m;
vector keys;
// Retrieve all keys
transform(m.begin(), m.end(), back_inserter(keys), RetrieveKey());
// Dump all keys
copy(keys.begin(), keys.end(), ostream_iterator(cout, "\n"));Of course, we can also retrieve all values from the map by defining another functor RetrieveValues.
Is there any other way to achieve this easily? (I'm always wondering why
std::map does not include a member function for us to do so.)Solution
While your solution should work, it can be difficult to read depending on the skill level of your fellow programmers. Additionally, it moves functionality away from the call site. Which can make maintenance a little more difficult.
I'm not sure if your goal is to get the keys into a vector or print them to cout so I'm doing both. You may try something like this:
Or even simpler, if you are using the Boost library:
Personally, I like the BOOST_FOREACH version because there is less typing and it is very explicit about what it is doing.
I'm not sure if your goal is to get the keys into a vector or print them to cout so I'm doing both. You may try something like this:
std::map m;
std::vector key, value;
for(std::map::iterator it = m.begin(); it != m.end(); ++it) {
key.push_back(it->first);
value.push_back(it->second);
std::cout first second << std::endl;
}Or even simpler, if you are using the Boost library:
map m;
pair me; // what a map is made of
vector v;
BOOST_FOREACH(me, m) {
v.push_back(me.first);
cout << me.first << "\n";
}Personally, I like the BOOST_FOREACH version because there is less typing and it is very explicit about what it is doing.
Code Snippets
std::map<int, int> m;
std::vector<int> key, value;
for(std::map<int,int>::iterator it = m.begin(); it != m.end(); ++it) {
key.push_back(it->first);
value.push_back(it->second);
std::cout << "Key: " << it->first << std::endl;
std::cout << "Value: " << it->second << std::endl;
}map<int,int> m;
pair<int,int> me; // what a map<int, int> is made of
vector<int> v;
BOOST_FOREACH(me, m) {
v.push_back(me.first);
cout << me.first << "\n";
}Context
Stack Overflow Q#110157, score: 241
Revisions (0)
No revisions yet.