How it possible to use operator<<
to push string
s into a vector
. I searched a lot but only find stream examples.
class CStringData
{
vector< string > myData;
// ...
// inline operator << ... ???
};
I want this to use as a simple elipsis (like void AddData(...)
) exchange for
robust parameters.
CStringData abc;
abc << "Hello" << "World";
Is this possible at all ?
You can define
operator<<
as:Now you can write this:
But instead of making it member function, I would suggest you to make it
friend
ofCStringData
:The usage will be same as before!
To explore why should you prefer making it friend and what are the rules, read this:
You need to use std::vector.push_back() or std::vector.insert() to insert elements inside a vector.
Following piece of code appends to stream. similary you can add it to vector also.
as it is
template
so you can use it for other types also.