Cpp Standard Library 简明教程

C++ Library - <vector>

Introduction

向量是可更改大小的序列容器。 容器是保存相同类型数据的对象。 序列容器将元素严格存储在线性序列中。

Vectors are sequence container that can change size. Container is a objects that hold data of same type. Sequence containers store elements strictly in linear sequence.

向量将元素存储在连续的内存位置中,并可以使用下标运算符 [] 直接访问任何元素。 与数组不同,向量可以在运行时根据需要进行收缩或扩展。 向量的存储会自动处理。

Vector stores elements in contiguous memory locations and enables direct access to any element using subscript operator []. Unlike array, vector can shrink or expand as needed at run time. The storage of the vector is handled automatically.

为了支持运行时的收缩和扩展功能,向量容器可能会分配一些额外的存储空间以容纳可能的增长,因此容器的实际容量大于大小。 因此,与数组相比,向量消耗更多的内存,以换取有效管理存储和动态增长。

To support shrink and expand functionality at runtime, vector container may allocate some extra storage to accommodate for possible growth thus container have actual capacity greater than the size. Therefore, compared to array, vector consumes more memory in exchange for the ability to manage storage and grow dynamically in an efficient way.

零大小的向量也有效。在这种情况中,vector.begin() 和 vector.end() 指向相同的位置。但调用 front() 或 back() 的行为是未定义的。

Zero sized vectors are also valid. In that case vector.begin() and vector.end() points to same location. But behavior of calling front() or back() is undefined.

Definition

以下是 <vector> 头文件中的 std::vector 定义

Below is definition of std::vector from <vector> header file

template < class T, class Alloc = allocator<T> > class vector;

Parameters

  1. T − Type of the element contained. T may be substituted by any other data type including user-defined type.

  2. Alloc − Type of allocator object. By default, the allocator class template is used, which defines the simplest memory allocation model and is value-independent.

Member types

以下成员类型可作为参数或返回类型,由成员函数使用。

Following member types can be used as parameters or return type by member functions.

Sr.No.

Member types

Definition

1

value_type

T (First parameter of the template)

2

allocator_type

Alloc (Second parameter of the template)

3

reference

value_type&

4

const_reference

const value_type&

5

pointer

value_type*

6

const_pointer

const value_type*

7

iterator

a random access iterator to value_type

8

const_iterator

a random access iterator to const value_type

9

reverse_iterator

std::reverse_iterator <iterator>

10

const_reverse_iterator

std::reverse_iterator <const_iterator>

11

size_type

size_t

12

difference_type

ptrdiff_t

Functions from <vector>

以下是 <vector> 头文件中的所有方法列表。

Below is list of all methods from <vector> header.

Constructors

Sr.No.

Method & Description

1

vector::vector default constructorConstructs an empty container, with zero elements.

2

vector::vector fill constructorConstructs a container with n elements and assignd val to each element.

3

vector::vector range constructorConstructs a container with as many elements in range of first to last.

4

vector::vector copy constructorConstructs a container with copy of each elements present in existing container x.

5

vector::vector move constructorConstructs the container with the contents of other using move semantics.

6

vector::vector initializer list constructorConstructs a container from initializer list.

Destructor

Sr.No.

Method & Description

1

vector::~vectorDestroys container by deallocating container memory.

Member functions

Sr.No.

Method & Description

1

vector::assign fill versionAssign new values to the vector elements by replacing old ones.

2

vector::assign range versionAssign new values to the vector elements by replacing old ones.

3

vector::assign initializer list versionAssign new values to the vector elements by replacing old ones.

4

vector::atReturns reference to the element present at location n in the vector.

5

vector::backReturns a reference to the last element of the vector.

6

vector::beginReturn a random access iterator pointing to the first element of the vector.

7

vector::capacityReturns the size of allocate storage, expressed in terms of elements.

8

vector::cbeginReturns a constant random access iterator which points to the beginning of the vector.

9

vector::cendReturns a constant random access iterator which points to the beginning of the vector.

10

vector::clearDestroys the vector by removing all elements from the vector and sets size of vector to zero.

11

vector::crbeginReturns a constant reverse iterator which points to the reverser beginning of the container.

12

vector::crendReturns a constant reverse iterator which points to the reverse end of the vector.

13

vector::dataReturns a pointer to the first element of the vector container.

14

vector::emplaceExtends container by inserting new element at position.

15

vector::emplace_backInserts new element at the end of vector.

16

vector::emptyTests whether vector is empty or not.

17

vector::endReturns an iterator which points to past-the-end element in the vector container.

18

vector::erase position versionRemoves single element from the the vector.

19

vector::erase range versionRemoves single element from the the vector.

20

vector::frontReturns a reference to the first element of the vector.

21

vector::get_allocatorReturns an allocator associated with vector.

22

vector::insert single element versionExtends iterator by inserting new element at position.

23

vector::insert fill versionExtends vector by inserting new element in the container.

24

vector::insert range versionExtends vector by inserting new element in the container.

25

vector::insert move versionExtends vector by inserting new element in the container.

26

vector::insert initializer list versionExtends vector by inserting new element in the container.

27

vector::max_sizeReturns the maximum number of elements can be held by vector.

28

vector::operator= copy version Assign new contents to the vector by replacing old ones and modifies size if necessary.

29

vector::operator= move version Assign new contents to the vector by replacing old ones and modifies size if necessary.

30

vector::operator = initializer list version Assign new contents to the vector by replacing old ones and modifies size if necessary.

31

vector::operator[]Returns a reference to the element present at location n.

32

vector::pop_backRemoves last element from vector and reduces size of vector by one.

33

vector::push_backInserts new element at the end of vector and increases size of vector by one.

34

vector::rbeginReturns a reverse iterator which points to the last element of the vector.

35

vector::rendReturns a reverse iterator which points to the reverse end of the vector.

36

vector::reserveRequests to reserve vector capacity be at least enough to contain n elements.

37

vector::resizeChanges the size of vector.

38

vector::shrink_to_fitRequests the container to reduce it’s capacity to fit its size.

39

vector::sizeReturns the number of elements present in the vector.

40

vector::swapExchanges the content of vector with contents of vector x.

Non-member overloaded functions

Sr.No.

Method & Description

1

[role="bare"]../cpp_standard_library/cpp_vector_operator_equal_to.htmlTests whether two vectors are equal or not.

2

operator !=Tests whether two vectors are equal or not.

3

operator <Tests whether first vector is less than other or not.

4

operator ⇐Tests whether first vector is less than or equal to other or not.

5

operator >Tests whether first vector is greater than other or not.

6

operator >=Tests whether first vector is greater than or equal to other or not.

7

swapExchanges the contents of two vector.