📜  C++ STL中的unordered_map为空

📅  最后修改于: 2021-05-30 03:25:36             🧑  作者: Mango

unordered_map :: empty()函数用于检查容器大小是否为零。如果容器大小为零,则返回TRUE,否则返回FALSE。
句法:

unordered_map_name.empty()

参数:此函数不接受任何参数
返回类型:该函数返回布尔值TRUE或FALSE。

例子:

// CPP program to illustrate
// Implementation of unordered_map empty() function
#include 
using namespace std;
  
int main()
{
    // Take any two unordered_map
    unordered_map 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
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”