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

📅  最后修改于: 2021-05-30 13:05:54             🧑  作者: Mango

unordered_multiset :: empty()是C++ STL中的内置函数,该函数返回布尔值。如果unordered_multiset容器为空,则返回true。否则,它返回false。

句法:

unordered_multiset_name.empty()

参数:该函数不接受任何参数。

返回值:返回一个布尔值,该值指示unordered_multiset是否为空。

下面的程序说明了上述函数:

程序1:

// C++ program to illustrate the
// unordered_multiset::empty() function
#include 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multiset sample;
  
    // inserts element
    sample.insert(11);
    sample.insert(11);
    sample.insert(11);
    sample.insert(12);
    sample.insert(13);
    sample.insert(13);
    sample.insert(14);
  
    // if not empty then print the elements
    if (sample.empty() == false) {
        cout << "Elements: ";
  
        for (auto it = sample.begin(); it != sample.end(); it++) {
            cout << *it << " ";
        }
    }
  
    // container is erased completely
    sample.clear();
  
    if (sample.empty() == true)
        cout << "\nContainer is empty";
    return 0;
}
输出:
Elements: 14 11 11 11 12 13 13 
Container is empty

程式2:

// C++ program to illustrate the
// unordered_multiset::empty() function
#include 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multiset sample;
  
    // inserts element
    sample.insert('a');
    sample.insert('a');
    sample.insert('b');
    sample.insert('c');
    sample.insert('d');
    sample.insert('d');
    sample.insert('d');
  
    // if not empty then print the elements
    if (sample.empty() == false) {
        cout << "Elements: ";
  
        for (auto it = sample.begin(); it != sample.end(); it++) {
            cout << *it << " ";
        }
    }
  
    // container is erased completely
    sample.clear();
  
    if (sample.empty() == true)
        cout << "\nContainer is empty";
    return 0;
}
输出:
Elements: a a b c d d d 
Container is empty
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”