当前位置:网站首页>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 > | (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 > | (5) | (since C++11) |
iterator insert( const_iterator hint, value_type&& value ); | (6) | (since C++17) |
template< class InputIt > | (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
边栏推荐
- Wang Dao Chapter II linear table exercises
- Ruoyi cloud startup tutorial (hand-held graphics)
- Flutter dart variables and constants
- Introduction to QT XML
- Shell built-in string substitution
- C language learning log 10.10
- Sampo Lock
- Section 8 - Practical commissioning techniques
- Win8.1和Win10各自的優勢
- C language learning log 1.2
猜你喜欢
Advanced C - Section 2 - pointers
Chapter 15 mechanism: Address Translation
The games that you've tasted
What is the difference between ROM, ram and flash? SRAM、DRAM、PROM、EPROM、EEPROM
MySQL8.0.13安装教程(有图)
Spread your wings and soar
Win8.1和Win10各自的优势
Design system based on MVC using javeswingjdbc
Section 2 - branch and loop statements
Shell built-in string substitution
随机推荐
String()和toString()方法得区别
Recursion and recursion
Chapter 13 abstraction: address space
Clause 32: moving objects into closures using initialization capture objects
Brick story
Luogu p1036 number selection
Explain the differences and usage scenarios between created and mounted
QT signal is automatically associated with the slot
All blog navigation
Hidden implementation and decoupling, knowing Pimpl mode
On switch() case statement in C language
C language learning log 12.5
C language learning log 10.10
Optocoupler working principle function electric parameter application circuit
UNO
CMB written test graphical reasoning
shell变量学习笔记
Trust programming - linked lists: use struct to implement linked lists, use heap to merge K ascending linked lists, and customize display
C language learning log 2.6
Shell variable learning notes