istream_iterator<>

N

NPC

Hi,
Is there any way to use an istream_iterator<> in a way which inserts each
element at the end of a newline character rather than a space character?
Could be it looks for any type of whitespace - not sure about that.

In particular, it would be nice to use it in a way which is similar to an
ostream_iterator:

std::vector<std::string> myVec;
/* add stuff to myVec */

/* add newline at the end of each element sent to cout */
std::copy(myVec.begin(), myVec.end(),
std::eek:stream_iterator<std::string>(std::cout, "\n"));

// wish I could....
myVec.clear();
std::istream_iterator<std::string> myFile("ok.txt", /* cannot do this */,
"\n");
std::istream_iterator<std::string> eof;
std::copy(myFile, eof, std::back_inserter(myVec));


Anyway to get the same affect using an istream_iterator? Perhaps through a
traits class? Not interested in a "getline" solution here --> specifically
looking to use istream_iterator.

Thanks,
NPC
 
D

David Harmon

On Thu, 13 May 2004 18:54:36 GMT in comp.lang.c++, "NPC"
Is there any way to use an istream_iterator<> in a way which inserts each
element at the end of a newline character rather than a space character?
Could be it looks for any type of whitespace - not sure about that.

istream_iterator just uses operator>> for whatever type.



struct by_line: std::string { };

std::istream & operator>> (std::istream &is, by_line &to_get)
{
std::getline(is, to_get);
return is;
}

int main()
{
std::ifstream in("datafile");
std::vector<std::string> v;
std::copy( std::istream_iterator<by_line>(in),
std::istream_iterator<by_line>(),
std::back_inserter(v));
}
 
N

NPC

David Harmon said:
On Thu, 13 May 2004 18:54:36 GMT in comp.lang.c++, "NPC"


istream_iterator just uses operator>> for whatever type.



struct by_line: std::string { };

std::istream & operator>> (std::istream &is, by_line &to_get)
{
std::getline(is, to_get);
return is;
}

int main()
{
std::ifstream in("datafile");
std::vector<std::string> v;
std::copy( std::istream_iterator<by_line>(in),
std::istream_iterator<by_line>(),
std::back_inserter(v));
}


Wow! That's gorgeous! That's been bugging me for way too long. Thank
you - very, very much David.
 
T

tom_usenet

Wow! That's gorgeous! That's been bugging me for way too long. Thank
you - very, very much David.

An alternative approach is to change the locale's definition of
whitespace so that operator>> for string does the right thing. That
approach has been detailed here before - try a google groups search
for e.g. "comma_ctype" or whitespace ctype facet. Posts by Dietmar
Kuehl are the ones to read.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top