C++ STL 中的 multiset empty() 函式


在本文中,我們將討論 C++ STL 中 multiset::empty() 函式的工作原理、語法和示例。

什麼是 C++ STL 中的 multiset?

Multiset 與 set 容器類似,這意味著它們像 set 一樣以鍵的形式儲存值,並按照特定順序排列。

在 multiset 中,值與 set 一樣被識別為鍵。multiset 和 set 的主要區別在於,set 具有不同的鍵(意味著沒有兩個鍵是相同的),而在 multiset 中可以存在相同的鍵值。

Multiset 鍵用於實現二叉搜尋樹。

什麼是 multiset::empty()?

multiset::empty() 函式是 C++ STL 中的內建函式,它在 <set> 標頭檔案中定義。

此函式檢查關聯的 multiset 容器是否為空。

如果關聯容器的大小為 0,則 empty() 返回 true;否則,如果容器中存在任何元素或容器的大小不為 0,則函式將返回 false。

語法

ms_name.empty();

引數

此函式不接受任何引數。

返回值

如果容器為空,此函式返回布林值 true,否則返回 false。

示例

Input: std::multiset<int> mymultiset = {1, 2, 2, 3, 4};
   mymultiset.empty();
Output: false

Input: std::multiset<int> mymultiset;
   mymultiset.empty();
Output: true

示例

 線上演示

#include <bits/stdc++.h>
using namespace std;
int main() {
   int arr[] = {2, 3, 4, 5};
   multiset<int> check(arr, arr + 4);
   if (check.empty())
      cout <<"The multiset is empty";
   else
      cout << "The multiset isn't empty";
   return 0;
}

輸出

如果我們執行上面的程式碼,它將生成以下輸出:

The multiset isn't empty

示例

 線上演示

#include <bits/stdc++.h>
using namespace std;
int main() {
   int arr[] = {};
   multiset<int> check(arr, arr + 0);
   if (check.empty())
      cout <<"The multiset is empty";
   else
      cout << "The multiset isn't empty";
   return 0;
}

輸出

如果我們執行上面的程式碼,它將生成以下輸出:

The multiset is empty

更新於:2020年3月23日

111 次檢視

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.