📜  C++ STL中的unordered_multimap empty()函数

📅  最后修改于: 2021-05-30 11:10:44             🧑  作者: Mango

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