c++ - How to collect the same type of Map's value -
we have 1 map key , value both int type. have search particular value in map , collect key in 1 vector. code snapshot like
map<int,int>m; map<int,int>::iterator itr; vector<int> v; m.insert(make_pair<int,int>(1,2)); m.insert(make_pair<int,int>(2,2)); m.insert(make_pair<int,int>(3,2)); m.insert(make_pair<int,int>(4,4)); m.insert(make_pair<int,int>(5,5));
and current code like:
for ( itr = m.begin(); itr != m.end(); ++itr ) { if ((*itr).second == 2 ) v.push_back((*itr).first ) }
we optimize it. how can stl algorithm.
it seems me going wrong way, want multimap.
std::multimap<int,int> m; std::vector<int> v; m.insert(std::make_pair<int,int>(2,1)); m.insert(std::make_pair<int,int>(2,2)); m.insert(std::make_pair<int,int>(2,3)); m.insert(std::make_pair<int,int>(4,4)); m.insert(std::make_pair<int,int>(5,5)); typedef std::multimap<int,int>::iterator iterator; std::pair<iterator, iterator> bounds = m.equal_range(2); for(iterator = bounds.first; != bounds.second; ++it) v.push_back(it->second);
Comments
Post a Comment