diff --git a/seminar04_templates/06_pairing/main.cpp b/seminar04_templates/06_pairing/main.cpp new file mode 100644 index 0000000..bece096 --- /dev/null +++ b/seminar04_templates/06_pairing/main.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +using std::cout, std::endl, std::vector, std::pair; + +template +std::ostream& operator<<(std::ostream& out, const std::pair& p) +{ + out << "(" << p.first << ", " << p.second << ")"; + return out; +} + +template +std::ostream& operator<<(std::ostream& out, const std::vector& v) +{ + out << "["; + for (size_t i = 0; i < v.size() - 1; ++i) + out << v[i] << ", "; + + if (!v.empty()) + { + out << v.back(); + } + out << "]"; + return out; +} + +template +vector> pairing(T& xs) { + typedef typename T::value_type U; + vector> res; + + size_t size = xs.size(); + + for (int i = 0, n = (size % 2 ? size - 1: size); i < n; i += 2) + res.push_back(pair(xs[i], xs[i+1])); + if (size % 2) + res.push_back(pair(xs[size - 1], U())); + return res; +} + +int main() +{ + std::vector v {10, 20, 30, 40, 50, 60}; + cout << pairing(v) << endl; + + + std::array a {"cat", "dog", "mouse", "elephant", "tiget", "axolotl", "wolf"}; + cout << pairing(a) << endl; + + + std::string s {"Cats and dogs!"}; + cout << pairing(s) << endl; +} + + +/* + Программа должна напечатать: + + [(10, 20), (30, 40), (50, 60)] + [(cat, dog), (mouse, elephant), (tiget, axolotl), (wolf, )] + [(C, a), (t, s), ( , a), (n, d), ( , d), (o, g), (s, !)] +*/