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

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

public member function - 公开成员函数

1. std::map::clear

C++98
void clear();

C++11
void clear() noexcept;

Clear content - 清除内容

Removes all elements from the map container (which are destroyed), leaving the container with a size of 0.
从容器擦除所有元素。此调用后 size() 返回零。

destroy [dɪˈstrɔɪ]:vt. 破坏,消灭,毁坏

The C++ function std::map::clear() destroys the map by removing all elements and sets size of map to zero.
C++ 函数 std::map::clear() 通过删除所有元素并将 map 大小设置为零来销毁 map

Map is dictionary like data structure. It is an associative array of (key, value) pair, where only single value is associated with each unique key.
map 是类似字典的数据结构。它是 (key, value) 对的关联数组,其中每个唯一键仅与单个值相关联。

clear() function is used to remove all the elements from the map container and thus leaving it’s size 0.
clear() 函数用于从 map 容器中删除所有元素,从而使其大小保持为 0。

2. Parameters

none

3. Return value

none

4. Examples

4.1 std::map::clear

//============================================================================
// Name        : std::map::clear
// 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['x'] = 100;
	map_data['y'] = 200;
	map_data['z'] = 300;

	std::cout << "map_data contains:\n";
	for (std::map<char, int>::iterator it = map_data.begin(); it != map_data.end(); ++it)
	{
		std::cout << it->first << " => " << it->second << '\n';
	}

	map_data.clear();
	map_data['a'] = 1101;
	map_data['b'] = 2202;

	std::cout << "map_data contains:\n";
	for (std::map<char, int>::iterator it = map_data.begin(); it != map_data.end(); ++it)
	{
		std::cout << it->first << " => " << it->second << '\n';
	}

	return 0;
}

map_data contains:
x => 100
y => 200
z => 300
map_data contains:
a => 1101
b => 2202

4.2 std::map::clear

//============================================================================
// Name        : std::map::clear
// 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>

using namespace std;

int main(void)
{
	/* Initializer_list constructor */
	map<char, int> map_data =
	{
	{ 'a', 1 },
	{ 'b', 2 },
	{ 'c', 3 },
	{ 'd', 4 },
	{ 'e', 5 }, };

	cout << "Initial size of map = " << map_data.size() << endl;

	map_data.clear();

	cout << "Size of map after clear opearation = " << map_data.size() << endl;

	return 0;
}

Initial size of map = 5
Size of map after clear opearation = 0

4.3 std::map::clear

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

#include <bits/stdc++.h>
using namespace std;

int main()
{
	map<int, string> map1, map2;

	// Inserting values
	map1[1] = "cheng";
	map1[2] = "yong";
	map1[3] = "qiang";

	// Print the size of map
	cout << "Map size before running function: \n";
	cout << "map1 size = " << map1.size() << endl;
	cout << "map2 size = " << map2.size() << endl;

	// Deleting the map elements
	map1.clear();
	map2.clear();

	// Print the size of map
	cout << "\nMap size after running function: \n";
	cout << "map1 size = " << map1.size() << endl;
	cout << "map2 size = " << map2.size();

	return 0;
}

Map size before running function: 
map1 size = 3
map2 size = 0

Map size after running function: 
map1 size = 0
map2 size = 0

5. Complexity - 复杂度

Linear in size (destructions).
与容器大小,即元素数成线性。

Time complexity - Linear i.e. O(n)

destruction [dɪˈstrʌkʃn]:n. 破坏,毁灭,摧毁

6. Iterator validity - 迭代器有效性

All iterators, pointers and references related to this container are invalidated.
与该容器相关的所有迭代器,指针和引用均无效。

7. Data races - 数据竞争

The container is modified.
容器已修改。

All contained elements are modified.
所有包含的元素均已修改。

8. Exception safety - 异常安全性

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

References

http://www.cplusplus.com/reference/map/map/clear/
https://www.tutorialspoint.com/cpp_standard_library/cpp_map_clear.htm
https://www.geeksforgeeks.org/mapclear-c-stl/

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

猜你喜欢

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