当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ unordered_multimap erase用法及代码示例


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

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

用法:

  1. iterator erase ( iterator position )
  2. size erase ( key_type& k )
  3. 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



相关用法


注:本文由纯净天空筛选整理自ankit15697大神的英文原创作品 unordered_multimap erase in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。