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/

发布了443 篇原创文章 · 获赞 1685 · 访问量 101万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104226965