Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <map>
- #include <vector>
- #include <utility> // std::pair, std::get
- /**
- * Возводим все в квадрат
- * vector, map, pair
- */
- template <class Type>
- Type Sqr(Type var) {
- return var * var;
- }
- template <class Type>
- std::vector<Type> Sqr(std::vector<Type> vec) {
- for (auto &elem : vec) {
- elem = Sqr(elem);
- }
- return vec;
- }
- template <class First, class Second>
- std::pair<First, Second> Sqr(std::pair<First, Second> pair) {
- std::get<0>(pair) = Sqr(std::get<0>(pair));
- std::get<1>(pair) = Sqr(std::get<1>(pair));
- return pair;
- }
- template <class First, class Second>
- std::map<First, Second> Sqr(std::map<First, Second> map) {
- for (auto &elem : map) {
- elem.second = Sqr(elem.second);
- }
- return map;
- }
- int main() {
- std::vector<int> v = {1, 2, 3};
- std::cout << "vector:";
- for (int x : Sqr(v)) {
- std::cout << ' ' << x;
- }
- std::cout << std::endl;
- std::map<int, std::pair<int, int>> map_of_pairs = {
- {4, {2, 2}},
- {7, {4, 3}}
- };
- std::cout << "map of pairs:" << std::endl;
- for (const auto& x : Sqr(map_of_pairs)) {
- std::cout << x.first << ' ' << x.second.first << ' ' << x.second.second << std::endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment