📜  C++ STL中的unordered_multimap擦除

📅  最后修改于: 2021-05-30 14:21:12             🧑  作者: Mango

unordered_multimap :: erase()是C++ STL中的内置函数,可通过位置和键从给定范围中删除元素。 C++ STL中有此函数的三个变体。
C++中针对unordered_multimap有以下类型的delete()函数。

  1. 按位置:按给定位置从unordered_multimap中删除元素,并返回一个迭代器,该迭代器指向紧随最后一个被删除的元素之后的位置。
  2. 按键:通过键删除元素。它返回已擦除的元素数。
  3. 按范围:它使用迭代器的第一个和最后一个,并删除它们之间的所有元素,包括first到last除外。它返回一个迭代器,该迭代器指向紧随最后一个被擦除的元素之后的位置。

句法:

下面的程序说明了有关上述功能的信息。
例子1

// C++ program to illustrate the
// unordered_multimap::erase() function
#include 
using namespace std;
  
int main()
{
  
    // declaration of unordered_multimap
    unordered_multimap 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 
using namespace std;
  
int main()
{
  
    // declaration of unordered_multimap
    unordered_multimap 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++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”