Leonid
Leonid

Reputation: 23490

Overloading output stream operator for vector<T>

What is a recommended way to overload the output stream operator? The following can not be done. It is expected that compilation will fail if the operator << is not defined for a type T.

template < class T >
inline std::ostream& operator << (std::ostream& os, const std::vector<T>& v) 
{
    os << "[";
    for (std::vector<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
    {
        os << " " << *ii;
    }
    os << " ]";
    return os;
}

EDIT: It does compile, the problem was unrelated and was in the namespace. Thanks for assistance.

Upvotes: 23

Views: 28928

Answers (6)

griloHBG
griloHBG

Reputation: 187

Here goes a contribution with an operator<< overload for std::vector example using for_each from the standard library algorithm (https://en.cppreference.com/w/cpp/algorithm/for_each).

#include <algorithm>
#include <vector>
#include <iostream>

template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {

    // printing the first element using at so it is bound checked
    // also, this helps to make the print prettier
    os << "{" << vec.at(0) << "}";

    // using for_each to go from second to last element of vec
    std::for_each(std::cbegin(vec)+1, std::cend(vec), [&](auto e){os << ", {" << e << "}";});

    return os;
}

int main() {
    std::vector<int> vec;

    vec.push_back(1);
    vec.push_back(2);

    std::cout << vec << std::endl;
}

Upvotes: 0

Rase
Rase

Reputation: 1

For people coming into this thread after C++11, use the ranged for loop to make the code easier to read.

template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
  for (const auto &x : v) {
    os << '[' << x << ']';
  }
  return os;
}

Upvotes: 0

smartnut007
smartnut007

Reputation: 6423

template<typename T>
std::ostream& operator<<(std::ostream& s, std::vector<T> t) { 
    s << "[";
    for (std::size_t i = 0; i < t.size(); i++) {
        s << t[i] << (i == t.size() - 1 ? "" : ",");
    }
    return s << "]" << std::endl;
}

Upvotes: 2

Nim
Nim

Reputation: 33655

Did you actually try this code? It works fine on gcc with a small tweak std::vector<T>::const_iterator, needs to be declared as typename std::vector<T>::const_iterator

You may be better off with using std::copy and std::ostream_iterator.

EDIT: types, dependent types and typename Can't fit it all in the comments, so here goes (btw. this is my understanding and I could be off by a country mile - if so please correct me!)...

I think this is best explained with a simple example..

Let's assume you have a function foo

template <typename T>
void foo()
{
  T::bob * instofbob; // this is a dependent name (i.e. bob depends on T)
};

Looks okay, and typically you may do this

class SimpleClass
{
  typedef int bob;
};

And call

foo<SimpleClass>(); // now we know that foo::instofbob is "int"

Again, seems self explanatory, however some nuser comes along and does this

class IdiotClass
{
  static int bob;
};

Now

foo<IdiotClass>(); // oops, 

What you have now is an expression (multiplication) as IdiotClass::bob resolves to a non-type!

To the human, it's obvious that this is stupid, but the compiler has no way of differentiating between types vs. non-types, and by default in C++ (and I think this is where compilers differ), all qualified dependent names (i.e. T::bob) will be treated as non-type. To explicitly tell the compiler that the dependent name is a real type, you must specify the typename keyword -

template <typename T>
void foo()
{
  typedef typename T::bob *instofbob; // now compiler is happy, it knows to interpret "bob" as a type (and will complain otherwise!)
};

This applies even if it is a typedef. i.e.

template <typename T>
void foo()
{
  typedef typename T::bob local_bob;
};

Is that any clearer?

Upvotes: 7

Jason Iverson
Jason Iverson

Reputation: 2640

This is what you want:

template < class T >
std::ostream& operator << (std::ostream& os, const std::vector<T>& v) 
{
    os << "[";
    for (typename std::vector<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
    {
        os << " " << *ii;
    }
    os << "]";
    return os;
}

You forgot the std:: on the first ostream

You put an extra space after [ in os << "[".

and you need typename before std::vector<T>::const_iterator

Upvotes: 16

Stephane Rolland
Stephane Rolland

Reputation: 39926

this compile for me on visual studio 2003. surely youshould use the keyword typename before the const std::vector<T> and I don't think the inline keyword has sense, IMHO templates are really close to inlining.

#include <ostream>
#include <vector>
#include <iostream>

template < class T >
std::ostream& operator << (std::ostream& os, typename const std::vector<T>& v) 
{
    os << "[ ";
    for (typename std::vector<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
    {
        os << " " << *ii;
    }
    os << "]";
    return os;
}

void Test()
{
    std::vector<int> vect;
    vect.push_back(5);
    std::cerr << vect;
}

Edit: I have added a typename also before the std::vector<T>::const_iterator as Nim suggested

Upvotes: 1

Related Questions