C++ Vectors
A C++ vector is an STL container that stores an ordered list of values of the same type. Unlike a built-in array, a std::vector can grow or shrink while the program runs.
Vectors are one of the most commonly used containers in C++ because they are flexible, efficient, and easy to loop through.
Including the Vector Header
To use vectors, include the <vector> header. A vector type is written as std::vector<T>, where T is the type of value stored in the vector.
#include <iostream>
#include <vector>
int main(void) {
std::vector<int> scores = {90, 85, 100};
std::cout << scores[0] << std::endl;
std::cout << scores[1] << std::endl;
std::cout << scores[2] << std::endl;
return 0;
}
Output:
90
85
100
This creates a vector named scores that stores int values. Like arrays and strings, vector indexes start at 0.
Adding Elements
The push_back() function adds a new element to the end of a vector. This is useful when you do not know all the values when the vector is first created.
#include <iostream>
#include <string>
#include <vector>
int main(void) {
std::vector<std::string> names;
names.push_back("Ada");
names.push_back("Bjarne");
names.push_back("Grace");
for (std::string name : names) {
std::cout << name << std::endl;
}
return 0;
}
Output:
Ada
Bjarne
Grace
The vector starts empty. Each call to push_back() appends one string, so the values stay in the order they were added.
Size and Index Access
The size() function returns the number of elements in a vector. You can use square brackets to access an element by index, or use at() when you want bounds checking.
#include <iostream>
#include <vector>
int main(void) {
std::vector<double> prices = {2.50, 4.00, 3.25};
prices[1] = 4.50;
std::cout << "Items: " << prices.size() << std::endl;
std::cout << "First: " << prices.at(0) << std::endl;
std::cout << "Second: " << prices[1] << std::endl;
return 0;
}
Output:
Items: 3
First: 2.5
Second: 4.5
prices[1] = 4.50; changes the second element. The expression prices.size() tells you how many elements are currently stored.
Looping Through a Vector
A range-based for loop is a clean way to read each value in a vector. Use a regular index-based for loop when you need the element position.
#include <iostream>
#include <vector>
int main(void) {
std::vector<int> points = {5, 10, 15, 20};
int total = 0;
for (int point : points) {
total += point;
}
for (int i = 0; i < points.size(); i++) {
std::cout << "Point " << i << ": " << points[i] << std::endl;
}
std::cout << "Total: " << total << std::endl;
return 0;
}
Output:
Point 0: 5
Point 1: 10
Point 2: 15
Point 3: 20
Total: 50
The first loop adds the values. The second loop prints each index and value. In larger programs, you will often use vector indexes when the position has meaning.
Common Vector Functions
| Function | What it does |
|---|---|
push_back(value) |
Adds a value to the end |
size() |
Returns the number of elements |
empty() |
Returns whether the vector has no elements |
at(index) |
Accesses an element with bounds checking |
clear() |
Removes all elements |
Vectors vs Arrays
Use a vector when the number of elements may change, when you want convenient functions like push_back() and size(), or when you are working with STL algorithms. Use a built-in array only when you need a fixed-size group of values and the size is known at compile time.
Takeaway: std::vector is the standard C++ choice for a resizable, ordered list of same-type values.
