map::insert
Wstawia element lub zakres elementów do mapy.
// (1) single element pair<iterator, bool> insert( const value_type& Val ); // (2) single element, perfect forwarded template<class ValTy> pair<iterator, bool> insert( ValTy&& Val ); // (3) single element with hint iterator insert( const_iterator Where, const value_type& Val ); // (4) single element, perfect forwarded, with hint template<class ValTy> iterator insert( const_iterator Where, ValTy&& Val ); // (5) range template<class InputIterator> void insert( InputIterator First, InputIterator Last ); // (6) initializer list void insert( initializer_list<value_type> IList );
Parametry
Parametr |
Opis |
Val |
Wartość elementu ma zostać wstawiony do mapy, chyba że zawiera już element ekwiwalentnie porządkowania której klucz. |
Where |
Miejsce, aby rozpocząć wyszukiwanie poprawne punkt wstawienia.(Jeśli tego punktu bezpośrednio przed Where, wstawiania może wystąpić w amortyzowanego czasu stała zamiast czasu logarytmicznej.) |
ValTy |
Parametr szablonu, który określa typ argumentu mapy służący do konstruowania elementu value_typei doskonałą przodu Val jako argumentu. |
First |
Pozycja pierwszego elementu do skopiowania. |
Last |
Pozycja poza ostatni element do skopiowania. |
InputIterator |
Argument funkcji szablonu, który spełnia wymagania input sterująca wskazujące elementy typu, który może służyć do konstruowania value_type obiektów. |
IList |
Initializer_list z której mają być kopiowane elementy. |
Wartość zwracana
Funkcje pojedynczy element członkowski (1) i (2) zwraca para którego bool składnik jest wartość true, jeśli wstawienie został utworzony, a wartość false, jeśli mapy zawiera już element, którego klucz miał wartość równoważną w kolejności.Składnik sterująca pary wartość zwracaną wskazuje nowo wstawiony element Jeśli bool składnik jest wartość PRAWDA lub do istniejącego elementu Jeśli bool składnik ma wartość false.
Funkcje elementu członkowskiego pojedynczego elementu z wskazówkę, [3] i [4], zwracać iterację wskazujące położenie nowego elementu została umieszczona w planie lub, jeśli element z kluczem równoważne już istnieje, do istniejącego elementu.
Uwagi
Nie Iteratory, wskaźniki lub odwołania są unieważnienie tej funkcji.
Podczas wstawiania tylko jednego elementu jeśli wyjątek stanu kontenera nie jest modyfikowany.Podczas wstawiania wiele elementów jeśli wyjątek kontenera pozostanie nieokreślony, ale nieprawidłowy stan.
To access the iterator component of a pairpr that's returned by the single-element member functions, use pr.first; to dereference the iterator within the returned pair, use *pr.first, giving you an element.Aby dostęp bool składników, użyj pr.second.Na przykład zobacz przykładowego kodu w tym artykule.
Value_type kontenera jest typedef, którego należy do kontenera i mapy, map<K, V>::value_type jest pair<const K, V>.Wartość elementu jest uporządkowaną pary, w którym pierwszy składnik jest taki sam, jak wartość klucza, a drugi składnik jest taki sam, jak wartość elementu danych.
Funkcja elementu członkowskiego zakresu [5] wstawia sekwencję wartości elementów do mapy, umożliwiająca każdy element zlikwidowane poprzez iterację w zakresie [First, Last); w związku z tym Last nie uzyskać wstawiony.Funkcja elementu członkowskiego kontenera end() odwołuje się do pozycji po ostatnim elemencie w kontenerze — na przykład instrukcji m.insert(v.begin(), v.end()); podejmuje próbę wstawienia wszystkie elementy v do m.Dodaje są tylko elementy, które mają unikatowe wartości z zakresu; zduplikowane nazwy są ignorowane.Do przestrzegania, elementy, które zostały odrzucone, użyj wersji pojedynczy element insert.
(6) jest używana funkcja inicjatora członka listy initializer_list można skopiować elementów do mapy.
Do wstawienia elementu skonstruowane w miejscu, oznacza to, że są wykonywane żadnych operacji kopiowania i przenoszenia — zobacz map::emplace i map::emplace_hint.
Przykład
// map_insert.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
#include <string>
#include <vector>
#include <utility> // make_pair()
using namespace std;
template <typename M> void print(const M& m) {
cout << m.size() << " elements: ";
for (const auto& p : m) {
cout << "(" << p.first << ", " << p.second << ") ";
}
cout << endl;
}
int main()
{
// insert single values
map<int, int> m1;
// call insert(const value_type&) version
m1.insert({ 1, 10 });
// call insert(ValTy&&) version
m1.insert(make_pair(2, 20));
cout << "The original key and mapped values of m1 are:" << endl;
print(m1);
// intentionally attempt a duplicate, single element
auto ret = m1.insert(make_pair(1, 111));
if (!ret.second){
auto pr = *ret.first;
cout << "Insert failed, element with key value 1 already exists."
<< endl << " The existing element is (" << pr.first << ", " << pr.second << ")"
<< endl;
}
else{
cout << "The modified key and mapped values of m1 are:" << endl;
print(m1);
}
cout << endl;
// single element, with hint
m1.insert(m1.end(), make_pair(3, 30));
cout << "The modified key and mapped values of m1 are:" << endl;
print(m1);
cout << endl;
// The templatized version inserting a jumbled range
map<int, int> m2;
vector<pair<int, int>> v;
v.push_back(make_pair(43, 294));
v.push_back(make_pair(41, 262));
v.push_back(make_pair(45, 330));
v.push_back(make_pair(42, 277));
v.push_back(make_pair(44, 311));
cout << "Inserting the following vector data into m2:" << endl;
print(v);
m2.insert(v.begin(), v.end());
cout << "The modified key and mapped values of m2 are:" << endl;
print(m2);
cout << endl;
// The templatized versions move-constructing elements
map<int, string> m3;
pair<int, string> ip1(475, "blue"), ip2(510, "green");
// single element
m3.insert(move(ip1));
cout << "After the first move insertion, m3 contains:" << endl;
print(m3);
// single element with hint
m3.insert(m3.end(), move(ip2));
cout << "After the second move insertion, m3 contains:" << endl;
print(m3);
cout << endl;
map<int, int> m4;
// Insert the elements from an initializer_list
m4.insert({ { 4, 44 }, { 2, 22 }, { 3, 33 }, { 1, 11 }, { 5, 55 } });
cout << "After initializer_list insertion, m4 contains:" << endl;
print(m4);
cout << endl;
}
Dane wyjściowe
The original key and mapped values of m1 are:
2 elements: (1, 10) (2, 20)
Insert failed, element with key value 1 already exists.
The existing element is (1, 10)
The modified key and mapped values of m1 are:
3 elements: (1, 10) (2, 20) (3, 30)
Inserting the following vector data into m2:
5 elements: (43, 294) (41, 262) (45, 330) (42, 277) (44, 311)
The modified key and mapped values of m2 are:
5 elements: (41, 262) (42, 277) (43, 294) (44, 311) (45, 330)
After the first move insertion, m3 contains:
1 elements: (475, blue)
After the second move insertion, m3 contains:
2 elements: (475, blue) (510, green)
After initializer_list insertion, m4 contains:
5 elements: (1, 11) (2, 22) (3, 33) (4, 44) (5, 55)
Wymagania
Nagłówek: < map >
Przestrzeń nazw: std