首选/惯用的方式插入到地图 [英] Preferred/idiomatic way to insert into a map
问题描述
我已经确定了四种插入 std :: map
的方法:
std :: map< int,int>功能;
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));
哪些是首选/惯用的方式? (还有另一种我没想过的方式。)
首先, ]
和 insert
成员函数在功能上不等同:
-
运算符[]
将搜索键,如果未找到则插入一个默认构造的值,您为其分配值的引用。显然,如果mapped_type
可以受益于直接初始化而不是默认构造和分配,这可能是低效的。此方法也不能确定插入是否确实发生,或者您是否只覆盖了之前插入的键的值 -
insert $如果密钥已经存在于映射中,并且虽然经常被忘记,但是c $ c>成员函数将没有效果,返回
std :: pair< iterator,bool>
从所有列出的可能性中调用可能感兴趣(最显着的是确定插入是否已经完成) code> insert ,这三个都是几乎等效的。作为提醒,让我们看看标准中的 insert
签名:
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
成员函数预期的参数类型。
最后,当目标是插入时,我会避免使用 operator []
,除非默认没有额外的费用 - 构造和分配 mapped_type
,并且我不在乎确定一个新的键是否有效插入。当使用 insert
时,构建 value_type
可能是要走的路。
I have identified four different ways of inserting 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?)
First of all, operator[]
and insert
member functions are not functionally equivalent :
- 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 themapped_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 anstd::pair<iterator, bool>
which can be of interest (most notably to determine if insertion has actually been done).
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
relies on template argument deduction and could (and in this case will) produce something of a different type than the actualvalue_type
of the map, which will require an additional call tostd::pair
template constructor in order to convert tovalue_type
(ie : addingconst
tofirst_type
)std::pair<int, int>
will also require an additional call to the template constructor ofstd::pair
in order to convert the parameter tovalue_type
(ie : addingconst
tofirst_type
)std::map<int, int>::value_type
leaves absolutely no place for doubt as it is directly the parameter type expected by theinsert
member function.
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屋!