stl中map函数_带有示例的C ++ STL中的map :: size()函数

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函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值