Open In App
Related Articles

Operator overloading in C++ to print contents of vector, map, pair, ..

Improve Article
Improve
Save Article
Save
Like Article
Like

Operator overloading is one of the features of Object oriented programming which gives an extra ability to an operator to act on a User-defined operand(Objects).
We can take advantage of that feature while debugging the code specially in competitive programming. All we need to do is to overload the stream insertion operator(See this article to understand more) “<<" for printing the class of vector, map, set, pair etc. For instance,

Vector




// C++ program to print vector objects
// by overloading "<<" operator
#include <iostream>
#include <vector>
using namespace std;
  
// C++ template to print vector container elements
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "[";
    for (int i = 0; i < v.size(); ++i) {
        os << v[i];
        if (i != v.size() - 1)
            os << ", ";
    }
    os << "]\n";
    return os;
}
  
// Driver code
int main()
{
    vector<int> vec{ 4, 2, 17, 11, 15 };
  
    // Printing the elements directly
    cout << vec;
     
    return 0;
}


Output
[4, 2, 17, 11, 15]

Set




// C++ program to print set elements
// by overloading "<<" operator
#include <iostream>
#include <set>
using namespace std;
  
// C++ template to print set container elements
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v)
{
    os << "[";
    for (auto it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "]\n";
    return os;
}
  
// Driver code
int main()
{
    set<int> st{ 4, 2, 17, 11, 15 };
    cout << st;
    return 0;
}


Output
[2, 4, 11, 15, 17]

Map




// C++ program to print map elements
// by overloading "<<" operator
#include <iostream>
#include <map>
using namespace std;
  
// C++ template to print map container elements
template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v)
{
    for (auto it : v) 
        os << it.first << " : " 
           << it.second << "\n";
      
    return os;
}
  
// Driver code
int main()
{
    map<char, int> mp;
    mp['b'] = 3;
    mp['d'] = 5;
    mp['a'] = 2;
  
    cout << mp;
}


Output
a : 2
b : 3
d : 5

Pair




// C++ program to print pair<> class
// by overloading "<<" operator
#include <iostream>
using namespace std;
  
// C++ template to print pair<>
// class by using template
template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v)
{
    os << "(";
    os << v.first << ", " 
       << v.second << ")";
    return os;
}
  
// Driver code
int main()
{
    pair<int, int> pi{ 45, 7 };
    cout << pi;
    return 0;
}


Output
(45, 7)

As we can see from above, printing or debugging would become easier as we don’t need to write down the extra for loop or print statement. All we need to write the specific container name after the insertion operator “<<" of cout.
Exercise: Now let’s design your own template with operator overloading for other container class according to your requirement.

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 03 Sep, 2018
Like Article
Save Article
Previous
Next
Similar Reads