在本文中,我们将讨论C ++ STL中map::size()函数的工作原理,语法和示例。
映射是关联容器,它有助于按特定顺序存储由键值和映射值的组合形成的元素。在映射容器中,数据始终在内部借助其关联的键进行排序。映射容器中的值通过其唯一键访问。
map::size()函数是C ++ STL中的内置函数,该函数在
map_name.size();
该函数不接受任何参数。
此函数返回映射容器中的元素数。如果容器没有值,则该函数返回0。
std::map<int> mymap;
mymap.insert({‘a’, 10});
mymap.insert({‘b’, 20});
mymap.insert({‘c’, 30});
mymap.size();输出结果
3
std::map<int> mymap; mymap.size();
输出结果
0
#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;
   cout<<"Size of TP_1 is: "<<TP_1.size();
   return 0;
}输出结果
Size of TP_1 is: 4
#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;
   auto size = TP_1.size();
   auto temp = 1;
   while(size!=0) {
      temp = temp * 10;
      size--;
   }
   cout<<"Temp value is: "<<temp<<endl;
   return 0;
}输出结果
Temp value is: 10000