map::erase
Odstraní prvek nebo prvky oblast v mapě od zadané pozice nebo odebere prvky, které odpovídají zadaným klíčem.
iterator erase(
iterator _Where
);
iterator erase(
iterator _First,
iterator _Last
);
size_type erase(
const key_type& _Key
);
Parametry
_Where
Umístění prvku odebrat z mapy._First
Pozice prvního prvku odebrán z mapy._Last
Za poslední prvek pozice odebrán z mapy._Key
Hodnota klíče prvky odebrány z mapy.
Vrácená hodnota
Pro první dva členské funkce obousměrný iterace, označí první prvek zbývající za jakékoliv prvky odstraněny nebo ukazatel na konec mapy, pokud neexistuje žádný takový prvek.
[!POZNÁMKA]
Tento návratový typ nevyhovuje standardu jazyka C++.
Třetí členské funkce vrátí počet prvků, které byly odebrány z mapy.
Poznámky
V některých případech tato metoda může být vyvolána out_of_range výjimku.
Příklad
Při kompilování v tomto příkladu se /Wp64 příznak nebo na 64bitové platformě budou generovány kompilátoru upozornění C4267.Další informace o tomto upozornění naleznete v C4267 (úroveň 3) upozornění kompilátoru.
// map_erase.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main()
{
using namespace std;
map<int, int> m1, m2, m3;
map<int, int>::iterator pIter, Iter1, Iter2;
int i;
map<int, int>::size_type n;
typedef pair<int, int> Int_Pair;
for (i = 1; i < 5; i++)
{
m1.insert(Int_Pair(i, i));
m2.insert(Int_Pair(i, i*i));
m3.insert(Int_Pair(i, i-1));
}
// The 1st member function removes an element at a given position
Iter1 = ++m1.begin();
m1.erase(Iter1);
cout << "After the 2nd element is deleted, the map m1 is:";
for (pIter = m1.begin(); pIter != m1.end(); pIter++)
cout << " " << pIter->second;
cout << "." << endl;
// The 2nd member function removes elements
// in the range [_First, _Last)
Iter1 = ++m2.begin();
Iter2 = --m2.end();
m2.erase(Iter1, Iter2);
cout << "After the middle two elements are deleted,"
<< " the map m2 is:";
for (pIter = m2.begin(); pIter != m2.end(); pIter++)
cout << " " << pIter->second;
cout << "." << endl;
// The 3rd member function removes elements with a given _Key
n = m3.erase(2);
cout << "After the element with a key of 2 is deleted,\n"
<< "the map m3 is:";
for (pIter = m3.begin(); pIter != m3.end(); pIter++)
cout << " " << pIter->second;
cout << "." << endl;
// The 3rd member function returns the number of elements removed
cout << "The number of elements removed from m3 is: "
<< n << "." << endl;
// The dereferenced iterator can also be used to specify a key
Iter1 = ++m3.begin();
m3.erase(Iter1);
cout << "After another element with a key equal to that"
<< endl;
cout << "of the 2nd element is deleted, "
<< "the map m3 is:";
for (pIter = m3.begin(); pIter != m3.end(); pIter++)
cout << " " << pIter->second;
cout << "." << endl;
}
Požadavky
Záhlaví: <map>
Obor názvů: std