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

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

public member function - 公开成员函数

1. std::vector::clear

C++98
void clear();

C++11
void clear() noexcept;

Clear content - 清除内容

从容器擦除所有元素。此调用后 size() 返回零。
非法化任何指代所含元素的引用、指针或迭代器。任何尾后迭代器亦被非法化。

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

Leaves the capacity() of the vector unchanged (note: the standard’s restriction on the changes to capacity is in the specification of vector::reserve, see [1]).
保持 vectorcapacity() 不变 (注意:更改容量上的标准限制在 vector::reserve 的规定中,见 [1])。

Removes all elements from the vector (which are destroyed), leaving the container with a size of 0.
从向量中删除所有元素 (已销毁),使容器的大小为 0。

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

A reallocation is not guaranteed to happen, and the vector capacity is not guaranteed to change due to calling this function. A typical alternative that forces a reallocation is to use swap:
由于调用此函数,不能保证会发生重新分配,也不保证矢量容量会发生变化。强制重新分配的典型替代方法是使用交换:

vector<T>().swap(x); // clear x reallocating

dereference [ˌdiːˈrefrəns]:v. 间接引用,间接访问,解引用
reallocate [ˌriːˈæləkeɪt]:v. 重新分配,再指派

Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.
std::vector 与动态数组相同,具有在插入或删除元素时自动调整自身大小的能力,并且容器自动处理其存储。

2. Parameters

none - 无

3. Return value

none - 无

4. Examples

4.1 std::vector::clear

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

int main()
{
	std::vector<int> vector_data;
	vector_data.push_back(100);
	vector_data.push_back(200);
	vector_data.push_back(300);

	std::cout << "vector_data contains:";
	for (unsigned i = 0; i < vector_data.size(); i++)
	{
		std::cout << ' ' << vector_data[i];
	}
	std::cout << '\n';

	vector_data.clear();
	vector_data.push_back(1101);
	vector_data.push_back(2202);

	std::cout << "vector_data contains:";
	for (unsigned i = 0; i < vector_data.size(); i++)
	{
		std::cout << ' ' << vector_data[i];
	}
	std::cout << '\n';

	return 0;
}

vector_data contains: 100 200 300
vector_data contains: 1101 2202

4.2 std::vector::clear

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

using namespace std;

int main(void)
{
	auto ilist = {1, 2, 3, 4, 5};
	vector<int> vector_data(ilist);

	cout << "Initial size of vector = " << vector_data.size() << endl;

	/* destroy vector */
	vector_data.clear();

	cout << "Size of vector after clear = " << vector_data.size() << endl;

	return 0;
}

Initial size of vector = 5
Size of vector after clear = 0

4.3 std::vector::clear()

std::vector::clear() function is used to remove all the elements of the vector container, thus making it size 0.
std::vector::clear() 函数用于删除 std::vector 容器的所有元素,从而使其大小为 0。

All the elements of the vector are removed (or destroyed).
std::vector 的所有元素都将被删除 (或销毁)。

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

using namespace std;

int main()
{
	vector<int> vector_data;
	vector_data.push_back(1);
	vector_data.push_back(2);
	vector_data.push_back(3);
	vector_data.push_back(4);
	vector_data.push_back(5);

	// Vector becomes 1, 2, 3, 4, 5

	vector_data.clear();
	// Vector becomes empty

	// Printing the vector
	for (auto it = vector_data.begin(); it != vector_data.end(); ++it)
	{
		cout << ' ' << *it;
	}

	return 0;
}

No Output

Time Complexity: O(N)
All elements are destroyed one by one.
所有元素都被一一销毁。

4.4 std::vector::erase()

std::vector::erase() function is used to remove elements from a container from the specified position or range.
std::vector::erase() 函数用于从指定位置或指定范围内删除容器中的元素。

vectorname.erase(position)
vectorname.erase(startingposition, endingposition)

Elements are removed from the specified position of the container.
从容器的指定位置删除元素。

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

#include <iostream>
#include <vector>

using namespace std;

int main()
{
	vector<int> vector_data {1, 2, 3, 4, 5};
	vector<int>::iterator it;

	it = vector_data.begin();
	vector_data.erase(it);

	// Printing the Vector
	for (auto it = vector_data.begin(); it != vector_data.end(); ++it)
	{
		cout << ' ' << *it;
	}

	return 0;
}

 2 3 4 5

4.5 std::vector::erase()

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

#include <iostream>
#include <vector>

using namespace std;

int main()
{
	vector<int> vector_data	{1, 2, 3, 4, 5};
	vector<int>::iterator it1, it2;

	it1 = vector_data.begin();
	it2 = vector_data.end();
	it2--;
	it2--;

	vector_data.erase(it1, it2);

	// Printing the Vector
	for (auto it = vector_data.begin(); it != vector_data.end(); ++it)
	{
		cout << ' ' << *it;
	}

	return 0;
}

 4 5

clear() removes all the elements from a vector container, thus making its size 0. All the elements of the vector are removed using clear() function.
clear() 从向量容器中删除所有元素,从而使其大小为 0。使用 clear() 函数将向量的所有元素删除。

erase() function on the other hand, is used to remove specific elements from the container or a range of elements from the container, thus reducing its size by the number of elements removed.
另一方面,erase() 函数用于从容器中删除特定元素或从容器中删除一系列元素,从而通过删除元素的数量来减小其大小。

5. Complexity - 复杂度

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

This may be optimized to constant complexity for trivially-destructible types (such as scalar or PODs), where elements need not be destroyed.
对于不需要破坏元素的琐碎可分解类型 (例如标量或 PODs),可以将其优化为恒定的复杂度。

destruction [dɪˈstrʌkʃn]:n. 破坏,毁灭,摧毁
trivially [ˈtrɪviəli]:adv. 琐细地,平凡地,无能地
destructible [dɪ'strʌktɪb(ə)l]:adj. 可破坏的,易损坏的

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.
无抛出保证:此成员函数从不抛出异常。

assignment [ə'saɪnmənt]:n. 任务,布置,赋值

References

http://www.cplusplus.com/reference/vector/vector/clear/
https://www.tutorialspoint.com/cpp_standard_library/cpp_vector_clear.htm
https://www.geeksforgeeks.org/vector-erase-and-clear-in-cpp/

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

猜你喜欢

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