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


C++ unordered_map empty用法及代碼示例


unordered_map::empty()函數用於檢查容器大小是否為零。如果容器大小為零,則返回TRUE,否則返回FALSE。
用法:

 unordered_map_name.empty()

參數:該函數不接受任何參數
返回類型:此函數返回布爾值TRUE或FALSE。

Examples:



Input: ump = { {1, 2}, {3, 4}, {5, 6}, {7, 8}}
ump.empty();
Output: False

Input: ump = { };
ump.empty();
Output: True

// CPP program to illustrate 
// Implementation of unordered_map empty() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // Take any two unordered_map 
    unordered_map<int, int> ump1, ump2; 
  
    // Inserting values 
    ump1[1] = 2; 
    ump1[3] = 4; 
    ump1[5] = 6; 
    ump1[7] = 8; 
  
    // Print the size of container 
    cout << "ump1 size = " << ump1.size() << endl; 
    cout << "ump2 size = " << ump2.size() << endl; 
  
    // Running the function for ump1 
    if (ump1.empty()) 
        cout << "True\n"; 
    else
        cout << "False\n"; 
  
    // Running the function for ump2 
    if (ump2.empty()) 
        cout << "True\n"; 
    else
        cout << "False\n"; 
  
    // Running the function for ump1 after 
    // clearing it 
    ump1.clear(); 
    if (ump1.empty()) 
        cout << "True\n"; 
    else
        cout << "False\n"; 
  
    return 0; 
}
輸出:
ump1 size = 4
ump2 size = 0
False
True
True



相關用法


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