Vectors are data structures to store multiple elements of the same data type. Unlike an array, which is another data structure type, vectors can resize any time.

Declare the library

#include <vector>

Create a Vector

To create a vector, you use the vector keyword, and specify the type of values it should store inside the angle brackets <> followed by a name. You should name the vector like you were to name a variable.

vector<type> name;

For example,

vector<string> items;

Let’s add some elements to a vector. First, assign it a list wrapped in curly braces {} similar to arrays.

vector<string> items = {"apple", "hotdog", "water", "bread"}

Access a Vector

Accessing a vector is similar to arrays. Since vector begins with index 0, index 0 will be the first element. Using the previous example, index 0 is “apple”.

#include <vector>
#include <iostream>
using namespace std;
 
int main() {
	vector<string> items = {"apple", "hotdog", "water", "bread"}
	
	cout << items[0]; // Outputs apple
	
	cout << items[2]; // Outputs water
	return 0;
}
 

.at() function

vector.at() works similar to string.at() and indexing an element with []. One difference is that if you attempt to index an empty element, the function will throw an error.

Getting the first element

An alternative method of getting the first element is vector.front().

#include <vector>
#include <iostream>
using namespace std;
 
int main() {
	vector<string> items = {"apple", "hotdog", "water", "bread"}
	
	cout << items[0]; // Outputs apple
	
	cout << items.front(); // Outputs apple
	return 0;
}

Getting the last element

An alternative method of getting the last element is vector.back().

#include <vector>
#include <iostream>
using namespace std;
 
int main() {
	vector<string> items = {"apple", "hotdog", "water", "bread"}
	
	cout << items[items.size() - 1]; // Outputs bread
	
	cout << items.back(); // Outputs bread
	return 0;
}

Changing an element inside the vector

If you want to change an element inside the vector, you could refer to its index number:

#include <vector>
#include <iostream>
using namespace std;
 
int main() {
	vector<string> items = {"apple", "hotdog", "water", "bread"}
	
	items[1] = "sandwich";
	
	cout << items[1]; // Outputs sandwich
	return 0;
}

Alternatively, you could change the element with vector.at(index).

items.at(1) = "sandwich"

Adding elements

To add an element to an existing array, you use vector.push_back().

items.push_back("Hello");

Removing the last element

To remove the last element of the vector, you use vector.pop_back().

items.pop_back();

Checking the number of elements

To check the number of elements there are in a vector, you use vector.size().

vector<string> homework = {"math", "writing", "programming"}
 
cout << homework.size(); // Outputs 3

Checking the vector is empty

If you want to check the vector is empty, you use vector.empty().

cout << items.empty(); // Outputs false
// items vector is not empty if you look back at previous example
 
vector<string> random; // Same as vector<string> random = {};
cout << random.empty(); // Outputs true

Iterating a vector

There are two methods of iterating a vector. The first one is new in C++ (introduced in 2011) and becomes easy to read.

Note: Both examples run the same, but have different syntax.

New and efficient

The new method doesn’t provide the index of which the value comes from. In the example, the loop iterates four times (because there are four elements), but you only retrieve the values inside the loop where car is a string variable which is represented by one of the elements inside the vector cars.

vector<string> cars = {"lambourghini", "ferrari", "rollsroyce", "tesla"};
 
for (string car : cars) {
	cout << car << endl; // Or cout << car << "\n"
}

Old and traditional (most common)

The difference with this method is that you have access to the index of the element. Although it’s quite practical to run a code like this, you’d have to initialize a variable, set a condition to limit from going at the size of the vector, and increase the variable by one.

If you are asked about getting the index of which element or finding the index of something in the vector, you need to use this method

vector<string> cars = {"lambourghini", "ferrari", "rollsroyce", "tesla"};
 
for (size_t i = 0; i < cars.size(); i++) {
	cout << cars[i] << "\n";
}

Clearing the vector

If you want to clear/erase all the elements inside the vector, you use vector.clear().

vector<string> cars = {"lambourghini", "ferrari", "rollsroyce", "tesla"};
 
cars.clear();
 
cout << cars.empty(); // Outputs true