royappa's blog

By royappa, history, 4 years ago, In English

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!

  • Vote: I like it
  • +5
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it +11 Vote: I do not like it

You need std::ostream& operator <<(std::ostream& output, const YourDataType& data) to print data of YourDataType using cout.

For example,

template <typename T>
std::ostream& operator <<(std::ostream& output, const std::vector<T>& data)
{
    for (const T& x : data)
        output << x << " ";
    return output;
}
  • »
    »
    2 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Can we also overload operator >> to directly take input in vector For example

    vector<int> v(n,0);
    cin >> v;
    

    so it will take input elements upto n values

    • »
      »
      »
      2 years ago, # ^ |
        Vote: I like it +13 Vote: I do not like it

      Sure, exactly like in Actium's answer

      template<typename T>
      std::istream& operator>>(std::istream& input, std::vector<T>& data) {
          for (auto& item : data) {
              input >> item;
          }
          return input;
      }
      
      • »
        »
        »
        »
        2 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        but it shows error in my ide brother don't what's going wrong here.

        #include <bits/stdc++.h>
        using namespace std;
        
        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;
        }
        
        template <typename T>
        istream& operator >> (istream& is,const vector<T>& v){
            for(auto &item:v)
                is >> item;
            return is;
        }
        
        
        int main() {
            vector<int> v(5,0);
            cin >> v;
            cout << v;
            return 0;
        }
        

        It shows error like this

        prog.cpp: In instantiation of ‘std::istream& operator>>(std::istream&, const std::vector<_Tp>&) [with T = int; std::istream = std::basic_istream<char>]’:
        prog.cpp:27:12:   required from here
        prog.cpp:20:12: error: no match for ‘operator>>’ (operand types are ‘std::istream {aka std::basic_istream<char>}’ and ‘const int’)
                 is >> item;
                 ~~~^~~~~~~
        
        • »
          »
          »
          »
          »
          2 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Read into variable means change that variable.

          You have const vector<T>& v. You cannot change const variables and items of const vector.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 <<

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    Wow you really have a lot, thank you. I like your long list of "self-warning comments" even more — that's awesome :-)

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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(auto& i: v) {
    cout << i << ' ';
}

For maps and pairs(mp is name for map and pair)

for(auto& i: mp) {
    cout << i.first << ' ' << i.second << endl;
}