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
|
#include <iostream>
#include <vector>
#include <string>
template < typename T >
struct my_array {
std::vector<T> items;
auto begin() noexcept {
return items.begin();
}
auto end() noexcept {
return items.end();
}
auto begin() const noexcept {
return items.cbegin();
}
auto end() const noexcept {
return items.cend();
}
auto cbegin() const noexcept {
return items.cbegin();
}
auto cend() const noexcept {
return items.cend();
}
};
struct myType {
size_t n {};
std::string nam {};
};
int main() {
my_array<myType> arr {{ { 0, "zero"}, { 1, "one" }, { 2, "two" }, { 3, "three" }, { 4, "four" }, { 5, "five" }, { 6, "six" }}};
for (auto& [n, str] : arr)
std::cout << n++ << ". " << (str += '!') << '\n';
std::cout << '\n';
for (auto [n, str] : arr)
std::cout << n << ". " << str << '\n';
std::cout << '\n';
for (const auto& [n, str] : arr)
std::cout << n << ". " << str << '\n';
std::cout << '\n';
const my_array<myType> arrc {{ { 0, "zero"}, { 1, "one" }, { 2, "two" }, { 3, "three" }, { 4, "four" }, { 5, "five" }, { 6, "six" }}};
for (auto [n, str] : arrc)
std::cout << n << ". " << str << '\n';
std::cout << '\n';
for (const auto& [n, str] : arrc)
std::cout << n << ". " << str << '\n';
}
|