English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Vector (vector)

Vector is C++Part of the Standard Template Library, it is a multifunctional template class and function library that can operate on various data structures and algorithms. Vector is considered a container because it can store various types of objects like a container, in simple terms, vector is a dynamic array that can store any type of data, and can increase and compress data.

Vectors can store elements in contiguous memory locations and allocate memory at runtime as needed.

The difference between vectors (Vector) and arrays (array)

Arrays follow static methods, which means their size cannot be changed at runtime, while the vector implementation of dynamic arrays means that its size is automatically adjusted when elements are added.

syntax

Create vector 'v1'The syntax is:

vector<object_type> v1;

To use vector, you must include the following code in your header file:  

#include<vector>

Example

Let's see a simple example.

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<string> v1;
    v1.push_back("w3codebox");
    v1.push_back(".com");
    for(vector<string>::iterator itr=v1.begin();itr!=v1.end();++itr){
        cout<<*itr;
    }
    return 0; 
}

Output:

oldtoolbag.com

In this example, the Vector class is used to display strings.

C ++Vector function

Function
Description
at(idx)Returns the data pointed to by index idx, if idx is out of bounds, throws out_of_range.
back()Returns the last original, without checking if this data exists.
front()Returns the first element.
swap()Swaps two Vectors.
push_back()Adds an element at the end of the Vector.
pop_back()It removes the last element from the vector.
empty()Determines whether the Vector is empty (returns true if it is empty)
insert()It inserts a new element at the specified position.
erase()Deletes the specified element.
resize()It modifies the size of the vector.
clear()It removes all elements from the vector.
size()Returns the size of the Vector element count.
capacity()Returns the number of elements that the vector can hold (without reallocating memory)
assign()It assigns a new value to the vector.
operator=()It assigns a new value to the vector container.
operator[]()It accesses the specified element.
end()Returns an iterator to the last element (points to the next position of the last element)
emplace()It inserts a new element before the position pos.
emplace_back()It inserts a new element at the end.
rend()It points to the element before the first element of the vector.
rbegin()It points to the last element of the vector.
begin()Returns the iterator to the first element.
max_size()Returns the maximum number of elements that the Vector can hold (upper limit).
cend()It points to the 'last' in the vector.-last-element.
cbegin()It points to the first element of the vector.
crbegin()It refers to the last character of the vector.
crend()It refers to the elements before the first element of the vector.
data()It writes the data of the vector into an array.
shrink_to_fit()It reduces the capacity and makes it equal to the size of the vector.