我怎样才能push_back一个map转换为向量>>通过迭代器?

发布于 2024-12-28 13:43:36 字数 1247 浏览 3 评论 0 原文

我目前正在学习《Accelerated C++》(Koening/Moo)一书,但其中一个练习遇到了问题。任务是编写一个程序,将一些单词序列作为输入,然后将其存储在 map 中。字符串是输入的单词,关联的 int 是每个单词出现的次数。然后你必须按照单词出现的次数对它们进行排序;也就是说,通过值而不是键。您无法按值对映射进行排序,因此我尝试将元素复制到向量中,我打算使用谓词对其进行排序。不幸的是,我得到的只是一个充满 g++ 错误的屏幕。它们似乎源于同一个地方 - 将地图的元素放入向量中,我尝试这样做:

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

这显然只是第一部分,但我什至无法编译它。我收到以下错误:

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'm currently studying the book Accelerated C++ (Koening/Moo) and I'm having trouble with one of the exercises. The task is to write a program which takes as an input some sequence of words which it then stores in a map<string, int>. The strings are the words entered and the associated int is the number of times each word occurs. 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++. 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 following 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>]

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我一直都在从未离去 2025-01-04 13:43:36

我很确定您不是在寻找地图矢量:

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

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());
}
黑白记忆 2025-01-04 13:43:36

有点偏离主题,但这里有一个使用 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"; }
}

A bit off-topic, but here's a sexy solution using a bimap, which is a map in which both sides work as 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"; }
}
想念有你 2025-01-04 13:43:36

您想将地图的元素放入向量中。地图是成对制作的,而不是其他地图。因此,你的向量应该是成对的向量。

也就是说,看起来您首先想要向量的原因是按地图中的值进行排序。为什么不制作你的地图 map< int, string > 代替?这将导致它按 int 升序排序:映射中的元素按照特定的严格弱排序标准从较低到较高的键值排序。

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? 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.

落花随流水 2025-01-04 13:43:36

取消引用 std::map::const_iterator 会得到一个 std::pair,而不是 >std::map,所以不要这样:

vector<map<string, int> > vec_counters;

你想要这样:

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

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:

vector<map<string, int> > vec_counters;

you want this:

vector<std::pair<string, int> > vec_counters;
祁梦 2025-01-04 13:43:36

如果您想在插入地图条目后对矢量进行排序,那么您还可以使用 std::set 相反。集合中的元素始终是排序的。如果您想稍后插入新元素,这尤其有用。但是,当您想要按值对映射条目进行排序时,您必须将翻转的键值对存储到集合中。为了清楚起见,我省略了读取以下 C++11 代码中的输入:

int main() {
    std::map<std::string, int> counters{
        {"sorted", 2}, {"value" , 5}, {"is" , 2}, {"by" , 3}, {"this" , 1}
    };

    std::set<std::pair<int, std::string>> s;  // Use a set instead of a vector.

    for (auto const &kv : counters)
        s.emplace(kv.second, kv.first);  // Flip the pairs.

    for (auto const &vk : s)
        std::cout << vk.second << ": " << vk.first << std::endl;

    return 0;
}

输出:

这个:1
是:2
排序:2
作者:3
值:5

如果要按降序排序,可以按如下方式定义集合:

using flippedPair = std::pair<int, std::string>;
std::set<flippedPair, std::greater<flippedPair>> s;

Shorter C Coliru 上的 ++17 代码

If you want to sort your vector after inserting your map entries into it, then you can also use a std::set instead. The elements in a set are always sorted. This is especially helpful if you want to insert new elements at a later time. However, as you want to sort your map entries by value, you have to store flipped key-value pairs into the set. I omitted reading the input in the following C++11 code for the sake of clarity:

int main() {
    std::map<std::string, int> counters{
        {"sorted", 2}, {"value" , 5}, {"is" , 2}, {"by" , 3}, {"this" , 1}
    };

    std::set<std::pair<int, std::string>> s;  // Use a set instead of a vector.

    for (auto const &kv : counters)
        s.emplace(kv.second, kv.first);  // Flip the pairs.

    for (auto const &vk : s)
        std::cout << vk.second << ": " << vk.first << std::endl;

    return 0;
}

Output:

this: 1
is: 2
sorted: 2
by: 3
value: 5

If you want to sort in descending order, you can define the set as follows:

using flippedPair = std::pair<int, std::string>;
std::set<flippedPair, std::greater<flippedPair>> s;

Shorter C++17 code on Coliru

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文