On 2007-04-06 14:17, mikehul...@googlemail.com wrote:
Ok,
Imagine I have a class
class C
{
};
ostream& operator<<(ostream& o, const C& c) { ...}
I have a collection of these objects, in an STL list or vector and
want to do 2 things:
1/ Print all these objects to cout
2/ Write all these objects to a long string
Now, I wanted to try and use for_each and avoid my usual code of
Don't know about for_each but std::copy can be quite useful together
with ostream_iterator mentioned by Victor:
int main()
{
std::vector<int> vec;
for (int i = 0; i < 10; ++i)
vec.push_back(i);
std::stringstream ss;
std::copy(vec.begin(), vec.end(),
std::ostream_iterator<int>(ss, " "));
std::copy(vec.begin(), vec.end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n" << ss.str() << "\n";
return 0;
}
--
Erik Wikstr=F6m