How to get the os<< operator to output a vector from a class?


What code would I need inside the << function to display the contents of
both vectors, names and sorted_ages?

//==============================================================================
class Name_pairs
{
public:
//....



const vector<string> & Names ()const {return names; }
const vector<double> & Ages ()const {return ages; }
const vector<double> & Sorted_ages ()const {return sorted_ages;}


private:

//...
vector<string> names;
vector<double> sorted_ages;
};

//==============================================================================

ostream& operator<<(ostream& os, const Name_pairs & n)
{

return os;
}
I think you should overload operator<< inside your function... something like

1
2
3
4
5
6
7
void Name_pairs::operator<< ()
{
     for(unsigned long long i = 0; i < names.size(); i++)
     {
          cout << names[i] << ' ' << sorted_ages[i] << endl;
     }
}


that's assuming both vectors have the same size of course, and you could call it like this

1
2
3
4
5
6
7
8
int main()
{
     Name_pairs myNamePairs;
     (...)
     myNamePairs.operator<< ();
     (...)
     return 0;
}


but why overload an operator when you could just do like a void output() function that does exacly the same thing?

You call it like that myNamePairs.output();
Friends
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
class Foo {
   public:
     Foo(int val):x(val){}
   //
   // Your friends can see your privates!
   //
   friend std::ostream &operator<<(std::ostream &os, const Foo &);
   private:
     int x;
};
std::ostream &operator<<(std::ostream &os, const Foo &obj){
   os << obj.x;
   return os;
}
int main() {
    Foo foo(123456789);
    std::cout << foo << std::endl;
    return 0;
}

$ ./a.out
123456789
$
What code would I need inside the << function to display the contents of
both vectors, names and sorted_ages?


http://www.parashift.com/c++-faq/output-operator.html

First of all the overloaded operator<< should be a friend of class Name_pairs.

After that, simply iterate over the vectors and output their elements to os.

1
2
3
4
5
6
7
8
for (std::vector<std::string>::const_iterator ci = n.names.begin(); ci != n.names.end(); ++ci)
    os << *ci << ' ';

// or if your compiler supports C++11 range-based for() loops
// (Visual Studio 2013, GNU GCC 4.6+)

for (const std::string &s: n.names)
    os << s << ' ';


@ AeonFlux1212: while a printOn() function is a good approach too, your operator<< snippets leave me surprised, what on Earth are you doing?

http://www.parashift.com/c++-faq/output-operator-via-friends.html
@Catfish666 It's an exercise from Bjarne Stroustrup's text book, chapter 9.

I think writing a function is easier, but the exercise calls for overloading <<, which is weird, cause the chapter only shows you how to overload << for use with built in class members that are integers.
I think writing a function is easier, but the exercise calls for overloading <<, which is weird, cause the chapter only shows you how to overload << for use with built in class members that are integers.

Not so weird. The process is the same for other types.

Note also that the overloaded operator does not need to be a friend function in this case, since you do not need access to private members to do what you want.
1
2
3
4
5
6
ostream& operator<<(ostream& os, const Name_pairs & n)
{
    for ( std::size_t i = 0; i<n.Names().size(); ++i )
        os << n.Names()[i] << ", " << n.Sorted_ages()[i] << '\n' ;
    return os;
}


The fact that you have ages and sorted_ages data members is a little confusing to me.
@catfish666 I tought he could overload the << operator from within the class and have it output what he wanted, but I didn't really see the purpose of that so I suggest him a simple void print function... I might also add that streams are not my area of expertise :-)
Last edited on
Topic archived. No new replies allowed.