C++ - Vector


Advertisements

Previous Page Next Page

C++ vectors

vectors are one of the most common data types in C++. It is used for storing text. It can be created by enclosing characters in double quotation marks. It can be assigned to a variable using = sign.

vector Myvector = "Hello World!";

Access character of a vector

A character (also called element) of a vector can be accessed with it's index number. In C++, index number starts with 0 in forward direction. The below figure and example describe the indexing concept of a vector.

C++ - Vector Functions

Capacity

FunctionsDescription
empty() Checks whether the vector is empty or not.
size() Returns the length of the vector in terms of bytes.
max_size() Returns the maximum length of the vector.
capacity() Returns size of allocated space to the vector.

Element Access

FunctionsDescription
at() Access an element of the vector.
operator[]() Access an element of the vector.
back() Access last element of the vector.
front() Access first element of the vector.

Iterators

FunctionsDescription
begin() Returns iterator pointing to the first element of the vector.
end() Returns iterator pointing to the past-the-last element of the vector.
rbegin() Returns reverse iterator to the last element of the vector.
rend() Returns reverse iterator to the element preceding the first element of the vector.
cbegin() Returns const_iterator pointing to the first element of the vector.
cend() Returns const_iterator pointing to the past-the-last element of the vector.
crbegin() Returns const_reverse_iterator to the last element of the vector.
crend() Returns const_reverse_iterator to the element preceding the first element of the vector.

Modifiers

FunctionsDescription
clear() Clears all elements of the vector.
pop_back() Deletes last element of the vector.
push_back() Adds a new element at the end of the vector.

Non-member function overloads

FunctionsDescription
swap() Exchanges elements between two vectors.

Extras

FunctionsDescription
resize() Exchanges elements between two vectors.
reserve() Exchanges elements between two vectors.
shrink_to_fit() Exchanges elements between two vectors.
data() Exchanges elements between two vectors.
assign() Exchanges elements between two vectors.
insert() Exchanges elements between two vectors.
erase() Exchanges elements between two vectors.
emplace() Exchanges elements between two vectors.
emplace_back() Exchanges elements between two vectors.
operator=() Exchanges elements between two vectors.
get_allocator() Exchanges elements between two vectors.

Previous Page Next Page
Advertisements