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
|
#include <iostream>
#include <vector>
#include <array>
template <typename CONTAINER, typename E> void store (const E&, CONTAINER&);
template <typename CONTAINER, typename E, std::size_t N> void store (const std::array<E, N>&, CONTAINER&);
template <typename CONTAINER, typename E> void store (const std::vector<E>&, CONTAINER&);
template <typename CONTAINER, typename E>
void store (const std::vector<E>& container, CONTAINER& storage) {
for (const auto& e : container)
store<CONTAINER> (e, storage);
}
template <typename CONTAINER, typename E, std::size_t N>
void store (const std::array<E, N>& container, CONTAINER& storage) {
for (const auto& e : container)
store<CONTAINER> (e, storage);
}
template <typename CONTAINER, typename E>
void store (const E& element, CONTAINER& storage) {storage.emplace_back(element);}
int main() {
std::vector<std::vector<std::vector<int>>> v = {
{ {1, 2}, {3, 4, 5} },
{ {6, 7}, {4, 8, 9, 10} },
{ {11, 12, 13}, {}, {14, 15, 16, 17} }
};
std::vector<int> storage;
store (v, storage);
for (int x : storage) std::cout << x << ' '; // 1 2 3 4 5 6 7 4 8 9 10 11 12 13 14 15 16 17
storage.clear(); std::cout << "\n------------------------------\n";
std::vector<std::array<std::vector<int>, 2>> a = {
{{ {1, 2}, {3, 4, 5} }},
{{ {6, 7}, {4, 8, 9, 10} }},
{{ {11, 12, 13}, {14, 15, 16, 17} }}
};
store (a, storage);
for (int x : storage) std::cout << x << ' '; // 1 2 3 4 5 6 7 4 8 9 10 11 12 13 14 15 16 17
}
|