首选/惯用的方式插入到地图 [英] Preferred/idiomatic way to insert into a map

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

问题描述

我已经确定了四种插入 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 成员函数将没有效果,返回 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 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).

    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 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.

    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天全站免登陆