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

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

public member function - 公开成员函数

1. std::map::empty

C++98
bool empty() const;

C++11
bool empty() const noexcept;

Test whether container is empty - 检查容器是否为空

检查容器是否无元素,即是否 begin() == end()

Returns whether the map container is empty (i.e. whether its size is 0).
返回 map 容器是否为空 (即其大小是否为 0)。

This function does not modify the container in any way. To clear the content of a map container, see map::clear.
此函数不会以任何方式修改容器。要清除 map 容器的内容,请参见 map::clear

2. Parameters

none - 无

3. Return value

true if the container size is 0, false otherwise.
若容器为空则为 true,否则为 false

4. Examples

4.1 std::map::empty

//============================================================================
// Name        : std::map::empty
// 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'] = 10;
	map_data['b'] = 20;
	map_data['c'] = 30;

	while (!map_data.empty())
	{
		std::cout << map_data.begin()->first << " => "<< map_data.begin()->second << '\n';
		map_data.erase(map_data.begin());
	}

	return 0;
}

a => 10
b => 20
c => 30

4.2 std::map::empty

//============================================================================
// Name        : std::map::empty
// 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>
#include <utility>

int main()
{
	std::map<int, int> numbers;
	std::cout << "Initially, numbers.empty(): " << numbers.empty() << '\n';

	numbers.emplace(42, 13);
	numbers.insert(std::make_pair(13317, 123));
	std::cout << "After adding elements, numbers.empty(): " << numbers.empty() << '\n';

	return 0;
}

20:19:28 **** 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
 

20:19:29 Build Finished (took 837ms)
Initially, numbers.empty(): 1
After adding elements, numbers.empty(): 0

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/empty/

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

猜你喜欢

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