插入地图的首选/惯用方法是什么?

2022-01-07 00:00:00 insert c++ stl std-pair stdmap

我已经确定了四种将元素插入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 可能很有趣(最值得注意的是确定插入是否已实际完成).
  • 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);

那么这三个调用有何不同?

So how are the three calls different ?

  • std::make_pair 依赖于模板参数推导,并且可以(在这种情况下将)产生与实际 value_type,这将需要额外调用 std::pair 模板构造函数以转换为 value_type(即:添加 const> 到 first_type)
  • std::pair 还需要额外调用 std::pair 的模板构造函数,以便将参数转换为 value_type(即:将 const 添加到 first_type)
  • std::map::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.

相关文章