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


C++ unordered_multimap clear()用法及代碼示例


unordered_multimap::clear()是C++ STL中的內置函數,可清除unordered_multimap容器中的內容。調用該函數後,容器的最終大小為0。

用法:

unordered_multimap_name.clear()

參數:該函數不接受任何參數。


返回值:它什麽也不返回。

以下示例程序旨在說明上述函數:

示例1:

// C++ program to illustrate the 
// unordered_multimap::clear() 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multimap<int, int> sample; 
  
    // inserts key an delement 
    sample.insert({ 10, 100 }); 
    sample.insert({ 10, 100 }); 
    sample.insert({ 20, 200 }); 
    sample.insert({ 30, 300 }); 
    sample.insert({ 15, 150 }); 
  
    cout << "Key and Elements of multimap are:"; 
  
    for (auto it = sample.begin(); it != sample.end(); it++) { 
        cout << "\n{" << it->first << ", " << it->second << "}"; 
    } 
  
    sample.clear(); 
  
    cout << "\nSize of container after function call: "
         << sample.size(); 
  
    return 0; 
}
輸出:
Key and Elements of multimap are:
{15, 150}
{30, 300}
{20, 200}
{10, 100}
{10, 100}
Size of container after function call: 0

示例2:

// C++ program to illustrate the 
// unordered_multimap::clear() 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multimap<char, char> sample; 
  
    // inserts element 
    sample.insert({ 'a', 'b' }); 
    sample.insert({ 'a', 'b' }); 
    sample.insert({ 'b', 'c' }); 
    sample.insert({ 'r', 'a' }); 
    sample.insert({ 'c', 'b' }); 
  
    cout << "Key and Elements of multimap are:"; 
  
    for (auto it = sample.begin(); it != sample.end(); it++) { 
        cout << "\n{" << it->first << ", " << it->second << "}"; 
    } 
  
    sample.clear(); 
  
    cout << "\nSize of container after function call: "
         << sample.size(); 
    return 0; 
}
輸出:
Key and Elements of multimap are:
{c, b}
{r, a}
{b, c}
{a, b}
{a, b}
Size of container after function call: 0


相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 unordered_multimap clear() function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。