<i id='LFvpU'><tr id='LFvpU'><dt id='LFvpU'><q id='LFvpU'><span id='LFvpU'><b id='LFvpU'><form id='LFvpU'><ins id='LFvpU'></ins><ul id='LFvpU'></ul><sub id='LFvpU'></sub></form><legend id='LFvpU'></legend><bdo id='LFvpU'><pre id='LFvpU'><center id='LFvpU'></center></pre></bdo></b><th id='LFvpU'></th></span></q></dt></tr></i><div id='LFvpU'><tfoot id='LFvpU'></tfoot><dl id='LFvpU'><fieldset id='LFvpU'></fieldset></dl></div>

    <small id='LFvpU'></small><noframes id='LFvpU'>

    • <bdo id='LFvpU'></bdo><ul id='LFvpU'></ul>
  1. <tfoot id='LFvpU'></tfoot>

    <legend id='LFvpU'><style id='LFvpU'><dir id='LFvpU'><q id='LFvpU'></q></dir></style></legend>

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

      What is the preferred/idiomatic way to insert into a map?(插入地图的首选/惯用方法是什么?)

      <small id='I0trm'></small><noframes id='I0trm'>

    2. <i id='I0trm'><tr id='I0trm'><dt id='I0trm'><q id='I0trm'><span id='I0trm'><b id='I0trm'><form id='I0trm'><ins id='I0trm'></ins><ul id='I0trm'></ul><sub id='I0trm'></sub></form><legend id='I0trm'></legend><bdo id='I0trm'><pre id='I0trm'><center id='I0trm'></center></pre></bdo></b><th id='I0trm'></th></span></q></dt></tr></i><div id='I0trm'><tfoot id='I0trm'></tfoot><dl id='I0trm'><fieldset id='I0trm'></fieldset></dl></div>

        <tbody id='I0trm'></tbody>
      1. <legend id='I0trm'><style id='I0trm'><dir id='I0trm'><q id='I0trm'></q></dir></style></legend>
          <bdo id='I0trm'></bdo><ul id='I0trm'></ul>

              <tfoot id='I0trm'></tfoot>




                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.




                What is the past-the-end iterator in STL C++?(STL C++ 中的最后迭代器是什么?)
                vector::at vs. vector::operator[](vector::at 与 vector::operator[])
                C++ equivalent of StringBuffer/StringBuilder?(C++ 等效于 StringBuffer/StringBuilder?)
                Adding types to the std namespace(将类型添加到 std 命名空间)
                Is the C++ std::set thread-safe?(C++ std::set 线程安全吗?)
                How to use std::find/std::find_if with a vector of custom class objects?(如何将 std::find/std::find_if 与自定义类对象的向量一起使用?)
              1. <tfoot id='Hovrb'></tfoot>

                  <small id='Hovrb'></small><noframes id='Hovrb'>

                  <i id='Hovrb'><tr id='Hovrb'><dt id='Hovrb'><q id='Hovrb'><span id='Hovrb'><b id='Hovrb'><form id='Hovrb'><ins id='Hovrb'></ins><ul id='Hovrb'></ul><sub id='Hovrb'></sub></form><legend id='Hovrb'></legend><bdo id='Hovrb'><pre id='Hovrb'><center id='Hovrb'></center></pre></bdo></b><th id='Hovrb'></th></span></q></dt></tr></i><div id='Hovrb'><tfoot id='Hovrb'></tfoot><dl id='Hovrb'><fieldset id='Hovrb'></fieldset></dl></div>
                    • <bdo id='Hovrb'></bdo><ul id='Hovrb'></ul>

                          <legend id='Hovrb'><style id='Hovrb'><dir id='Hovrb'><q id='Hovrb'></q></dir></style></legend>
                            <tbody id='Hovrb'></tbody>