Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <set>
- #include <vector>
- using namespace std;
- template <typename First, typename Second>
- ostream& operator<<(ostream& out, const pair<First, Second>& p) {
- return out << '(' << p.first << ", "s << p.second << ')';
- }
- template <typename Container>
- void Print(ostream& out, const Container& container) {
- bool is_first = true;
- for (const auto& element : container) {
- if (!is_first) {
- out << ", "s;
- }
- is_first = false;
- out << element;
- }
- }
- template <typename Element>
- ostream& operator<<(ostream& out, const vector<Element>& container) {
- out << '[';
- Print(out, container);
- out << ']';
- return out;
- }
- template <typename Element>
- ostream& operator<<(ostream& out, const set<Element>& container) {
- out << '{';
- Print(out, container);
- out << '}';
- return out;
- }
- template <typename Key, typename Value>
- ostream& operator<<(ostream& out, const map<Key, Value>& container) {
- out << '<';
- Print(out, container);
- out << '>';
- return out;
- }
- int main() {
- setlocale(LC_ALL, "ru");
- const set<string> cats = { "Мурка"s, "Белка"s, "Георгий"s, "Рюрик"s };
- cout << cats << endl;
- const vector<int> ages = { 10, 5, 2, 12 };
- cout << ages << endl;
- const map<string, int> cat_ages = {
- {"Мурка"s, 10},
- {"Белка"s, 5},
- {"Георгий"s, 2},
- {"Рюрик"s, 12}
- };
- cout << cat_ages << endl;
- }
- /////////////////////////////////////////////////////////////////
- Мои попыточки
- #include <iostream>
- #include <vector>
- #include <map>
- #include <set>
- using namespace std;
- template <typename First, typename Second>
- ostream& operator<<(ostream& out, const pair<First, Second>& p) {
- return out << '(' << p.first << ", "s << p.second << ')';
- }
- //с const работать не будет!
- template <typename Container>
- ostream& operator<<(ostream& out, Container& container) {
- bool is_first = true;
- for (const auto& element : container) {
- if (!is_first) {
- out << ", "s; без s работать не будет!
- }
- is_first = false;
- out << element;
- }
- return out;
- }
- int main() {
- //для строк не работает
- const set<string> cats = { "Мурка"s, "Белка"s, "Георгий"s, "Рюрик"s };
- cout << cats << endl;
- const set<int> ages = { 10, 5, 2, 12 };
- cout << ages << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement