插入地图的首选/惯用方式是什么? [英] What is the preferred/idiomatic way to insert into a map?

查看:84
本文介绍了插入地图的首选/惯用方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经确定了将元素插入std::map的四种不同方式:

I have identified four different ways of inserting elements into a std::map:

std::map<int, int> function;

function[0] = 42;
function.insert(std::map<int, int>::value_type(0, 42));
function.insert(std::pair<int, int>(0, 42));
function.insert(std::make_pair(0, 42));

其中哪种是首选/惯用的方式? (还有我没想到的另一种方式吗?)

Which of those is the preferred/idiomatic way? (And is there another way I have not thought of?)

推荐答案

首先,operator[]insert成员函数在功能上并不等效:

First of all, operator[] and insert member functions are not functionally equivalent :

  • operator[]搜索该键,如果未找到则插入默认构造的值,并返回为其分配值的引用.显然,如果mapped_type可以从直接初始化而不是默认构造和分配中受益,那么这可能是低效的.这种方法还使得无法确定是否确实发生了插入,或者仅覆盖了先前插入的键的值
  • 如果键已经存在于映射中,则insert成员函数将无效,尽管通常会忘记该键,但返回一个有趣的std::pair<iterator, bool>(最重要的是确定插入是否已被实际执行).完成).
  • The operator[] will search for the key, insert a default constructed value if not found, and return a reference to which you assign a value. Obviously, this can be inefficient if the mapped_type can benefit from being directly initialized instead of default constructed and assigned. This method also makes it impossible to determine if an insertion has indeed taken place or if you have only overwritten the value for an previously inserted key
  • The insert member function will have no effect if the key is already present in the map and, although it is often forgotten, returns an std::pair<iterator, bool> which can be of interest (most notably to determine if insertion has actually been done).

在所有列出的呼叫insert的可能性中,所有三个都是几乎等效的.提醒一下,让我们来看一下标准中的insert签名:

From all the listed possibilities to call insert, all three are almost equivalent. As a reminder, let's have look at insert signature in the standard :

typedef pair<const Key, T> value_type;

  /* ... */

pair<iterator, bool> insert(const value_type& x);

那三个电话有何不同?

  • std::make_pair依赖于模板参数推导,并且可能(在这种情况下, )产生的类型与地图的实际value_type类型不同,这将需要额外的调用std::pair模板构造函数以便转换为value_type(即:将const添加到first_type)
  • std::pair<int, int>还需要额外调用std::pair的模板构造函数,以将参数转换为value_type(即:将const添加到first_type)
  • std::map<int, int>::value_type绝对没有疑问,因为它直接是insert成员函数期望的参数类型.
  • std::make_pair relies on template argument deduction and could (and in this case will) produce something of a different type than the actual value_type of the map, which will require an additional call to std::pair template constructor in order to convert to value_type (ie : adding const to first_type)
  • std::pair<int, int> will also require an additional call to the template constructor of std::pair in order to convert the parameter to value_type (ie : adding const to first_type)
  • std::map<int, int>::value_type leaves absolutely no place for doubt as it is directly the parameter type expected by the insert member function.

最后,如果目标是插入,我将避免使用operator[],除非在默认构造和分配mapped_type时没有额外的费用,并且我不在乎确定是否有新的钥匙已有效插入.使用insert时,构造value_type可能是可行的方法.

In the end, I would avoid using operator[] when the objective is to insert, unless there is no additional cost in default-constructing and assigning the mapped_type, and that I don't care about determining if a new key has effectively inserted. When using insert, constructing a value_type is probably the way to go.

这篇关于插入地图的首选/惯用方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆