C ++ STL中的map :: size()
在本文中,我们将讨论C++STL中map::size()函数的工作原理,语法和示例。
什么是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