简体   繁体   English

为什么std :: map没有insert类型的插入函数(key&k,val&v)

[英]Why does std::map not have an insert function of type insert(key &k, val &v)

Why does std::map not support an insert like the following: 为什么std::map不支持如下插入:

std::map<obj1, obj2> map_int;

void insert_map(obj1 &key, obj2 &val)
{
    map_int.insert(key, val);
}

I know that the above is incorrect. 我知道上面的内容不正确。 I want to know what prevents from designing the insert function like that. 我想知道是什么阻止了这样设计插入函数。 It is more intuitive than creating a pair IMO. 它比创建一对IMO更直观。

It's called emplace() : 它叫做emplace()

 std::map<std::string, std::string> m; // uses pair's template constructor m.emplace("d", "ddd"); 

In C++17, you can use: 在C ++ 17中,您可以使用:

map_int.try_emplace(key, val);

This actually returns some useful information: 这实际上返回一些有用的信息:

std::pair<std::map<obj1, obj2>::iterator, bool> p = map_int.try_emplace(key, val);
  • p.first is an iterator that points to the element with the given key. p.first是一个迭代器,它指向具有给定键的元素。
  • p.second indicates whether the insertion took place. p.second表示插入是否发生。

You can achieve a similar effect in C++11 with map_int.emplace(key, val) , but there are some subtle differences (related to copying/moving of values). 您可以使用map_int.emplace(key, val)在C ++ 11中实现类似的效果,但是存在一些细微差别(与复制/移动值有关)。 Moreover, try_emplace allows for convenient construction of arbitrary elements: 而且, try_emplace允许方便地构造任意元素:

struct X { X(int, char, bool); };

std::map<int, X> m;
m.try_emplace(10, 20, 'x', false);  // constructs X(20, 'x', false) if new

A similar operation in C++11 would look like this: C ++ 11中的类似操作如下所示:

m.emplace(
    std::piecewise_construct,
    std::forward_as_tuple(10),
    std::forward_as_tuple(20, 'x', false));

std::map does have an insert() method (several, in fact), it just has a different signature than what you are expecting. std::map 确实有一个insert()方法(事实上有几个),它只有一个不同于你期望的签名。 You need to pass it a std::pair containing the desired key and value, eg: 你需要传递一个包含所需键和值的std::pair ,例如:

std::map<obj1, obj2> map_int;

void insert_map(obj1 &key, obj2 &val)
{
    map_int.insert(std::make_pair(key, val));
}

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

相关问题 为什么我们需要pair<iterator,bool> 插入 (value_type&amp;&amp; val); std::set 的插入版本 function</iterator,bool> - Why do we need pair<iterator,bool> insert (value_type&& val); version of std::set's insert function 为什么std :: map :: insert文档引用的键参数不存在 - Why does std::map::insert docs refer to a key argument that does not exist 集合必须对std :: map有多大 <k,v> 超过排序的std :: vector <std::pair<k,v> &gt;? - How large does a collection have to be for std::map<k,v> to outpace a sorted std::vector<std::pair<k,v> >? 是std :: map <K,V> :: iterator实例化std :: map <K,V> ? - Does std::map<K,V>::iterator instantiate std::map<K,V>? 为什么std :: map :: insert不能使用键和值而不是std :: pair? - Why can't std::map::insert take a key and a value rather than an std::pair? 在std :: map中插入std :: map - insert std::map in std::map 为什么我插入 std::map 失败? - Why is my insert into std::map failing? 为什么std :: map :: insert在以下示例中失败? - Why the std::map::insert failed in the following example? 为什么std :: map有一个find成员函数? - Why does std::map have a find member function? 对std :: map有什么限制 <K, V> :: mapped_type? - What are the restrictions on std::map<K, V>::mapped_type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM