C++. The vector class. Dynamic array. General information

The vector class. Dynamic array. General information. Methods overview. Creating a dynamic array. Ways to access vector elements

This topic is the beginning of exploring the facilities of the vector class, which is a dynamic array.


Search other resources:

1. General information about the vector class. Overview of class methods

The vector class is a dynamic array that can be resized in the program as needed. This class is one of the most versatile and common to use when writing C++ programs. The elements of the class are accessed like a regular array using square brackets [ ].

To use the methods and constants of an array of vector type, you need to include the <vector> header and the std namespace

#include <vector>
using namespace std;

The template specification of the vector class is as follows:

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


  • T – type of array elements. It can be any base type (int, float, char, etc.) or a user-defined type;
  • Allocator – the name of the class that provides memory allocation.

Methods of the vector class can be conditionally divided into 3 groups:

  • 1. Methods that define and modify the general characteristics of the array
    • 1.1. Method size(). Determine the size of the vector
    • 1.2. Method max_size(). Maximum allowed array size
    • 1.3. Method capacity(). Determine the size of the array taking into account the reserved memory
    • 1.4. Method empty(). Determine if a vector is empty
    • 1.5. Method shrink_to_fit(). Align the size of the array, taking into account the reserved memory, to the actual size of the array
    • 1.6. Method reserve(). Reserve memory for additional array elements
    • 1.7. Method resize(). Resize the array
  • 2. Methods that modify (change) data in the array
    • 2.1. Method push_back(). Add an element to the end of the vector
    • 2.2. Method pop_back(). Remove the last element of the vector
    • 2.3. Method clear(). Removes all elements from the array.
    • 2.4. Method swap(). Swapping two vectors
    • 2.5. Assignment of arrays. Overloaded operator =
    • 2.6. Method erase(). Remove one or more elements of a specified range
    • 2.7. Method insert(). Inserts an element or group of elements into a vector
      • 7.1. Inserting an initialization list into a vector
      • 7.2. Inserting an element a specified number of times at a specified position
      • 7.3. Insert a single item at a given position
      • 7.4. Insert multiple items from a given range
    • Method assign(). Create array from existing data
  • 3. Methods that provide access to array elements
    • 3.1 Method at(). Get a vector element by its position (index)
    • 3.2. Method front(). Returns a reference to the first element of the vector
    • 3.3. Method back(). Returns a reference to the last element of the vector
    • 3.4. Method data(). Get a pointer to a vector
    • 3.5. Method begin(). Return an iterator pointing to the first element of the vector
    • 3.6. Method end(). Return an iterator pointing to the last element of an array
    • 3.7. Methods cbegin(), cend(). Get constant iterator to start and end of array
    • 3.8. Methods rbegin(), rend(). Access to array elements using a reverse iterator
    • 3.9. Methods crbegin(), crend​​(). Set a constant reverse iterator to the start and end of the array


2. Creating a dynamic array of vector type. Constructors. Example

To create an array, the vector class declares a number of constructors. Below are the most common ones.

To create an empty array, use the constructor:

explicit vector(const Allocator &a = Allocator());

To create an array that contains num elements with the value val, use the constructor

explicit vector(size_t num, const T &val = T(),
const Allocator &a = Allocator());

To create an array based on another ob array, you need to use the constructor

vector(const vector<T, Allocator> &ob);

To create an array based on the range pointed to by the start and end iterators, use the following constructor

template <class InIter> vector<InIter start, InIter end,
const Allocator &a = Allocator());

Example. The example creates various arrays of type vector.

#include <vector>
using namespace std;


// 1. Create a vector of 10 int numbers
vector<int> A1(10);

// 2. Create an empty vector whose elements are of type char
vector<char> A2;

// 3. Create a vector of 5 double numbers,
//    write the values 1.0 to the vector
vector<double> A3(5, 1.0);

// 4. Based on vector A3, create a new vector A4
vector<double> A4(A3);

// 5. Create a vector based on iterators that point to
//    the elements of vector A4 with indices from 0 to 1.
vector<double>::iterator start, end; // declare iterators
start = A4.begin();   // set the first iterator to position 0
end = A4.begin() + 2; // second iterator at position 2 (position after 1)
vector<double> A5(start, end); // create vector



3. Ways to access vector elements. Indexing [ ]

After creating a vector, you can access its elements in one of the following ways:

  • using the indexing operation [ ], as is the case with a regular array;
  • using an iterator;
  • using the at() method.

Examples of access to vector elements.

#include <vector>
using namespace std;


// 1. Accessing the vector using the indexer []
// 1.1. Create a vector of 5 int numbers
vector<int> A1(5);

// 1.2. Write to the vector the value [1, 2, 3, 4, 5]
for (int i = 0; i < A1.size(); i++)
  A1[i] = i + 1;

// 1.3. Display the vector on the screen
for (int i = 0; i < A1.size(); i++)
  cout << A1[i] << " ";
cout << endl;

// 2. Accessing elements of a vector using iterators
// 2.1. Create a vector of 5 floats
vector<float> A2(5);

// 2.2. Declare an iterator on a float vector
vector<float>::iterator p;

// 2.3. Write numbers into a vector [ 1.1, 2.2, 3.3, 4.4, 5.5 ] using iterator
p = A2.begin();
int i = 0;
while (p != A2.end())
  *p = (float)((i + 1) + (i + 1) * 0.1);

// 2.4. Output a vector using an iterator
p = A2.begin();
while (p != A2.end())
  cout << *p << " ";
cout << endl;

// 3. Accessing the elements of a vector using the at() method
// The at() method - get a vector element by its position
vector<double> A3(5); // Create array of 5 items of double type

// Fill with arbitrary values
A3.at(0) = 2.8; // A3[0] = 2.8;
A3.at(1) = 3.3; // A3[1] = 3.3;



Related topics