如何在 C++ 中填充 int 和 vector<int> 的映射?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15192534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:09:12  来源:igfitidea点击:

How can I fill a map of int and vector<int> in C++?

c++dictionaryvectorcontainersstdmap

提问by george mano

I have been working with <map>, where I declared a map as follows:

我一直在使用<map>,在那里我声明了一张地图,如下所示:

map <int, vector<int> > tree;

I am now trying to assign values to it. My goal is to place multiple values as elements of its keys. Something like this:

我现在正在尝试为它赋值。我的目标是将多个值作为其键的元素。像这样的东西:

0=null
1=>0
2=>1,0
3=>2,1,0
4=>3,2,1,0
5=>0

I tried to assign to the map like this, but it does not work:

我试图像这样分配给地图,但它不起作用:

tree[3]=vector<int>(2,1,0);

However, the following two ways of assigning work:

但是,有以下两种分配工作的方式:

tree[1]=vector<int>(0);
tree[2]=vector<int>(1,0);

Where is the problem? How can I make a function that works as a Python dictionary?

问题出在哪儿?如何制作一个可用作 Python 字典的函数?

I am not using C++11.

我没有使用 C++11。

回答by Daniel Frey

With C++11, you could try:

使用 C++11,您可以尝试:

tree[3]=vector<int>({2,1,0});

Other than that, the question could use more details and some code of what you already tried...

除此之外,这个问题可以使用更多的细节和一些你已经尝试过的代码......

回答by Drew Dormann

Since you are asking for a C++03 answer, this (more verbose than C++11) solution will work.

由于您要求的是 C++03 答案,因此此(比 C++11 更冗长)解决方案将起作用。

tree[3].push_back(2);
tree[3].push_back(1);
tree[3].push_back(0);

回答by honk

Please note that the following two lines are not doing what you expect:

请注意,以下两行没有按照您的预期执行:

tree[1] = vector<int>(0);
tree[2] = vector<int>(1, 0);

The first parameter of the corresponding vector's constructoris the initial size of the container. The second parameter is the value to initialize elements of the container with. So, the first line constructs an empty vector and the second line constructs a vector with one element which is initialized to 0.

对应向量的构造函数的第一个参数是容器的初始大小。第二个参数是用于初始化容器元素的值。因此,第一行构造一个空向量,第二行构造一个向量,其中一个元素被初始化为 0。

As indicated by the other answers, push_back()is a good option if you cannot use C++11 features. However, once you upgrade to C++11, you can also initialize your map by using nested list initializationas follows:

如其他答案所示,push_back()如果您不能使用C++11 features,这是一个不错的选择。但是,一旦升级到 C++11,您还可以使用嵌套列表初始化来初始化您的地图,如下所示:

int main() {
    std::map<int, std::vector<int>> tree{
        {1, {0}}, {2, {1, 0}}, {3, {2, 1, 0}}, {4, { 3, 2, 1, 0 }}, {5, { 0 }}
    };

    for (auto const &kv : tree) {
        std::cout << kv.first << " =>";
        for (auto const &i : kv.second)
            std::cout << " " << i;
        std::cout << std::endl;
    }

    return 0;
}

Output:

输出:

1 => 0
2 => 1 0
3 => 2 1 0
4 => 3 2 1 0
5 => 0

1 => 0
2 => 1 0
3 => 2 1 0
4 => 3 2 1 0
5 => 0

Code on Ideone

Ideone 上的代码

回答by sehe

Have you considered std::multi_map?

你考虑过std::multi_map吗?

#include <map>

int main()
{
    std::multimap<int, int> map;

    for (int i=1; i < 6; i++)
        for (int j=1; j < i; j++)
            map.insert(std::make_pair(i, j));
}

回答by Daniel Frey

Without C++11, the code won't be as elegant:

没有 C++11,代码不会那么优雅:

tree[0]; // create empty vector for index 0
tree[1].push_back(0);
tree[2].push_back(1);
tree[2].push_back(0);
tree[3].push_back(2);
tree[3].push_back(1);
tree[3].push_back(0);
tree[4].push_back(3);
tree[4].push_back(2);
tree[4].push_back(1);
tree[4].push_back(0);
tree[5].push_back(0);

回答by Rapptz

I don't particularly likeva_args but the solution is "neater" to a degree than most as long as you (the user) don't mess it up, i.e. mixing types. Another downside is that your vector cannot contain -1, but your example case doesn't show it.

我不是特别喜欢va_args,但只要您(用户)不搞砸,即混合类型,解决方案在一定程度上比大多数解决方案“更简洁”。另一个缺点是您的向量不能包含 -1,但您的示例案例没有显示它。

#include <vector>
#include <cstdarg>
#include <iostream>

//Unsafe but it works.
template<typename T>
std::vector<T> make_vector(T num, ...) {
    std::vector<T> result;
    va_list args;
    va_start(args,num);
    for(T i = num; i != -1; i = va_arg(args,T))
        result.push_back(i);
    va_end(args);
    return result;
}

int main() {
    std::vector<int> v = make_vector(0,1,2,3,-1); //-1 to stop
    //do stuff with vector v
}

回答by Mr Fooz

As Daniel Frey points out, you can use

正如丹尼尔弗雷指出的那样,你可以使用

tree[3] = vector<int>({2,1,0})

In python-like pseudo-code, the vector constructor being used here is

在类似 python 的伪代码中,这里使用的向量构造函数是

def vector(arr)

The original post suggests you're trying to use a constructor of the form

原始帖子建议您尝试使用表单的构造函数

def vector(*args)

which doesn't exist.

这是不存在的。

If you're not using C++11, consider using one of vector's other constructors.

如果您不使用 C++11,请考虑使用vector其他构造函数之一