map::erase
지도에서 지정 된 위치에서 요소 또는 요소 범위를 제거 하거나 지정 된 키와 일치 하는 요소를 제거 합니다.
iterator erase(
iterator _Where
);
iterator erase(
iterator _First,
iterator _Last
);
size_type erase(
const key_type& _Key
);
매개 변수
_Where
지도에서 제거할 요소의 위치입니다._First
지도에서 제거할 첫 번째 요소의 위치입니다._Last
마지막 요소 바로 뒤에 위치에서 제거 합니다._Key
지도에서 제거할 요소의 키 값입니다.
반환 값
첫 번째 두 명의 멤버 함수를 양방향 반복기 이러한 요소가 있으면 모든 요소를 제거, 또는 지도 끝 포인터 외 남은 첫 번째 요소를 지정 합니다.
[!참고]
이 반환 형식이 C++ 표준에 맞지 않습니다.
멤버 함수에 대 한 세 번째 지도에서 제거 된 요소 수를 반환 합니다.
설명
경우에 따라서는이 메서드가 throw 할 수 있습니다는 out_of_range 예외입니다.
예제
이 예제를 컴파일하는 경우는 /Wp64 플래그를 지정 하거나 컴파일러 C4267 경고는 64 비트 플랫폼에서 생성 됩니다.이 경고에 대 한 자세한 내용은 컴파일러 경고 (수준 3) C4267.
// 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;
}
요구 사항
헤더: <map>
네임 스페이스: std