C++ 如何将对插入地图

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

How to insert pair into map

c++insertmap

提问by Myx

I have the following map structure: map < pair < int,int >, object* >and I wish to insert into it.

我有以下地图结构:map < pair < int,int >, object* >我希望插入其中。

How would I do it since I am trying to insert a pair and an objectand I must make a pairout of this?

既然我试图插入一对和一个对象,我该怎么,我必须这个做一对

Should I create a new pair using make_pair()out of the pair and object that I have? If so, could you please let me know how to do this?

我应该使用make_pair()我拥有的配对和对象创建一个新配对吗?如果是这样,你能告诉我如何做到这一点吗?

回答by Nick Meyer

object * myObject = // get an object somehow
myMap.insert(std::make_pair(std::make_pair(1,2), myObject));

or

或者

typedef map<pair<int, int>, object *> MapType;
object * myObject = // get an object somehow
myMap.insert(MapType::value_type(std::make_pair(1,2), myObject));

回答by Geoff Romer

Assuming you're using C++11 or later, the best approach is probably:

假设您使用的是 C++11 或更高版本,最好的方法可能是:

object * myObject = // get an object somehow
myMap.emplace({1,2}, myObject);

For maps, emplacecan be thought of as a version of insertthat takes the key and value as separate arguments (it can actually take any combination of arguments that the corresponding pairtype's constructors can take). In addition to being syntactically cleaner, it's also potentially more efficient than make_pair, because make_pairwill usually produce an output whose type doesn't precisely match the value_typeof the container, and so it incurs an unnecessary type conversion.

对于映射,emplace可以将其视为insert将键和值作为单独参数的版本(它实际上pair可以采用相应类型的构造函数可以采用的任何参数组合)。除了在语法上更简洁之外,它还可能比 更有效make_pair,因为make_pair通常会产生类型value_type与容器的不完全匹配的输出,因此会导致不必要的类型转换。

I used to recommend this, which also only works in C++11 or later:

我曾经推荐过这个,它也只适用于 C++11 或更高版本:

object * myObject = // get an object somehow
myMap.insert({{1,2}, myObject});

This avoids the slightly surprising use of emplace, but it formerly didn't work if the key or value type are move-only (e.g. unique_ptr). That's been fixed in the standard, but your standard library implementation may not have picked up the fix yet. This might also theoretically be slightly less efficient, but in a way that any halfway decent compiler can easily optimize away.

这避免了使用有点令人惊讶的emplace,但如果键或值类型是仅移动的(例如unique_ptr),它以前不起作用。这已在标准中修复,但您的标准库实现可能尚未修复。从理论上讲,这也可能效率稍低,但在某种程度上,任何不错的编译器都可以轻松优化掉。

回答by Matthieu M.

There are two ways:

有两种方式:

typedef std::map<int,Object> map_t;
map_t map;
Object obj;

std::pair<map_t::iterator, bool> result = map.insert(std::make_pair(1,obj)); // 1

map[1] = obj; // 2
  1. Only works if the key is not already present, the iterator points to the pair with the key value and the bool indicates if it has been inserted or not.

  2. Easier, but if it does not already exist the object is first default constructed and then assigned instead of being copy constructed

  1. 仅当键不存在时才有效,迭代器指向键值对,布尔值指示是否已插入。

  2. 更容易,但如果它不存在,则首先默认构造对象,然后分配而不是复制构造

If you don't have to worry about performance, just choose by whether or not you wish to erase the previous entry.

如果您不必担心性能,只需选择是否要删除之前的条目。

回答by Aryan Sharma

int a=10,b=20;

map < pair < int,int >, int > m;

pair < int,int >numbers = make_pair(a,b);

int sum=a+b;

m[numbers]=sum;

Our map will have its key as pairs of numbers.We can access the integer values of pair variable using dot(.) operator.

我们的地图将其键作为数字对。我们可以使用 dot(.) 运算符访问对变量的整数值。