unordered_multimap::erase()是C++ STL中的内置函数,可通过位置和键从给定范围删除元素。 C++ STL中此函数有三个变体。
C++中的unordered_multimap具有以下类型的erase()函数。
- 按位置:它从unordered_multimap中删除给定位置的元素,并返回一个迭代器,该迭代器指向紧随最后一个被删除元素之后的位置。
- 按键:它通过键删除元素。它返回已擦除的元素数。
- 按范围:它使用迭代器的第一个和最后一个,并删除它们之间的所有元素(包括第一个但不包括最后一个)。它返回一个迭代器,该迭代器指向紧随最后一个被擦除的元素之后的位置。
用法:
- iterator erase ( iterator position )
- size erase ( key_type& k )
- iterator erase ( iterator first, iterator last );
下面的程序解释了上述函数。
例子1
// C++ program to illustrate the
// unordered_multimap::erase() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// declaration of unordered_multimap
unordered_multimap<char, int> sample;
// inserts element
sample.insert({ 'a', 2 });
sample.insert({ 'b', 4 });
sample.insert({ 'c', 8 });
sample.insert({ 'd', 10 });
sample.insert({ 'c', 4 });
sample.insert({ 'e', 4 });
sample.insert({ 'f', 4 });
cout << " Elements of multimap are:\n";
for (auto& x:sample)
cout << x.first << ":" << x.second << endl;
// delete element by position
sample.erase(sample.begin());
// print after delete by position
cout << " Elements of multimap after deleting by position are:\n";
for (auto& x:sample)
cout << x.first << ":" << x.second << endl;
// erase by Element
sample.erase('c');
// print after delete by element
cout << " Elements of multimap after deleting by element name:\n";
for (auto& x:sample)
cout << x.first << ":" << x.second << endl;
// erase by range
sample.erase(sample.find('e'), sample.end());
// print after delete by range
cout << " Elements of multimap after deleting by range are:\n";
for (auto& x:sample)
cout << x.first << ":" << x.second << endl;
return 0;
}
输出:
Elements of multimap are: f:4 b:4 a:2 c:4 c:8 d:10 e:4 Elements of multimap after deleting by position are: b:4 a:2 c:4 c:8 d:10 e:4 Elements of multimap after deleting by element name: b:4 a:2 d:10 e:4 Elements of multimap after deleting by range are: b:4 a:2 d:10
例子2
// C++ program to illustrate the
// unordered_multimap::erase() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// declaration of unordered_multimap
unordered_multimap<int, int> sample;
// inserts element
sample.insert({ 1, 2 });
sample.insert({ 2, 4 });
sample.insert({ 3, 8 });
sample.insert({ 4, 10 });
sample.insert({ 3, 4 });
sample.insert({ 5, 4 });
sample.insert({ 6, 4 });
cout << " Elements of multimap are:\n";
for (auto& x:sample)
cout << x.first << ":" << x.second << endl;
// delete element by position
sample.erase(sample.begin());
// print after delete by position
cout << " Elements of multimap after deleting by position are:\n";
for (auto& x:sample)
cout << x.first << ":" << x.second << endl;
// erase by Element
sample.erase(3);
// print after delete by element
cout << " Elements of multimap after deleting by element name:\n";
for (auto& x:sample)
cout << x.first << ":" << x.second << endl;
// erase by range
sample.erase(sample.find(5), sample.end());
// print after delete by range
cout << " Elements of multimap after deleting by range are:\n";
for (auto& x:sample)
cout << x.first << ":" << x.second << endl;
return 0;
}
输出:
Elements of multimap are: 6:4 2:4 1:2 3:4 3:8 4:10 5:4 Elements of multimap after deleting by position are: 2:4 1:2 3:4 3:8 4:10 5:4 Elements of multimap after deleting by element name: 2:4 1:2 4:10 5:4 Elements of multimap after deleting by range are: 2:4 1:2 4:10
相关用法
- C++ set::erase用法及代码示例
- C++ multiset erase()用法及代码示例
- C++ multimap::erase()用法及代码示例
- C++ std::string::erase用法及代码示例
- C++ map erase()用法及代码示例
- C++ unordered_map erase用法及代码示例
- C++ unordered_multiset erase()用法及代码示例
- C++ vector erase()、clear()用法及代码示例
- C++ list erase()用法及代码示例
- C++ unordered_set erase()用法及代码示例
- C++ deque::clear()、deque::erase()用法及代码示例
注:本文由纯净天空筛选整理自ankit15697大神的英文原创作品 unordered_multimap erase in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。