在本文中,我们将讨论C ++ STL中map::empty()函数的工作,语法和示例。
映射是关联容器,它有助于按特定顺序存储由键值和映射值的组合形成的元素。在映射容器中,数据始终在内部借助其关联的键进行排序。映射容器中的值通过其唯一键访问。
map::empty()函数是C ++ STL中的内置函数,该函数在
此函数检查容器的大小是否为0,然后返回true;否则,如果有一些值,则返回false。
map_name.empty();
该函数不接受任何参数。
如果映射为空,则此函数返回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