I seem to remember, but cannot find, a tutorial posted here on how to use new features in C++ to print things more easily from cout.
It showed how to set it up so you can do "cout << x" on several common data structures (vectors, pairs, maps, etc.) and it would print them, e.g., a vector may print out as "32 12 45\n". Of course you would have to implement some code for each datatype you want.
If someone remembers this tutorial or can show how, I would appreciate greatly. Thanks!
You need
std::ostream& operator <<(std::ostream& output, const YourDataType& data)
to print data of YourDataType using cout.For example,
Can we also overload operator >> to directly take input in vector For example
so it will take input elements upto n values
Sure, exactly like in Actium's answer
but it shows error in my ide brother don't what's going wrong here.
It shows error like this
Read into variable means change that variable.
You have
const vector<T>& v
. You cannot change const variables and items of const vector.https://www.geeksforgeeks.org/operator-overloading-cpp-print-contents-vector-map-pair/ I found this article about printing a vector, map, and pairs using cout <<
See any of my submissions for overloading the cout on arrays, vectors, maps, custom classes... I've overloaded the cout class for practically all of std :p
Wow you really have a lot, thank you. I like your long list of "self-warning comments" even more — that's awesome :-)
I wouldn't understand why would you want to do this. For stl containers, just use a range-based for loop.
For vectors or sets (if vector name is v and set name is v)
For maps and pairs(mp is name for map and pair)