The choice between std::vector
and std::array
depends on your specific requirements:
Use std::vector
 when:
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec{1, 2, 3};
vec.push_back(4);
std::cout << "Vector size: " << vec.size();
}
Vector size: 4
Use std::array
 when:
#include <array>
#include <iostream>
int main() {
std::array<int, 3> arr{1, 2, 3};
// arr.push_back(4); // Compile error
std::cout << "Array size: " << arr.size();
}
Array size: 3
In general, prefer std::array
for fixed-size arrays and std::vector
for dynamic arrays.
Answers to questions are automatically generated and may not have been reviewed.
A quick tour of ten useful techniques in C++, covering dates, randomness, attributes and more