이 기사에서는 C++ STL에서 multimap::erase() 함수의 작동, 구문 및 예제에 대해 논의할 것입니다.
C++ STL의 멀티맵이란 무엇입니까?
멀티맵은 맵 컨테이너와 유사한 연관 컨테이너입니다. 또한 키-값과 매핑된 값의 조합으로 구성된 요소를 특정 순서로 쉽게 저장할 수 있습니다. 멀티맵 컨테이너에는 동일한 키와 연결된 여러 요소가 있을 수 있습니다. 데이터는 항상 관련 키를 사용하여 내부적으로 정렬됩니다.
멀티맵::erase()란 무엇입니까?
multimap::erase() 함수는
이 기능은 키, 위치 또는 지정된 범위로 요소를 제거하거나 지울 수 있습니다. 이 기능을 실행하면 멀티맵 컨테이너의 크기가 제거되는 요소의 수만큼 줄어듭니다.
구문
multimap_name.erase(key_type key); multimap_name.erase(const_iterator it); multimap_name.erase(const_ iterator start, const_itertaor end);
매개변수
이 함수는 다음 매개변수를 허용합니다. -
-
키 − 요소를 제거하려는 키의 위치입니다.
-
그것 − 요소를 제거하려는 반복자 위치.
-
시작, 끝 - 이것은 요소 집합이 제거되어야 하는 위치에서 어떤 위치까지의 범위를 정의합니다.
반환 값
이 함수는 연결된 컨테이너에서 제거되는 요소의 수를 반환합니다.
입력
std::multimap<int> mymap;
mymap.insert({‘a’, 10});
mymap.insert({‘b’, 20});
mymap.insert({‘c’, 30});
mymap.erase(‘b’); 출력
a:10 c:30
예시
//주어진 키 삭제
#include<iostream>
#include<map>
using namespace std;
int main(){
multimap<int,char > mul_1;
//declaring iterator to traverse the elements
multimap<int,char>:: iterator i;
//inserting elements to multimap1
mul_1.insert(make_pair(0,'a'));
mul_1.insert(make_pair(1,'b'));
mul_1.insert(make_pair(2,'c'));
mul_1.insert(make_pair(3,'d'));
mul_1.insert(make_pair(4,'e'));
mul_1.insert(make_pair(5,'f'));
mul_1.insert(make_pair(6,'g'));
//calling erase() to delete the element
mul_1.erase(1);
mul_1.erase(4);
mul_1.erase(6);
mul_1.erase(5);
//elements of multimap1
cout<<"Elements in multimap1 are: "<<"\n";
for( i = mul_1.begin(); i!= mul_1.end(); i++){
cout<<(*i).first<<" "<< (*i).second << "\n";
}
} 출력
위의 코드를 실행하면 다음 출력이 생성됩니다 -
Elements in multimap1 are: 0 a 2 c 3 d
예시
//주어진 범위 내 요소 지우기
#include<iostream>
#include<map>
using namespace std;
int main(){
multimap<int,char > mul_1;
//declaring iterator to traverse the elements
multimap<int,char>:: iterator i;
//inserting elements to multimap1
mul_1.insert(make_pair(0,'a'));
mul_1.insert(make_pair(1,'b'));
mul_1.insert(make_pair(2,'c'));
mul_1.insert(make_pair(3,'d'));
mul_1.insert(make_pair(4,'e'));
mul_1.insert(make_pair(5,'f'));
mul_1.insert(make_pair(6,'g'));
//elements in multimap before erasing
cout<<"Elements in multimap1 are: "<<"\n";
for( i = mul_1.begin(); i!= mul_1.end(); i++){
cout<<(*i).first<<" "<< (*i).second << "\n";
}
//calling erase() to delete the element
auto start = mul_1.find(3);
auto end = mul_1.find(6);
mul_1.erase(start, end);
//elements of multimap1 after erasing
cout<<"Elements in multimap1 are: "<<"\n";
for( i = mul_1.begin(); i!= mul_1.end(); i++){
cout<<(*i).first<<" "<< (*i).second << "\n";
}
} 출력
위의 코드를 실행하면 다음 출력이 생성됩니다 -
Elements in multimap1 are: 0 a 1 b 2 c 3 d 4 e 5 f 6 g Elements in multimap1 are: 0 a 1 b 2 c 6 g
예시
//주어진 위치에서 요소 지우기
#include<iostream>
#include<map>
using namespace std;
int main(){
multimap<int,char > mul_1;
//declaring iterator to traverse the elements
multimap<int,char>:: iterator i;
//inserting elements to multimap1
mul_1.insert(make_pair(0,'a'));
mul_1.insert(make_pair(1,'b'));
mul_1.insert(make_pair(2,'c'));
mul_1.insert(make_pair(3,'d'));
mul_1.insert(make_pair(4,'e'));
mul_1.insert(make_pair(5,'f'));
mul_1.insert(make_pair(6,'g'));
//elements in multimap before erasing
cout<<"Elements in multimap1 are: "<<"\n";
for( i = mul_1.begin(); i!= mul_1.end(); i++){
cout<<(*i).first<<" "<< (*i).second << "\n";
}
//calling erase() to delete the element
auto first = mul_1.find(1);
mul_1.erase(first);
auto second = mul_1.find(6);
mul_1.erase(second);
auto third = mul_1.find(2);
mul_1.erase(third);
//elements of multimap1 after erasing
cout<<"Elements in multimap1 are: "<<"\n";
for( i = mul_1.begin(); i!= mul_1.end(); i++){
cout<<(*i).first<<" "<< (*i).second << "\n";
}
} 출력
위의 코드를 실행하면 다음 출력이 생성됩니다 -
Elements in multimap1 are: 0 a 1 b 2 c 3 d 4 e 5 f 6 g Elements in multimap1 are: 0 a 3 d 4 e 5 f