C ++: push_back a map <string, int> to a vector <map <string, int >> via an iterator?

I am currently studying an accelerated C ++ book (Koenig / Moo) and I am having problems with one of the exercises. The problem is to write a program that takes as input some sequence of words, which it then stores on the map. Lines are words entered, and the number associated with it is the number of times each word occurs. Then you have to sort the words by the number of times; that is, by value, not by key. You cannot sort the map by value, so I tried to copy the elements into a vector instead, which I was going to sort using a predicate. Unfortunately, all I get is a screen full of errors from g ++. It seems that they come from the same place - putting the elements of my map into my vector, which I am trying to do as follows:

int main() { map<string, int> counters; cout << "Enter some words followed by end-of-file (ctrl-d): "; string word; while (cin >> word) ++counters[word]; //maps cannot be sorted by values, so pass the elements of counters to a vector vector<map<string, int> > vec_counters; map<string, int>::const_iterator it = counters.begin(); while (it != counters.end()) { vec_counters.push_back(*it); ++it; } 

This is obviously only the first part, but I can’t even compile it. I get an error message:

32:31: error: there is no corresponding function to call std :: vector, int β†’ :: push_back (const std :: pair, int> &) / usr / include / c ++ / 4.5 / bits / stl_vector.h: 741 : 7: note: candidate: void std :: vector <_Tp, _Alloc> :: push_back (const value_type &) [with _Tp = std :: map, int>, _Alloc = std :: allocator, int β†’, value_type = std :: map, int>]

I think I'm doing something fundamentally stupid ... but I can’t see me for life that ..

Any help would be great!

C

+4
source share
4 answers

I am sure that you are not looking for a map vector:

 #include <map> #include <vector> #include <string> #include <iostream> using namespace std; int main() { map<string, int> counters; cout << "Enter some words followed by end-of-file (ctrl-d): "; string word; while (cin >> word) ++counters[word]; vector<std::pair<string, int> > vec(counters.begin(), counters.end()); } 
+3
source

A little off topic, but here's a sexual solution using bimap, which is a map in which both sides work like keys.

 #include <iostream> #include <sstream> #include <string> #include <boost/bimap.hpp> #include <boost/bimap/list_of.hpp> #include <boost/bimap/set_of.hpp> int main() { boost::bimap<boost::bimaps::set_of<std::string>, boost::bimaps::list_of<int>> m; for (std::string line; std::getline(std::cin, line); ) { ++m.left[line]; } auto & bml = m.left; auto & bmr = m.right; bmr.sort(); for (auto const & p : bml) { std::cout << p.first << " => " << p.second << "\n"; } for (auto const & p : bmr) { std::cout << p.first << " => " << p.second << "\n"; } } 
+2
source

You want to put map elements in a vector. Cards are made of pairs, not other cards. Therefore, your vector must be a vector of pairs.

However, it seems like the reason you want the vector to consist primarily of sorting by values ​​on the map. Why not make a map< int, string > instead? This will cause it to be sorted in ascending order by the following parameters: Elements on the map are sorted from lower to higher key values ​​after a certain strict weak ordering.

+1
source

Highlighting a std::map<std::string, int>::const_iterator gives you std::pair<std:string, int> , not std::map<std::string, int> , so instead:

 vector<map<string, int> > vec_counters; 

Do you want to:

 vector<std::pair<string, int> > vec_counters; 
0
source

Source: https://habr.com/ru/post/1392809/


All Articles