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 個瀏覽量

開啟你的 職業生涯

完成課程即可取得認證

開始
廣告