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
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP