Click to See Complete Forum and Search --> : Vectors and Files


Justis
April 11th, 2002, 10:52 PM
hi.
i need to write a STL vector to a file, but i want to do that in a single line, that is, not by iterating the vector. is there some STL function enabling me to do so?
thanks in advance.

NMTop40
April 13th, 2002, 03:53 PM
yes there is. It is called ostream_iterator, and you use copy, thus:

std::copy( v.begin(), v.end(), std::ostream_iterator<T>( ofs, sep ) );




where v is your vector, ofs is your open output file stream, T is the type in your vector and sep is a string separator. (sep can definitely be char *. Maybe it can also be std::string but I'm not sure).

If ofs is a wide-character output stream, use ostream_iterator< T, wchar_t > and use a wide-string separator.



The best things come to those who rate