stl中map函数
Maps are a part of the C++ STL and key values in maps are generally used to identify the elements i.e. there is a value associated with every key. map::size() is built-in function in C++ used to find the size of the map. The time complexity of this function is constant i.e. O(1).
映射是C ++ STL的一部分,并且映射中的键值通常用于标识元素,即每个键都有一个关联的值。 map :: size()是C ++中的内置函数,用于查找map的大小 。 该函数的时间复杂度是恒定的,即O(1) 。
Syntax:
句法:
MapName.size()
Return Value:
返回值:
It returns the size of the map i.e. number of elements map container have.
它返回地图的大小,即地图容器具有的元素数。
Parameters:
参数:
There is no parameter to be passed.
没有要传递的参数。
Example:
例:
MyMap ={
{1, "c++"},
{2, "java"},
{3, "Python"},
{4, "HTML"},
{5,"PHP"}
};
Here, MyMap.size() returns 5.
在这里, MyMap.size()返回5 。
Program:
程序:
#include <bits/stdc++.h>
using namespace std;
int main()
{
map<int, string> MyMap1, MyMap2;
MyMap1.insert(pair <int, string> (1, "c++"));
MyMap1.insert(pair <int, string> (2, "java"));
MyMap1.insert(pair <int, string> (3, "Python"));
MyMap1.insert(pair <int, string> (4, "HTML"));
MyMap1.insert(pair <int, string> (5, "PHP"));
map <int, string> :: iterator it;
cout<<"Elements in MyMap1 : "<<endl;
for (it = MyMap1.begin(); it != MyMap1.end(); it++)
{
cout <<"key = "<< it->first <<" value = "<< it->second <<endl;
}
cout<<endl;
cout << "size of MyMap1 : " << MyMap1.size()<<endl;
cout << "size of MyMap2 : " << MyMap2.size();
return 0;
}
Output
输出量
Elements in MyMap1 :
key = 1 value = c++
key = 2 value = java
key = 3 value = Python
key = 4 value = HTML
key = 5 value = PHP
size of MyMap1 : 5
size of MyMap2 : 0
翻译自: https://www.includehelp.com/stl/map-size-function-in-cpp-stl-with-example.aspx
stl中map函数