
Untitled
By: a guest on
May 5th, 2012 | syntax:
None | size: 1.71 KB | hits: 12 | expires: Never
C : push_back a map<string, int> into a vector<map<string, int> > via an iterator?
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;
}
#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());
}
#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"; }
}
vector<map<string, int> > vec_counters;
vector<std::pair<string, int> > vec_counters;
std::vector<std::pair<std::string, int> > vec_counters;
std::copy(counters.begin(),
counters.end(),
std::back_inserter(vec_counters));