简体   繁体   English

C ++:通过迭代器将map <string,int>推送到vector <map <string,int >>中?

[英]C++ : push_back a map<string, int> into a vector<map<string, int> > via an iterator?

I'm currently studying the book accelerated C++ (Koenig / Moo) and I'm having trouble with one of the exercises. 我目前正在学习加速C ++(Koenig / Moo)这本书,我在其中一个练习中遇到了麻烦。 The problem is to write a program which takes as an input some sequence of words which it then stores in a map. 问题是编写一个程序,它将一些单词序列作为输入,然后将它存储在地图中。 The strings are the words entered and the associated int is the number of times each word occurs. 字符串是输入的单词,关联的int是每个单词出现的次数。 You then have to sort the words by the number of times they occur; 然后,您必须按其出现的次数对单词进行排序; that is, by the value and not the key. 也就是说,通过价值而不是关键。 You can't sort a map by the value, so I tried to copy the elements to a vector instead, which I intended to sort using a predicate. 您无法按值对地图进行排序,因此我尝试将元素复制到矢量中,我打算使用谓词进行排序。 Unfortunately, all I get is a screen full of errors from g++. 不幸的是,我得到的是一个充满g ++错误的屏幕。 They seem to stem from the same place - putting the elements of my map into my vector, which I try to do like this: 它们似乎来自同一个地方 - 将我的地图元素放入我的矢量中,我尝试这样做:

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 just the first part, but I can't get even this to compile. 这显然只是第一部分,但我甚至无法编译。 I get the error: 我收到错误:

32:31: error: no matching function for call to std::vector, int> >::push_back(const std::pair, int>&)' /usr/include/c++/4.5/bits/stl_vector.h:741:7: note: candidate is: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::map, int>, _Alloc = std::allocator, int> >, value_type = std::map, int>] 32:31:错误:没有匹配函数来调用std :: vector,int >>> :: push_back(const std :: pair,int>&)'/ usr /include/c++/4.5/bits/stl_vector.h: 741:7:注意:候选者是: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 guess I'm doing something fundamentally stupid... but I can't for the life of me see what.. 我想我做的事情从根本上是愚蠢的...但我不能为我的生活看到什么..

Any help would be great! 任何帮助都会很棒!

C C

I'm pretty sure you aren't looking for a vector of maps: 我很确定你不是在寻找地图矢量:

#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());
}

A bit off-topic, but here's a sexy solution using a bimap, which is a map in which both sides work as keys. 有点偏离主题,但这是一个使用bimap的性感解决方案,这是一个双方都作为键的地图。

#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"; }
}

You want to put the elements of a map in a vector. 您想将地图的元素放在矢量中。 Maps are made out of pairs, not other maps. 地图是成对的,而不是其他地图。 Therefore, your vector should be a vector of pairs. 因此,您的向量应该是对的向量。

That said, it looks like the reason you want the vector in the first place is to sort by values in the map. 也就是说,看起来你想要矢量的原因是在地图中按值排序。 Why not then make your map map< int, string > instead? 那么为什么不使你的地图map< int, string >呢? This will result in it being sorted in ascending order by the ints : Elements in the map are sorted from lower to higher key value following a specific strict weak ordering criterion. 这将导致它按整数按升序排序:映射中的元素按照特定的严格弱排序标准从较低到较高的键值排序。

Dereferencing a std::map<std::string, int>::const_iterator gives you a std::pair<std:string, int> , not a std::map<std::string, int> , so instead of this: 解除引用std::map<std::string, int>::const_iterator会给你一个std::pair<std:string, int> ,而不是std::map<std::string, int> ,所以代替这个:

vector<map<string, int> > vec_counters;

you want this: 你要这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM