Trong bài viết này, chúng ta sẽ thảo luận về cách hoạt động, cú pháp và các ví dụ của hàm multimap::erase() trong C++ STL.
Multimap trong C++ STL là gì?
Multimap là container liên kết, tương tự như map. Nó lưu trữ các phần tử dạng cặp key-value theo thứ tự cụ thể. Multimap cho phép nhiều phần tử có cùng một key. Dữ liệu luôn được sắp xếp theo key.
Hàm multimap::erase() là gì?
multimap::erase() là hàm built-in trong C++ STL, được định nghĩa trong header <map>. Hàm này xóa phần tử khỏi multimap container.
Hàm có thể xóa phần tử bằng key, vị trí (iterator), hoặc phạm vi. Khi gọi, kích thước multimap giảm đi số phần tử bị xóa.
Cú pháp
// Xóa theo key
multimap_name.erase(key_type key);
// Xóa theo iterator
multimap_name.erase(const_iterator it);
// Xóa theo phạm vi
multimap_name.erase(const_iterator start, const_iterator end);
Tham số
- key – Key của phần tử cần xóa.
- it – Vị trí iterator cần xóa.
- start, end – Phạm vi [start, end) các phần tử cần xóa.
Giá trị trả về
Trả về số lượng phần tử bị xóa (đối với xóa theo key), hoặc iterator tiếp theo (đối với xóa theo iterator/phạm vi).
Ví dụ 1: Xóa theo key
#include <iostream>
#include <map>
using namespace std;
int main() {
multimap<int, char> mul_1;
multimap<int, char>::iterator i;
// Chèn phần tử
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'));
// Xóa các key
mul_1.erase(1);
mul_1.erase(4);
mul_1.erase(6);
mul_1.erase(5);
cout << "Phần tử còn lại trong multimap1:\n";
for (i = mul_1.begin(); i != mul_1.end(); i++) {
cout << (*i).first << " " << (*i).second << "\n";
}
return 0;
}
Kết quả
Phần tử còn lại trong multimap1:
0 a
2 c
3 d
Ví dụ 2: Xóa theo phạm vi
#include <iostream>
#include <map>
using namespace std;
int main() {
multimap<int, char> mul_1;
multimap<int, char>::iterator i;
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'));
cout << "Trước khi xóa:\n";
for (i = mul_1.begin(); i != mul_1.end(); i++) {
cout << (*i).first << " " << (*i).second << "\n";
}
// Xóa phạm vi từ key 3 đến key 6 (không bao gồm 6)
auto start = mul_1.find(3);
auto end = mul_1.find(6);
mul_1.erase(start, end);
cout << "\nSau khi xóa:\n";
for (i = mul_1.begin(); i != mul_1.end(); i++) {
cout << (*i).first << " " << (*i).second << "\n";
}
return 0;
}
Kết quả
Trước khi xóa:
0 a
1 b
2 c
3 d
4 e
5 f
6 g
Sau khi xóa:
0 a
1 b
2 c
6 g
Ví dụ 3: Xóa tại vị trí iterator
#include <iostream>
#include <map>
using namespace std;
int main() {
multimap<int, char> mul_1;
multimap<int, char>::iterator i;
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'));
cout << "Trước khi xóa:\n";
for (i = mul_1.begin(); i != mul_1.end(); i++) {
cout << (*i).first << " " << (*i).second << "\n";
}
// Xóa tại vị trí cụ thể
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);
cout << "\nSau khi xóa:\n";
for (i = mul_1.begin(); i != mul_1.end(); i++) {
cout << (*i).first << " " << (*i).second << "\n";
}
return 0;
}
Kết quả
Trước khi xóa:
0 a
1 b
2 c
3 d
4 e
5 f
6 g
Sau khi xóa:
0 a
3 d
4 e
5 f