std::vector::clear()

public member function

1. std::vector::clear()

C++98

void clear();

C++11

void clear() noexcept;

Clear content (清除内容)

从容器擦除所有元素。此调用后 std::vector::size() 返回零。

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

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

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

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

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

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

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

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

	std::cout << "Size of vector after clear = " << vector_data.size() << std::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>

int main() {
	std::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);

	for (auto it = vector_data.begin(); it != vector_data.end(); ++it) {
		std::cout << ' ' << *it;
	}
	std::cout << std::endl;

	vector_data.clear();

	for (auto it = vector_data.begin(); it != vector_data.end(); ++it) {
		std::cout << ' ' << *it;
	}
	std::cout << std::endl;

	return 0;
}

 1 2 3 4 5

请按任意键继续. . .

Time Complexity: O(N)

All elements are destroyed one by one.
所有元素都被一一销毁。

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

std::vector::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.
另一方面,std::vector::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.
可以将其优化为恒定的复杂度。

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

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] std::vector::clear(), https://cplusplus.com/reference/vector/vector/clear/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Yongqiang Cheng

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

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

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

打赏作者

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

抵扣说明:

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

余额充值