English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C ++ map delete()함수는 주어진 키와 연관된 단일 요소 또는 요소 범위를 맵 컨테이너에서 제거하는 데 사용됩니다. 따라서 요소의 수를 통해 크기를 줄입니다.
void erase(iterator position); // C++ 11 전에 size_type erase(const key_type& k); // C++ 11 전에 void erase(iterator first, iterator last); // C++ 11 전에 iterator erase(const_iterator position); //C++ 11 시작 size_type erase(const key_type& k); //C++ 11 시작 iterator erase(const_iterator first, const_iterator last); //C++ 11 시작
position:맵에서 제거하고자 하는 단일 요소를 가리키는 이터레이터.
k:맵에서 제거하고자 하는 요소의 키.
first:지우고자 하는 범위의 시작.
last:지우고자 하는 범위의 끝.
지워진 요소의 다음 요소를 가리키는 이터레이터를 반환하거나, 지워진 요소의 수를 반환합니다.
이제 간단한 예제를 보여드리겠습니다. 이터레이터를 통해 요소를 지우는 것.
#include <iostream> #include <map> #include <string> using namespace std; int main () { map<char,int> mymap; map<char,int>::iterator it; mymap['a']=10; mymap['b']=20; mymap['c']=30; mymap['d']=40; cout<<"요소를 제거하기 전: \n"; for (it=mymap.begin(); it!=mymap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; it=mymap.find('b'); mymap.erase (it); // erasing by iterator cout<<"\n요소를 제거한 후: \n"; for (it=mymap.begin(); it!=mymap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; return 0; }
출력:
요소를 제거하기 전: a => 10 b => 20 c => 30 d => 40 요소를 제거한 후: a => 10 c => 30 d => 40
위의 예제에서 요소가 이터레이터로 지워졌습니다。
주어진 키 값으로 map의 요소를 지우는 간단한 예제를 보겠습니다。
#include <iostream> #include <map> #include <string> using namespace std; int main () { map<char,int> mymap; map<char,int>::iterator it; mymap['a']=10; mymap['b']=20; mymap['c']=30; mymap['d']=40; cout<<"요소를 제거하기 전: \n"; for (it=mymap.begin(); it!=mymap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; mymap.erase ('c'); // erasing by key cout<<"\n요소를 제거한 후: \n"; for (it=mymap.begin(); it!=mymap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; return 0; }
출력:
요소를 제거하기 전: a => 10 b => 20 c => 30 d => 40 요소를 제거한 후: a => 10 b => 20 d => 40
위의 예제에서 delete(key) 함수를 사용하여 키 'c' 및 그 매핑된 값으로 지우는 방법을 사용했습니다。
주어진 범위로 요소를 지우는 간단한 예제를 보겠습니다。
#include <iostream> #include <map> #include <string> using namespace std; int main () { map<char,int> mymap; map<char,int>::iterator it; mymap['a']=10; mymap['b']=20; mymap['c']=30; mymap['d']=40; cout<<"요소를 제거하기 전: \n"; cout<<"Size 크기: "<<mymap.size()<<'\n'; for (it=mymap.begin(); it!=mymap.end(); ++it) cout << it->first << " => " << it->second << '\n'; mymap.erase ( mymap.begin () , mymap.end () ); // erasing by range cout<<"\n요소를 제거한 후: \n"; cout<<"Size 크기: "<<mymap.size(); for (it=mymap.begin(); it!=mymap.end(); ++it) cout << it->first << " => " << it->second << '\n'; return 0; }
출력:
요소를 제거하기 전: Size is: 4 a => 10 b => 20 c => 30 d => 40 요소를 제거한 후: Size is: 0
위의 예제에서 delete(first, last) 함수를 사용하여 주어진 범위(즉 시작부터 끝까지)의 요소를 지우는 방법을 사용했습니다。
map에서 모든奇수를 제거하는 간단한 예제를 보겠습니다。
#include <map> #include <iostream> using namespace std; int main() { map<int, string> m = {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"}, {6, "six"}}; // m에서 모든奇数을 제거합니다 cout<<"홀수를 제거한 후, 요소는:\n "; for(auto it = m.begin(); it != m.end(); ) if(it->first % 2 == 1) it = m.erase(it); else ++it; for(auto& p : m) cout << p.second << ", "; }
출력:
홀수를 제거한 후, 요소는: two, four, six,
위의 예제에서 모든奇수가 제거되고, 짝수가 표시되었습니다.