Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/container/map/insert"

From cppreference.com
< cpp‎ | container‎ | map
(Undo Example made by Ybab32, anime inserts do not belong on cppreference)
m (Return value: templatized.)
 
(14 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{cpp/container/map/title | insert}}
+
{{cpp/container/map/title|insert}}
 
{{cpp/container/map/navbar}}
 
{{cpp/container/map/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl | num=1 |  
+
{{dcl|num=1|
std::pair<iterator,bool> insert( const value_type& value );
+
std::pair<iterator, bool> insert( const value_type& value );
 
}}
 
}}
{{dcl | num=2 | since=c++11 |
+
{{dcl|num=2|since=c++11|
 
template< class P >
 
template< class P >
std::pair<iterator,bool> insert( P&& value );
+
std::pair<iterator, bool> insert( P&& value );
 
}}
 
}}
{{dcl | num=3 | since=c++17 |
+
{{dcl|num=3|since=c++17|
std::pair<iterator,bool> insert( value_type&& value );
+
std::pair<iterator, bool> insert( value_type&& value );
 
}}
 
}}
{{dcl rev begin | num=4}}
+
{{dcl rev multi|num=4|until1=c++11|
{{dcl | until=c++11 |
+
|dcl1=
iterator insert( iterator hint, const value_type& value );
+
iterator insert( iterator pos, const value_type& value );
 +
|dcl2=
 +
iterator insert( const_iterator pos, const value_type& value );
 
}}
 
}}
{{dcl | since=c++11 |
+
{{dcl|num=5|since=c++11|
iterator insert( const_iterator hint, const value_type& value );
+
}}
+
{{dcl rev end}}
+
{{dcl | num=5 | since=c++11 |
+
 
template< class P >
 
template< class P >
iterator insert( const_iterator hint, P&& value );
+
iterator insert( const_iterator pos, P&& value );
 
}}
 
}}
{{dcl | num=6 | since=c++17 |
+
{{dcl|num=6|since=c++17|
iterator insert( const_iterator hint, value_type&& value );
+
iterator insert( const_iterator pos, value_type&& value );
 
}}
 
}}
{{dcl | num=7 |  
+
{{dcl|num=7|
 
template< class InputIt >
 
template< class InputIt >
 
void insert( InputIt first, InputIt last );
 
void insert( InputIt first, InputIt last );
 
}}
 
}}
{{dcl | num=8 | since=c++11 |
+
{{dcl|num=8|since=c++11|
 
void insert( std::initializer_list<value_type> ilist );
 
void insert( std::initializer_list<value_type> ilist );
 
}}
 
}}
{{dcl | num=9 | since=c++17 |
+
{{dcl|num=9|since=c++17|
insert_return_type insert(node_type&& nh);
+
insert_return_type insert( node_type&& nh );
 
}}
 
}}
{{dcl | num=10 | since=c++17 |
+
{{dcl|num=10|since=c++17|
iterator insert(const_iterator hint, node_type&& nh);
+
iterator insert( const_iterator pos, node_type&& nh );
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
Line 44: Line 42:
 
Inserts element(s) into the container, if the container doesn't already contain an element with an equivalent key.
 
Inserts element(s) into the container, if the container doesn't already contain an element with an equivalent key.
  
@1-3@ Inserts {{tt|value}}. The overload {{v|2}} is equivalent to {{c|emplace(std::forward<P>(value))}} and only participates in overload resolution if {{c|std::is_constructible<value_type, P&&>::value {{==}} true}}.
+
@1-3@ Inserts {{c|value}}.
 +
@@ Overload {{v|2}} is equivalent to {{c|emplace(std::forward<P>(value))}} and only participates in overload resolution if {{c|1=std::is_constructible<value_type, P&&>::value == true}}.
  
@4-6@ Inserts {{tt|value}} in the position as close as possible, just prior{{mark since c++11}}, to {{tt|hint}}. The overload {{v|5}} is equivalent to {{c|emplace_hint(hint, std::forward<P>(value))}} and only participates in overload resolution if {{c|std::is_constructible<value_type, P&&>::value {{==}} true}}.
+
@4-6@ Inserts {{c|value}} in the position as close as possible to the position just prior to {{c|pos}}.
 +
@@ Overload {{v|5}} is equivalent to {{c|emplace_hint(hint, std::forward<P>(value))}} and only participates in overload resolution if {{c|1=std::is_constructible<value_type, P&&>::value == true}}.
  
@7@ Inserts elements from range {{tt|[first, last)}}. {{cpp/container/lwg2488-note}}
+
@7@ Inserts elements from range {{range|first|last}}. {{cpp/container/lwg2488-note}}
  
@8@ Inserts elements from initializer list {{tt|ilist}}. {{cpp/container/lwg2488-note}}
+
@8@ Inserts elements from initializer list {{c|ilist}}. {{cpp/container/lwg2488-note}}
  
{{cpp/container/node_handle_insert_desc|map|9|10}}
+
{{cpp/container/node handle insert desc|map|9|10}}
  
{{cpp/container/note_iterator_invalidation|map|insert}}
+
{{cpp/container/note iterator invalidation|map|insert}}
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | hint | {{rev begin}}{{rev|until=c++11|iterator, used as a suggestion as to where to start the search}}{{rev|since=c++11|iterator to the position before which the new element will be inserted}}{{rev end}}}}
+
{{par|pos|iterator to the position before which the new element will be inserted}}
{{par | value | element value to insert}}
+
{{par|value|element value to insert}}
{{par | first, last | range of elements to insert}}
+
{{par|first, last|range of elements to insert}}
{{par | ilist | initializer list to insert the values from}}
+
{{par|ilist|initializer list to insert the values from}}
{{par | nh | a compatible [[cpp/container/node_handle|node handle]]}}
+
{{par|nh|a compatible [[cpp/container/node_handle|node handle]]}}
 
{{par hreq}}
 
{{par hreq}}
{{par req named | InputIt | InputIterator}}
+
{{par req named|InputIt|InputIterator}}
{{par end}}  
+
{{par end}}
  
 
===Return value===
 
===Return value===
@1-3@ Returns a pair consisting of an iterator to the inserted element (or to the element that prevented the insertion) and a {{c|bool}} denoting whether the insertion took place.
+
@1-3@ {{cpp/container/return iterator pair||pair}}
  
@4-6@ Returns an iterator to the inserted element, or to the element that prevented the insertion.
+
@4-6@ {{cpp/container/return iterator pair}}
  
@7-8@ (none)
+
@7,8@ (none)
  
{{cpp/container/node_handle_insert_return|map|9|10}}
+
{{cpp/container/node handle insert return|map|9|10}}
  
 
===Exceptions===
 
===Exceptions===
Line 83: Line 83:
 
@1-3@ Logarithmic in the size of the container, {{tt|O(log(size()))}}.
 
@1-3@ Logarithmic in the size of the container, {{tt|O(log(size()))}}.
  
{{rev begin}}
+
@4-6@ Amortized constant if the insertion happens in the position just {{rev inl|until=c++11|''after''}}{{rev inl|since=c++11|''before''}} {{c|pos}}, logarithmic in the size of the container otherwise.
{{rev | until=c++11 |
+
@4-6@ Amortized constant if the insertion happens in the position just ''after'' the hint, logarithmic in the size of the container otherwise.
+
}}
+
{{rev | since=c++11 |
+
@4-6@ Amortized constant if the insertion happens in the position just ''before'' the hint, logarithmic in the size of the container otherwise.
+
}}
+
{{rev end}}
+
  
@7-8@ {{tt|O(N*log(size() + N))}}, where N is the number of elements to insert.
+
@7,8@ {{tt|O(N&middot;log(size() + N))}}, where {{tt|N}} is the number of elements to insert.
  
 
@9@ Logarithmic in the size of the container, {{tt|O(log(size()))}}.
 
@9@ Logarithmic in the size of the container, {{tt|O(log(size()))}}.
  
@10@ Amortized constant if the insertion happens in the position just ''before'' the hint, logarithmic in the size of the container otherwise.
+
@10@ Amortized constant if the insertion happens in the position just ''before'' {{c|pos}}, logarithmic in the size of the container otherwise.
  
 
===Notes===
 
===Notes===
{{cpp/container/hinted_insert_note|4-6}}
+
{{cpp/container/hinted insert note|4-6}}
 +
 
 +
===Example===
 +
{{example
 +
|code=
 +
#include <iomanip>
 +
#include <iostream>
 +
#include <map>
 +
#include <string>
 +
using namespace std::literals;
 +
 
 +
template<typename It>
 +
void print_insertion_status(It it, bool success)
 +
{
 +
    std::cout << "Insertion of " << it->first
 +
              << (success ? " succeeded\n" : " failed\n");
 +
}
 +
 
 +
int main()
 +
{
 +
    std::map<std::string, float> heights;
 +
   
 +
    // Overload 3: insert from rvalue reference
 +
    const auto [it_hinata, success] = heights.insert({"Hinata"s, 162.8});
 +
    print_insertion_status(it_hinata, success);
 +
   
 +
    {
 +
        // Overload 1: insert from lvalue reference
 +
        const auto [it, success2] = heights.insert(*it_hinata);
 +
        print_insertion_status(it, success2);
 +
    }
 +
    {
 +
        // Overload 2: insert via forwarding to emplace
 +
        const auto [it, success] = heights.insert(std::pair{"Kageyama", 180.6});
 +
        print_insertion_status(it, success);
 +
    }
 +
    {
 +
        // Overload 6: insert from rvalue reference with positional hint
 +
        const std::size_t n = std::size(heights);
 +
        const auto it = heights.insert(it_hinata, {"Azumane"s, 184.7});
 +
        print_insertion_status(it, std::size(heights) != n);
 +
    }
 +
    {
 +
        // Overload 4: insert from lvalue reference with positional hint
 +
        const std::size_t n = std::size(heights);
 +
        const auto it = heights.insert(it_hinata, *it_hinata);
 +
        print_insertion_status(it, std::size(heights) != n);
 +
    }
 +
    {
 +
        // Overload 5: insert via forwarding to emplace with positional hint
 +
        const std::size_t n = std::size(heights);
 +
        const auto it = heights.insert(it_hinata, std::pair{"Tsukishima", 188.3});
 +
        print_insertion_status(it, std::size(heights) != n);
 +
    }
 +
   
 +
    auto node_hinata = heights.extract(it_hinata);
 +
    std::map<std::string, float> heights2;
 +
   
 +
    // Overload 7: insert from iterator range
 +
    heights2.insert(std::begin(heights), std::end(heights));
 +
   
 +
    // Overload 8: insert from initializer_list
 +
    heights2.insert({{"Kozume"s, 169.2}, {"Kuroo", 187.7}});
 +
 
 +
    // Overload 9: insert node
 +
    const auto status = heights2.insert(std::move(node_hinata));
 +
    print_insertion_status(status.position, status.inserted);
 +
   
 +
    node_hinata = heights2.extract(status.position);
 +
    {
 +
        // Overload 10: insert node with positional hint
 +
        const std::size_t n = std::size(heights2);
 +
        const auto it = heights2.insert(std::begin(heights2), std::move(node_hinata));
 +
        print_insertion_status(it, std::size(heights2) != n);
 +
    }
 +
 
 +
    // Print resulting map
 +
    std::cout << std::left << '\n';
 +
    for (const auto& [name, height] : heights2)
 +
        std::cout << std::setw(10) << name << " {{!}} " << height << "cm\n";
 +
}
 +
|output=
 +
Insertion of Hinata succeeded
 +
Insertion of Hinata failed
 +
Insertion of Kageyama succeeded
 +
Insertion of Azumane succeeded
 +
Insertion of Hinata failed
 +
Insertion of Tsukishima succeeded
 +
Insertion of Hinata succeeded
 +
Insertion of Hinata succeeded
 +
 
 +
Azumane    {{!}} 184.7cm
 +
Hinata    {{!}} 162.8cm
 +
Kageyama  {{!}} 180.6cm
 +
Kozume    {{!}} 169.2cm
 +
Kuroo      {{!}} 187.7cm
 +
Tsukishima {{!}} 188.3cm
 +
}}
 +
 
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=233|std=C++98|before={{c|pos}} was just a hint, it could be totally ignored|after=the insertion is required to<br>be as close as possible to the<br>position just prior to {{c|pos}}}}
 +
{{dr list item|wg=lwg|dr=264|std=C++98|before=the complexity of overload {{v|7}} was required to be linear if<br>the range {{range|first|last}} is sorted according to {{tt|Compare}}|after=removed the linear requirement<br>in this special case}}
 +
{{dr list item|wg=lwg|dr=316|std=C++98|before=in the return value of overload {{v|1}}, it was not specified<br>which {{c/core|bool}} value indicates a successful insertion|after=success is<br>indicated by {{c|true}}}}
 +
{{dr list item|wg=lwg|dr=2005|std=C++11|before=overloads {{v|2,5}} were poorly described|after=improved the description}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/container/dsc emplace |map}}
+
{{dsc inc|cpp/container/dsc emplace|map}}
{{dsc inc | cpp/container/dsc emplace_hint |map}}
+
{{dsc inc|cpp/container/dsc emplace_hint|map}}
{{dsc inc | cpp/container/dsc insert_or_assign |map}}
+
{{dsc inc|cpp/container/dsc insert_or_assign|map}}
 +
{{dsc inc|cpp/iterator/dsc inserter}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/container/map/insert]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/container/map/insert]]
+
[[fr:cpp/container/map/insert]]
+
[[it:cpp/container/map/insert]]
+
[[ja:cpp/container/map/insert]]
+
[[pt:cpp/container/map/insert]]
+
[[ru:cpp/container/map/insert]]
+
[[zh:cpp/container/map/insert]]
+

Latest revision as of 10:35, 15 March 2024

 
 
 
 
std::pair<iterator, bool> insert( const value_type& value );
(1)
template< class P >
std::pair<iterator, bool> insert( P&& value );
(2) (since C++11)
std::pair<iterator, bool> insert( value_type&& value );
(3) (since C++17)
(4)
iterator insert( iterator pos, const value_type& value );
(until C++11)
iterator insert( const_iterator pos, const value_type& value );
(since C++11)
template< class P >
iterator insert( const_iterator pos, P&& value );
(5) (since C++11)
iterator insert( const_iterator pos, value_type&& value );
(6) (since C++17)
template< class InputIt >
void insert( InputIt first, InputIt last );
(7)
void insert( std::initializer_list<value_type> ilist );
(8) (since C++11)
insert_return_type insert( node_type&& nh );
(9) (since C++17)
iterator insert( const_iterator pos, node_type&& nh );
(10) (since C++17)

Inserts element(s) into the container, if the container doesn't already contain an element with an equivalent key.

1-3) Inserts value.
Overload (2) is equivalent to emplace(std::forward<P>(value)) and only participates in overload resolution if std::is_constructible<value_type, P&&>::value == true.
4-6) Inserts value in the position as close as possible to the position just prior to pos.
Overload (5) is equivalent to emplace_hint(hint, std::forward<P>(value)) and only participates in overload resolution if std::is_constructible<value_type, P&&>::value == true.
7) Inserts elements from range [firstlast). If multiple elements in the range have keys that compare equivalent, it is unspecified which element is inserted (pending LWG2844).
8) Inserts elements from initializer list ilist. If multiple elements in the range have keys that compare equivalent, it is unspecified which element is inserted (pending LWG2844).
9) If nh is an empty node handle, does nothing. Otherwise, inserts the element owned by nh into the container , if the container doesn't already contain an element with a key equivalent to nh.key(). The behavior is undefined if nh is not empty and get_allocator() != nh.get_allocator().
10) If nh is an empty node handle, does nothing and returns the end iterator. Otherwise, inserts the element owned by nh into the container, if the container doesn't already contain an element with a key equivalent to nh.key(), and returns the iterator pointing to the element with key equivalent to nh.key()(regardless of whether the insert succeeded or failed). If the insertion succeeds, nh is moved from, otherwise it retains ownership of the element. The element is inserted as close as possible to the position just prior to pos. The behavior is undefined if nh is not empty and get_allocator() != nh.get_allocator().

No iterators or references are invalidated. If the insertion is successful, pointers and references to the element obtained while it is held in the node handle are invalidated, and pointers and references obtained to that element before it was extracted become valid.(since C++17)

Contents

[edit] Parameters

pos - iterator to the position before which the new element will be inserted
value - element value to insert
first, last - range of elements to insert
ilist - initializer list to insert the values from
nh - a compatible node handle
Type requirements
-
InputIt must meet the requirements of LegacyInputIterator.

[edit] Return value

1-3) A pair consisting of an iterator to the inserted element (or to the element that prevented the insertion) and a bool value set to true if and only if the insertion took place.
4-6) An iterator to the inserted element, or to the element that prevented the insertion.
7,8) (none)
9) An object of insert_return_type with the members initialized as follows:
  • If nh is empty, inserted is false, position is end(), and node is empty.
  • Otherwise if the insertion took place, inserted is true, position points to the inserted element, and node is empty.
  • If the insertion failed, inserted is false, node has the previous value of nh, and position points to an element with a key equivalent to nh.key().
10) End iterator if nh was empty, iterator pointing to the inserted element if insertion took place, and iterator pointing to an element with a key equivalent to nh.key() if it failed.

[edit] Exceptions

1-6) If an exception is thrown by any operation, the insertion has no effect.

[edit] Complexity

1-3) Logarithmic in the size of the container, O(log(size())).
4-6) Amortized constant if the insertion happens in the position just after(until C++11)before(since C++11) pos, logarithmic in the size of the container otherwise.
7,8) O(N·log(size() + N)), where N is the number of elements to insert.
9) Logarithmic in the size of the container, O(log(size())).
10) Amortized constant if the insertion happens in the position just before pos, logarithmic in the size of the container otherwise.

[edit] Notes

The hinted insert (4-6) does not return a boolean in order to be signature-compatible with positional insert on sequential containers, such as std::vector::insert. This makes it possible to create generic inserters such as std::inserter. One way to check success of a hinted insert is to compare size() before and after.

[edit] Example

#include <iomanip>
#include <iostream>
#include <map>
#include <string>
using namespace std::literals;
 
template<typename It>
void print_insertion_status(It it, bool success)
{
    std::cout << "Insertion of " << it->first
              << (success ? " succeeded\n" : " failed\n");
}
 
int main()
{
    std::map<std::string, float> heights;
 
    // Overload 3: insert from rvalue reference
    const auto [it_hinata, success] = heights.insert({"Hinata"s, 162.8});
    print_insertion_status(it_hinata, success);
 
    {
        // Overload 1: insert from lvalue reference
        const auto [it, success2] = heights.insert(*it_hinata);
        print_insertion_status(it, success2);
    }
    {
        // Overload 2: insert via forwarding to emplace
        const auto [it, success] = heights.insert(std::pair{"Kageyama", 180.6});
        print_insertion_status(it, success);
    }
    {
        // Overload 6: insert from rvalue reference with positional hint
        const std::size_t n = std::size(heights);
        const auto it = heights.insert(it_hinata, {"Azumane"s, 184.7});
        print_insertion_status(it, std::size(heights) != n);
    }
    {
        // Overload 4: insert from lvalue reference with positional hint
        const std::size_t n = std::size(heights);
        const auto it = heights.insert(it_hinata, *it_hinata);
        print_insertion_status(it, std::size(heights) != n);
    }
    {
        // Overload 5: insert via forwarding to emplace with positional hint
        const std::size_t n = std::size(heights);
        const auto it = heights.insert(it_hinata, std::pair{"Tsukishima", 188.3});
        print_insertion_status(it, std::size(heights) != n);
    }
 
    auto node_hinata = heights.extract(it_hinata);
    std::map<std::string, float> heights2;
 
    // Overload 7: insert from iterator range
    heights2.insert(std::begin(heights), std::end(heights));
 
    // Overload 8: insert from initializer_list
    heights2.insert({{"Kozume"s, 169.2}, {"Kuroo", 187.7}});
 
    // Overload 9: insert node
    const auto status = heights2.insert(std::move(node_hinata));
    print_insertion_status(status.position, status.inserted);
 
    node_hinata = heights2.extract(status.position);
    {
        // Overload 10: insert node with positional hint
        const std::size_t n = std::size(heights2);
        const auto it = heights2.insert(std::begin(heights2), std::move(node_hinata));
        print_insertion_status(it, std::size(heights2) != n);
    }
 
    // Print resulting map
    std::cout << std::left << '\n';
    for (const auto& [name, height] : heights2)
        std::cout << std::setw(10) << name << " | " << height << "cm\n";
}

Output:

Insertion of Hinata succeeded
Insertion of Hinata failed
Insertion of Kageyama succeeded
Insertion of Azumane succeeded
Insertion of Hinata failed
Insertion of Tsukishima succeeded
Insertion of Hinata succeeded
Insertion of Hinata succeeded
 
Azumane    | 184.7cm
Hinata     | 162.8cm
Kageyama   | 180.6cm
Kozume     | 169.2cm
Kuroo      | 187.7cm
Tsukishima | 188.3cm

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 233 C++98 pos was just a hint, it could be totally ignored the insertion is required to
be as close as possible to the
position just prior to pos
LWG 264 C++98 the complexity of overload (7) was required to be linear if
the range [firstlast) is sorted according to Compare
removed the linear requirement
in this special case
LWG 316 C++98 in the return value of overload (1), it was not specified
which bool value indicates a successful insertion
success is
indicated by true
LWG 2005 C++11 overloads (2,5) were poorly described improved the description

[edit] See also

(C++11)
constructs element in-place
(public member function) [edit]
constructs elements in-place using a hint
(public member function) [edit]
inserts an element or assigns to the current element if the key already exists
(public member function) [edit]
creates a std::insert_iterator of type inferred from the argument
(function template) [edit]