C++ STL 中的 map::size()
在本文中,我们将讨论 C++ STL 中 map::size() 函数的工作原理、语法和示例。
什么是 C++ STL 中的 Map?
映射是关联容器,它便利于存储由键值和映射值组成的元素,并且按照特定顺序存储。在映射容器中,数据在内部始终通过其关联键排序。映射容器中的值可通过其唯一键访问。
什么是 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP