Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 1.71 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. C   : push_back a map<string, int> into a vector<map<string, int> > via an iterator?
  2. int main()
  3. {
  4.     map<string, int> counters;
  5.  
  6.     cout << "Enter some words followed by end-of-file (ctrl-d): ";
  7.     string word;
  8.     while (cin >> word)
  9.        ++counters[word];
  10.  
  11.     //maps cannot be sorted by values, so pass the elements of counters to a vector
  12.     vector<map<string, int> > vec_counters;
  13.  
  14.     map<string, int>::const_iterator it = counters.begin();
  15.     while (it != counters.end()) {
  16.        vec_counters.push_back(*it);
  17.        ++it;
  18.     }
  19.        
  20. #include <map>
  21. #include <vector>
  22. #include <string>
  23. #include <iostream>
  24.  
  25. using namespace std;
  26. int main()
  27. {
  28.     map<string, int> counters;
  29.  
  30.     cout << "Enter some words followed by end-of-file (ctrl-d): ";
  31.     string word;
  32.     while (cin >> word)
  33.        ++counters[word];
  34.  
  35.     vector<std::pair<string, int> > vec(counters.begin(), counters.end());
  36. }
  37.        
  38. #include <iostream>
  39. #include <sstream>
  40. #include <string>
  41. #include <boost/bimap.hpp>
  42. #include <boost/bimap/list_of.hpp>
  43. #include <boost/bimap/set_of.hpp>
  44.  
  45. int main()
  46. {
  47.     boost::bimap<boost::bimaps::set_of<std::string>, boost::bimaps::list_of<int>> m;
  48.  
  49.     for (std::string line; std::getline(std::cin, line); )
  50.     {
  51.         ++m.left[line];
  52.     }
  53.  
  54.     auto & bml = m.left;
  55.     auto & bmr = m.right;
  56.  
  57.     bmr.sort();
  58.  
  59.     for (auto const & p : bml) { std::cout << p.first << " => " << p.second << "n"; }
  60.     for (auto const & p : bmr) { std::cout << p.first << " => " << p.second << "n"; }
  61. }
  62.        
  63. vector<map<string, int> > vec_counters;
  64.        
  65. vector<std::pair<string, int> > vec_counters;
  66.        
  67. std::vector<std::pair<std::string, int> > vec_counters;
  68.        
  69. std::copy(counters.begin(),
  70.           counters.end(),
  71.           std::back_inserter(vec_counters));