当前位置:网站首页>Std:: map insert details

Std:: map insert details

2022-06-13 05:01:00 Snow * sleet * snow

std::map<Key,T,Compare,Allocator>::insert

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 hint, const value_type& value );

(until C++11)

iterator insert( const_iterator hint, const value_type& value );

(since C++11)

template< class P >
iterator insert( const_iterator hint, P&& value );

(5)(since C++11)

iterator insert( const_iterator hint, 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 hint, 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.

Insert an element into the container , This element does not exist in the container before insertion . If the element already exists in the container, the insertion fails . Be careful 1-3 The return value of the overloaded version is a pair

std::pair<iterator, bool>

  The return value is an iterator and a Boolean value pair. You can see this through the Boolean value insert Successful operation .

#include <iomanip>
#include <iostream>
#include <map>
#include <string>
 
using namespace std::literals;
 
template<typename It>
void printInsertionStatus(It it, bool success)
{
    std::cout << "Insertion of " << it->first << (success ? " succeeded\n" : " failed\n");
}
 
int main()
{
    std::map<std::string, float> karasunoPlayerHeights;
 
    // Overload 3: insert from rvalue reference
    // heavy load 3: Insert right value reference ,{"Hinata"s, 162.8} Is a temporary variable 
    const auto [it_hinata, success] = karasunoPlayerHeights.insert({"Hinata"s, 162.8});
    printInsertionStatus(it_hinata, success);
 
    {
        // Overload 1: insert from lvalue reference
        // heavy load 1: Insert lvalue reference , In this step it_hinata Is the return value of the last insert , So this insertion will return a failure 
        const auto [it, success2] = karasunoPlayerHeights.insert(*it_hinata);
        printInsertionStatus(it, success2);
    }
    {
        // Overload 2: insert via forwarding to emplace
        // heavy load 2:
        const auto [it, success] = karasunoPlayerHeights.insert({"Kageyama", 180.6});
        printInsertionStatus(it, success);
    }
 
    {
        // Overload 6: insert from rvalue reference with positional hint
        // heavy load 6: Away from hint Insert an R-value as close as possible 
        const std::size_t n = std::size(karasunoPlayerHeights);
        const auto it = karasunoPlayerHeights.insert(it_hinata, {"Azumane"s, 184.7});
        printInsertionStatus(it, std::size(karasunoPlayerHeights) != n);
    }
    {
        // Overload 4: insert from lvalue reference with positional hint
        // heavy load 4: stay hint Insert an lvalue at the relevant position 
        const std::size_t n = std::size(karasunoPlayerHeights);
        const auto it = karasunoPlayerHeights.insert(it_hinata, *it_hinata);
        printInsertionStatus(it, std::size(karasunoPlayerHeights) != n);
    }
    {
        // Overload 5: insert via forwarding to emplace with positional hint
        const std::size_t n = std::size(karasunoPlayerHeights);
        const auto it = karasunoPlayerHeights.insert(it_hinata, {"Tsukishima", 188.3});
        printInsertionStatus(it, std::size(karasunoPlayerHeights) != n);
    }
 
    auto node_hinata = karasunoPlayerHeights.extract(it_hinata);
    std::map<std::string, float> playerHeights;
 
    // Overload 7: insert from iterator range
    // heavy load 7, Iterator range insert 
    playerHeights.insert(std::begin(karasunoPlayerHeights), std::end(karasunoPlayerHeights));
 
    // Overload 8: insert from initializer_list
    // Initialize list insert 
    playerHeights.insert({
   {"Kozume"s, 169.2}, {"Kuroo", 187.7}});
 
 
    // Overload 9: insert node
    // heavy load 9, Node insertion 
    const auto status = playerHeights.insert(std::move(node_hinata));
    printInsertionStatus(status.position, status.inserted);
 
    node_hinata = playerHeights.extract(status.position);
    {
        // Overload 10: insert node with positional hint
        const std::size_t n = std::size(playerHeights);
        const auto it = playerHeights.insert(std::begin(playerHeights), std::move(node_hinata));
        printInsertionStatus(it, std::size(playerHeights) != n);
    }
 
 
    // Print resulting map
    std::cout << std::left << '\n';
    for (const auto& [name, height] : playerHeights)
        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
原网站

版权声明
本文为[Snow * sleet * snow]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280516196918.html