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() {
- 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;
- }
Advertisement
Comments
-
- template <typename Element, typename T>
- void Print(ostream &out, const map<Element, T> &container){
- int count = 1;
- for(const auto &[element,age] : container){
- out << "("s << element << ", " << age << ")";
- if(container.size() > count){
- out << ", "s;
- ++count;
- }
- }
- }
- template <typename Element>
- void Print(ostream &out, const Element &container){
- bool is_map = true;
- for(const auto &element : container){
- if(!is_map)
- out << ", "s;
- is_map = false;
- out << element;
- }
- }
- template <typename Element>
- ostream &operator<<(ostream &out, const vector<Element> &container){
- out << "["s;
- Print(out, container);
- out << "]"s;
- return out;
- }
- template <typename Element>
- ostream &operator<<(ostream &out, const set<Element> &container){
- out << "{"s;
- Print(out, container);
- out << "}"s;
- return out;
- }
- template <typename Element, typename T>
- ostream &operator<<(ostream &out, const map<Element,T> &container){
- out << "<"s;
- Print(out, container);
- out << ">"s;
- return out;
- }
- int main(){
- setlocale(LC_ALL, "");
- 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 <string>
-
- #include <iostream>
- #include <vector>
- #include <set>
- #include <map>
- using namespace std;
- template<typename key, typename value>
- void Print(ostream& out, const map<key, value>& container){
- bool first = true;
- for(const auto& [name, age]: container){
- if(!first){
- cout << ", ";
- }
- first = false;
- out << "(" << name << ", " << age << ")";
- }
- }
- template <typename T>
- void Print(ostream& out, const T& container ){
- bool first = true;
- for (const auto& element : container) {
- if(!first){
- out << ", ";
- }
- first = false;
- out << element;
- }
- }
- template <typename T>
- ostream& operator<<(ostream& out, const vector<T>& container) {
- out << "[";
- Print(out, container);
- out << "]";
- return out;
- }
- template <typename T>
- ostream& operator<<(ostream& out, const set<T>& 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() {
- 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;
- return 0;
- }
Add Comment
Please, Sign In to add comment