简体   繁体   English

如何将对插入地图

[英]How to insert pair into map

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 object and I must make a pair out of this? 我怎么做,因为我试图插入一对和一个对象 ,我必须从中做出一对

Should I create a new pair using make_pair() out of the pair and object that I have? 我应该使用make_pair()从我拥有的对和对象中创建一个新对吗? If so, could you please let me know how to do this? 如果是的话,你能告诉我怎么做吗?

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

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, emplace can be thought of as a version of insert that takes the key and value as separate arguments (it can actually take any combination of arguments that the corresponding pair type's constructors can take). 对于map, emplace可以被认为是insert一个版本,它将key和value作为单独的参数(它实际上可以采用相应的pair类型的构造函数可以采用的任何参数组合)。 In addition to being syntactically cleaner, it's also potentially more efficient than make_pair , because make_pair will usually produce an output whose type doesn't precisely match the value_type of the container, and so it incurs an unnecessary type conversion. 除了语法更清晰之外,它还可能比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 (eg unique_ptr ). 这避免了使用emplace的轻微意外,但如果键或值类型是仅移动的(例如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. 从理论上讲,这也可能稍微低一些,但是在某种程度上,任何中途不错的编译器都可以轻松地优化掉。

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. 仅当密钥不存在时才有效,迭代器指向具有键值的对,bool指示是否已插入。

  2. Easier, but if it does not already exist the object is first default constructed and then assigned instead of being copy constructed 更容易,但如果它尚不存在,则首先默认构造对象,然后分配对象而不是复制构造

If you don't have to worry about performance, just choose by whether or not you wish to erase the previous entry. 如果您不必担心性能,只需选择是否要删除上一个条目。

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. 我们的地图将其键作为数字对。我们可以使用点(。)运算符访问对变量的整数值。

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

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