C++ Vectors (std::vector)

std::vector is a dynamic array: it can grow and shrink. It is the container you should use most of the time. It is in the header <vector>.

Basics

#include <iostream>
#include <vector>

int main() {
    std::vector<int> marks = {70, 85, 90};

    marks.push_back(65);                // add at the end
    marks.insert(marks.begin() + 1, 99);   // add at a position
    std::cout << marks.size() << " items, first " << marks.front() << ", last " << marks.back() << '\n';

    marks.pop_back();                   // remove the last
    marks.erase(marks.begin());         // remove the first
    for (int m : marks) std::cout << m << ' ';
    std::cout << '\n';
    std::cout << marks[1] << ' ' << marks.at(1) << ' ' << marks.empty() << '\n';
}
Output

5 items, first 70, last 65
99 85 90
85 85 0

Create in different ways

#include <iostream>
#include <vector>

int main() {
    std::vector<int> a(5);              // five zeros
    std::vector<int> b(3, 7);           // three sevens
    std::vector<int> c{3, 7};           // two items: 3 and 7
    std::cout << a.size() << ' ' << b.size() << ' ' << c.size() << '\n';
    std::cout << b[0] << ' ' << c[0] << '\n';
}
Output

5 3 2
7 3

Careful: (3, 7) with round brackets means “3 items of value 7”, and {3, 7} with curly brackets means “the two items 3 and 7”.

Sort, search and sum

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

int main() {
    std::vector<int> v = {5, 2, 9, 1, 7};
    std::sort(v.begin(), v.end());
    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';

    std::cout << std::accumulate(v.begin(), v.end(), 0) << '\n';
    std::cout << *std::max_element(v.begin(), v.end()) << '\n';
    std::cout << (std::find(v.begin(), v.end(), 9) != v.end()) << '\n';
    std::reverse(v.begin(), v.end());
    std::cout << v.front() << '\n';
}
Output

1 2 5 7 9
24
9
1
9

Capacity and reserve

A vector keeps extra space, and grows when it is full (it copies all the items to a new place). If you know the size, call reserve.

#include <iostream>
#include <vector>

int main() {
    std::vector<int> v;
    v.reserve(100);
    std::cout << v.size() << ' ' << (v.capacity() >= 100) << '\n';
    for (int i = 0; i < 10; i++) v.push_back(i);
    std::cout << v.size() << ' ' << (v.capacity() >= 100) << '\n';
}
Output

0 1
10 1

Vector of vectors (2D)

#include <iostream>
#include <vector>

int main() {
    std::vector<std::vector<int>> grid(2, std::vector<int>(3, 0));
    grid[1][2] = 7;
    for (const auto& row : grid) {
        for (int v : row) std::cout << v << ' ';
        std::cout << '\n';
    }
}
Output

0 0 0
0 0 7

Invalid iterators

After push_back the old iterators, references and pointers into the vector can become invalid, because the data may move. Do not keep them.

Note: Use vector by default. Choose another container only when you have a reason. Next: C++ Functions.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.