當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。