map::empty() 在 C++ STL 中


在本文中我们将讨论 C++ STL 中 map::empty() 函数的工作原理、语法和示例。

何为 C++ STL 中的 Map?

映射是关联容器,它有助于按照特定的顺序储存由键值和映射值组合构成的元素。在 map 容器中,数据在内部始终借助其关联的键进行排序。容器中值通过其唯一键进行访问。

何为 map::empty()?

map::empty() 函数是在 C++ STL 中的一个内建函数,定义在它检查容器的大小是否为 0,如果是则返回 true,否则如果有值则返回 false。 header file. empty() is used to check whether the associated map container is empty or not

语法

参数

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

更新于:15-Apr-2020

436 个浏览量

开启你的 职业生涯

完成课程即可取得认证

开始
广告
© . All rights reserved.