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


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


unordered_multimap::empty()是C++ STL中的內置函數,該函數返回布爾值。如果unordered_multimap容器為空,則返回true。否則,它返回false。

用法:

unordered_multimap_name.empty()

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


返回值:它返回一個布爾值,該值指示unordered_multimap是否為空。

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

示例1:

// C++ program to illustrate the 
// unordered_multimap::empty() function 
#include <iostream> 
#include <unordered_map> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multimap<int, int> sample; 
  
    // inserts key and element 
    sample.insert({ 1, 2 }); 
    sample.insert({ 1, 2 }); 
    sample.insert({ 2, 3 }); 
    sample.insert({ 3, 4 }); 
    sample.insert({ 5, 6 }); 
  
    // if not empty then print the elements 
    if (sample.empty() == false) { 
        cout << "Key and Elements: "; 
  
        for (auto it = sample.begin(); it != sample.end(); it++) { 
            cout << "{" << it->first << ":" << it->second << "} "; 
        } 
    } 
  
    // container is erased completely 
    sample.clear(); 
  
    if (sample.empty() == true) 
        cout << "\nContainer is empty"; 
  
    return 0; 
}
輸出:
Key and Elements: {5:6} {3:4} {2:3} {1:2} {1:2} 
Container is empty

示例2:

// C++ program to illustrate the 
// unordered_multimap::empty() 
#include <iostream> 
#include <unordered_map> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multimap<char, char> sample; 
  
    // inserts element 
    sample.insert({ 'a', 'b' }); 
    sample.insert({ 'a', 'b' }); 
    sample.insert({ 'g', 'd' }); 
    sample.insert({ 'r', 'e' }); 
    sample.insert({ 'g', 'd' }); 
  
    // if not empty then print the elements 
    if (sample.empty() == false) { 
        cout << "Key and elements: "; 
  
        for (auto it = sample.begin(); it != sample.end(); it++) { 
            cout << "{" << it->first << ":" << it->second << "} "; 
        } 
    } 
  
    // container is erased completely 
    sample.clear(); 
  
    if (sample.empty() == true) 
        cout << "\nContainer is empty"; 
  
    return 0; 
}
輸出:
Key and elements: {r:e} {g:d} {g:d} {a:b} {a:b} 
Container is empty


相關用法


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