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,
Output [4, 2, 17, 11, 15]
Output [2, 4, 11, 15, 17]
Output a : 2 b : 3 d : 5
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.
This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- vector::operator= and vector::operator[ ] in C++ STL
- Operator Overloading '<<' and '>>' operator in a linked list class
- Rules for operator overloading
- Operator Overloading in C++
- Overloading Subscript or array index operator  in C++
- C++ | Operator Overloading | Question 10
- Overloading New and Delete operator in c++
- C++ Program to concatenate two strings using Operator Overloading
- Types of Operator Overloading in C++
- Increment (++) and Decrement (--) operator overloading in C++
- C++ program to compare two Strings using Operator Overloading
- Count number of Unique Triangles using Operator overloading
- Overloading the Comma Operator
- vector::front() and vector::back() in C++ STL
- vector::push_back() and vector::pop_back() in C++ STL
- vector::crend() & vector::crbegin() with example
- vector :: cbegin() and vector :: cend() in C++ STL
- How to flatten a Vector of Vectors or 2D Vector in C++
- vector::empty() and vector::size() in C++ STL
- vector::begin() and vector::end() in C++ STL