Standard Template Library (STL) - std::map::size

Standard Template Library (STL) - std::map::size

public member function - 公开成员函数

1. std::map::size

C++98
size_type size() const;

C++11
size_type size() const noexcept;

Return container size - 返回容纳的元素数

Returns the number of elements in the map container.
返回 map 容器中的元素数。

返回容器中的元素数,即 std::distance(begin(), end())

2. Parameters

none - 无

3. Return value

The number of elements in the container.
容器中的元素数量。

Member type size_type is an unsigned integral type (unsigned int).

4. Examples

4.1 std::map::size

//============================================================================
// Name        : std::map::size
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <map>

int main()
{
	std::map<char, int> map_data;
	map_data['a'] = 101;
	map_data['b'] = 202;
	map_data['c'] = 302;

	std::cout << "map_data.size() is " << map_data.size() << '\n';

	return 0;
}

map_data.size() is 3

4.2 std::map::size

//============================================================================
// Name        : std::map::size
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <map>
#include <iostream>

int main()
{
	std::map<int,char> nums {{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}};

	std::cout << "nums contains " << nums.size() << " elements.\n";
}

19:34:26 **** Build of configuration Debug for project hello_world ****
make all 
Building file: ../src/hello_world.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/hello_world.d" -MT"src/hello_world.o" -o "src/hello_world.o" "../src/hello_world.cpp"
Finished building: ../src/hello_world.cpp
 
Building target: hello_world
Invoking: GCC C++ Linker
g++  -o "hello_world"  ./src/hello_world.o   
Finished building target: hello_world
 

19:34:27 Build Finished (took 778ms)
nums contains 4 elements.

5. Complexity - 复杂度

Constant. - 常数。

6. Iterator validity - 迭代器有效性

No changes.

7. Data races - 数据竞争

The container is accessed. - 容器被访问。

No elements are accessed: concurrently accessing or modifying them is safe.
没有元素被访问:同时访问或修改它们是安全的。

8. Exception safety - 异常安全性

No-throw guarantee: this member function never throws exceptions.
No-throw guarantee:此成员函数从不抛出异常。

References

http://www.cplusplus.com/reference/map/map/size/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值