1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#include <iostream>
#include <vector>
#include <array>
int main()
{
std::cout << "std::vector:\n";
const int arr_size { 5 };
// a compile-time sized std::vector is default zero-filled
std::vector<int> vec1(arr_size);
for (auto& itr : vec1) { std::cout << itr << ' '; }
std::cout << '\n';
// a vector's size and contents can be altered at run-time
vec1[2] = 44;
vec1.push_back(8);
vec1.insert(vec1.begin(), 22);
for (auto& itr : vec1) { std::cout << itr << ' '; }
std::cout << "\n\n";
// creating a compile time sized std::vector from an initializer list
std::vector<int> vec2 { 1, 2, 3, 4, 5 };
for (auto& itr : vec2) { std::cout << itr << ' '; }
std::cout << '\n';
vec2[2] = 44;
vec2.push_back(8);
vec2.insert(vec2.begin(), 22);
for (auto& itr : vec2) { std::cout << itr << ' '; }
std::cout << "\n\n";
// const std::vector<int> vec3(arr_size) { 1, 2, 3, 4, 5 }; // error!
const std::vector<int> vec3 { 1, 2, 3, 4, 5 };
for (auto& itr : vec3) { std::cout << itr << ' '; }
std::cout << '\n';
// can't alter the size or contents of a const std::vector
// vec3[2] = 44;
// vec3.push_back(8);
// vec3.insert(vec3.begin(), 22);
for (auto& itr : vec3) { std::cout << itr << ' '; }
std::cout << "\n\n";
std::cout << "std::array:\n";
std::array<int, arr_size> arr1;
for (auto& itr : arr1) { std::cout << itr << ' '; }
std::cout << '\n';
arr1[2] = 44;
for (auto& itr : arr1) { std::cout << itr << ' '; }
std::cout << "\n\n";
std::array<int, arr_size> arr2 { 1, 2, 3, 4, 5 };
for (auto& itr : arr2) { std::cout << itr << ' '; }
std::cout << "\n\n";
arr2[2] = 44;
for (auto& itr : arr2) { std::cout << itr << ' '; }
std::cout << '\n';
}
|