在 C++ 中的映射中插入值的向量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10487865/
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 14:08:04  来源:igfitidea点击:

Insert vector for value in map in C++

c++

提问by Akavall

I am stuck on trying to figure out how to insert a vector for a value in a map. For example:

我一直在试图弄清楚如何在地图中插入一个值的向量。例如:

#include <iostream>
#include <vector>
#include <map>

using namespace std;

int main()

{

    map <int, vector<int> > mymap;   

    mymap.insert(pair<int, vector<int> > (10, #put something here#));

    return 0;
}

I don't know what syntax to use insert a vector for value. I tried {1,2}, but that failed. What syntax should I use?

我不知道使用什么语法插入向量作为值。我试过了{1,2},但失败了。我应该使用什么语法?

Everything works if I declare a vector in advance and give it a name, but I don't want to do that, as I want to have a map with a lot of vectors.

如果我提前声明一个向量并给它一个名字,一切都会正常,但我不想这样做,因为我想要一张包含很多向量的地图。

Thank You in Advance

先感谢您

采纳答案by jamesdlin

Basically your question is not about inserting std::vectorinto a std::map. Your question is how can you easily create an anonymousstd::vectorwith arbitrary initial element values.

基本上你的问题不是关于插入std::vectorstd::map. 您的问题是如何轻松创建具有任意初始元素值的匿名对象std::vector

In ISO C++03, you can't. C++11 allows using initialization lists for this, however.

在 ISO C++03 中,你不能。 但是,C++11 允许为此使用初始化列表

If you are stuck with a C++03 compiler, you possibly could create a helper function to return a vector with specified elements:

如果您坚持使用 C++03 编译器,您可能可以创建一个辅助函数来返回具有指定元素的向量:

std::vector<int> make_vector(int a, int b)
{
    std::vector<int> v;
    v.push_back(a);
    v.push_back(b);
    return v;
}

If the vectors you're inserting are of different sizes, you could use a variadic function, although doing so would require that you either pass along the number of elements or have a reserved sentinel value.

如果您插入的向量大小不同,您可以使用可变参数函数,尽管这样做需要您传递元素数量或保留标记值。

回答by Edward Loper

If you want an empty vector you could do:

如果你想要一个空向量,你可以这样做:

mymap.insert(pair<int,vector<int> >(10, vector<int>()));

You could then add whatever elements you want with something like:

然后,您可以添加任何您想要的元素,例如:

mymap[10].push_back(1);
mymap[10].push_back(2);

Edit: Removed incorrect assertion that the vectors would be copied if/when the map grows. As the commenters pointed out, this is not true for std::map, which is node-based.

编辑:删除了如果/当地图增长时将复制向量的错误断言。正如评论者指出的那样,这对于基于节点的 std::map 来说并非如此。

回答by David Brown

If you are using C++11 you can use vector's initialization list constructor(the last constructor in that list) which would look like this:

如果您使用的是 C++11,您可以使用 vector 的初始化列表构造函数(该列表中的最后一个构造函数),如下所示:

mymap.insert(pair<int, vector<int> > (10, {1, 2, 3}));

If you can only use C++03, vectorhas a constructor that takes a size and a default value for each element that might be enough for you. Otherwise you will have to construct the vector and then insert it. If you want to avoid an unnessicary copy of the vector when inserting you could swapit in like so:

如果您只能使用 C++03,则vector有一个构造函数,该构造函数为每个可能对您足够的元素采用大小和默认值。否则,您将不得不构建向量然后插入它。如果你想在插入时避免向量的不必要的副本,你可以swap像这样:

vector<int> myvec;
myvec.push_back(1);
myvec.push_back(2);
mymap[10].swap(myvec);

This way the vector won't need to be copied. You'll get an extra vector default construction but that's not very expensive.

这样就不需要复制向量了。你会得到一个额外的向量默认构造,但这并不是很昂贵。

回答by Edward Strange

#put something here#= vector<int>{1,2}

#put something here#= vector<int>{1,2}

I'm surprised though that {1,2}didn't work. Are you not using a C++11 compiler? If not then you can only create the vector with default constructor there (no values), or fill it with values first and stick it in.

我很惊讶,虽然那{1,2}没有用。你没有使用 C++11 编译器吗?如果没有,那么您只能在那里创建带有默认构造函数的向量(没有值),或者先用值填充它并将其粘贴进去。

回答by Rob?

This should work in C++2003 compilers.

这应该适用于 C++2003 编译器。

#include <iostream>
#include <vector>
#include <map>
#include <cassert>

using namespace std;

std::vector<int> make_vector(int a, int b) {
  std::vector<int> result;
  result.push_back(a);
  result.push_back(b);
  return result;
}

int main()

{

    map <int, vector<int> > mymap;

    mymap.insert(make_pair(10, make_vector(1,2)));
    // Or, alternatively:
    //   mymap[10] = make_vector(1,2);

    assert(mymap[10][0] == 1);
    assert(mymap[10][1] == 2);

    return 0;
}

回答by Matthieu M.

C++03 does not have initializer lists, which can be a pain to initialize collections.

C++03 没有初始化列表,初始化集合可能很麻烦。

If you cannot upgrade to a more modern version of the compiler, you can always use the Boost.Assignmentlibrary. It has a list_offunction precisely for this.

如果您无法升级到更现代的编译器版本,您可以随时使用该Boost.Assignment库。它list_of正是为此而发挥作用。

#put something here# -> boost::assign::list_of(1)(2)