Delen via


vector Class

The STL vector class is a class template that stores elements of a given type in a linear arrangement and allows fast random access to any element. The vector class should be the preferred container for a sequence when random-access performance is at a premium. When you are not sure what type of sequence container to use, use vector.

template < 
   class Type,  
   class Allocator = allocator<Type>  
> 
class vector

Parameters

  • Type
    The type of the element to be stored in the vector

  • Allocator
    The type of the object that allocates and deallocates memory for the vector. This argument is optional and the default value is std::allocator*<Type>.*

Remarks

Vectors allow constant time insertions and deletions at the end of the sequence. Inserting or deleting elements in the middle of a vector requires linear time. The performance of the deque Class container is superior with respect to insertions and deletions at the beginning and end of a sequence. The list Class container is superior with respect to insertions and deletions at any location within a sequence.

Vector reallocation occurs when a member function must increase the number of elements contained in the vector beyond its current storage capacity. Other insertions and erasures may alter various storage addresses within the sequence. In all such cases, iterators or references that point at altered portions of the sequence become invalid. If no reallocation happens, only iterators and references before the insertion/deletion point remain valid.

The vector<bool> Class is a full specialization of the class template vector for elements of type bool with an allocator for the underlying type used by the specialization.

The vector<bool> reference Class is a nested class whose objects are able to provide references to elements (single bits) within a vector<bool> object.

Members

Constructors

vector

Constructs a vector of a specific size or with elements of a specific value or with a specific allocator or as a copy of some other vector.

Typedefs

allocator_type

The type of the allocator used by the vector.

const_iterator

A type that represents a random-access read-only iterator.

const_pointer

A type that represents a read-only pointer to an element in a vector.

const_reference

A reference to a const element stored in a vector for reading and performing const operations.

const_reverse_iterator

A read-only reverse iterator.

difference_type

A type that represents the difference between the addresses of two elements in a vector.

iterator

A type that provides a random-access iterator that can read or modify any element in a vector.

pointer

A type that represents a pointer to an element in a vector.

reference

A type that represents a reference to an element stored in a vector.

reverse_iterator

A type that represents a reverse_iterator.

size_type

A type that represents the number of elements in a vector.

value_type

A type that represents the data type stored in a vector.

Member Functions

assign

Replaces the specified elements with copies of the new elements.

at

Returns a reference to the element at a specified location in the vector and throws out_of_range exception if the index is less than zero or greater than or equal to size().

back

Returns a reference to the last element of the vector.

begin

Returns a random-access iterator to the first element in the vector.

capacity

Returns the number of elements that the vector could contain without allocating more storage.

cbegin

Returns a random-access const iterator to the first element in the vector.

cend

Returns a random-access const iterator that points to one pastj the end of the vector.

crbegin

Returns a const reverse_iterator to the last element in a vector.

crend

Returns a const reverse_iterator that points to one before the first element in the vector.

clear

Erases the elements of the vector.

data

Returns a pointer to the first element in the vector.

emplace

Inserts an element constructed in place into the vector at a specified position.

emplace_back

Adds an element constructed in place to the end of the vector.

empty

Tests if the vector contains any elements.

end

Returns a random-access iterator that points to one past the last element of the vector.

erase

Removes an element or a range of elements in a vector from specified positions.

front

Returns a reference to the first element in a vector.

get_allocator

Returns the allocator used by the vector.

insert

Inserts an element or a number of elements into the vector at a specified position.

max_size

Returns the maximum length of the vector.

pop_back

Deletes the element at the end of the vector.

push_back

Adds an element to the end of the vector.

rbegin

Returns an iterator to the first element in a reversed vector.

rend

Returns a reverse_iterator that points to one before the first element.

reserve

Reserves a minimum length of storage for a vector object.

resize

Specifies a new size for a vector.

shrink_to_fit

Discards excess capacity.

size

Returns the number of elements in the vector.

swap

Exchanges the elements of two vectors.

Operators

operator[]

Returns a reference to the vector element at a specified position.

operator=

Replaces the elements of the vector with a copy of another vector.

Requirements

Header: <vector>

Namespace: std

See Also

Reference

Thread Safety in the C++ Standard Library

Standard Template Library