map::empty() 在 C++ STL 中
在本文中我们将讨论 C++ STL 中 map::empty() 函数的工作原理、语法和示例。
何为 C++ STL 中的 Map?
映射是关联容器,它有助于按照特定的顺序储存由键值和映射值组合构成的元素。在 map 容器中,数据在内部始终借助其关联的键进行排序。容器中值通过其唯一键进行访问。
何为 map::empty()?
map::empty() 函数是在 C++ STL 中的一个内建函数,定义在它检查容器的大小是否为 0,如果是则返回 true,否则如果有值则返回 false。
语法
参数
map_name.empty();
该函数不接受参数。
返回值
如果 map 为空则该函数返回 true,如果不为空则返回 false。
示例
输入
std::map<int> mymap;
mymap.insert({‘a’, 10});
mymap.insert({‘b’, 20});
mymap.insert({‘c’, 30});
mymap.empty();输出
false
std::map<int> mymap; mymap.empty();
输出
true
输入
#include <bits/stdc++.h>
using namespace std;
int main() {
map<int, int> TP_1;
TP_1[1] = 10;
TP_1[2] = 20;
TP_1[3] = 30;
TP_1[4] = 40;
if(TP_1.empty()) {
cout<<"Map is NULL";
} else {
cout<<"Map isn't NULL";
}
return 0;
}输出
Map isn't NULL
输入
#include <bits/stdc++.h>
using namespace std;
int main() {
map<int, int> TP_1;
map<int, int> TP_2;
TP_1[1] = 10;
TP_1[2] = 20;
TP_1[3] = 30;
TP_1[4] = 40;
if(TP_1.empty()) {
cout<<"Map_1 is NULL";
} else {
cout<<"Map_1 isn't NULL";
}
if(TP_2.empty()) {
cout<<"\nMap_2 is NULL";
} else {
cout<<"Map_2 isn't NULL";
}
return 0;
}输出
Map_1 isn't NULL Map_2 is NULL
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP