What is the difference between an array and a vector in C++?

PointC arraystd::arraystd::vector
SizeFixedFixedChanges at run time
Knows its sizeNoYesYes
MemoryStackStackHeap
Bounds check with at()NoYesYes
Add itemsNoNopush_back
#include <array>
#include <iostream>
#include <vector>

int main() {
    int c[3] = {1, 2, 3};
    std::array<int, 3> a = {1, 2, 3};
    std::vector<int> v = {1, 2, 3};
    v.push_back(4);
    std::cout << sizeof(c) / sizeof(c[0]) << ' ' << a.size() << ' ' << v.size() << '\n';
}
Output

3 3 4

See Arrays and Vectors.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.