| Point | C array | std::array | std::vector |
|---|
| Size | Fixed | Fixed | Changes at run time |
| Knows its size | No | Yes | Yes |
| Memory | Stack | Stack | Heap |
| Bounds check with at() | No | Yes | Yes |
| Add items | No | No | push_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';
}
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. |