Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > istream_iterator<>

Reply
Thread Tools

istream_iterator<>

 
 
NPC
Guest
Posts: n/a
 
      05-13-2004
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: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


 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      05-14-2004
On Thu, 13 May 2004 18:54:36 GMT in comp.lang.c++, "NPC"
<> wrote,
> 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));
}

 
Reply With Quote
 
 
 
 
NPC
Guest
Posts: n/a
 
      05-14-2004

"David Harmon" <> wrote in message
news:...
> On Thu, 13 May 2004 18:54:36 GMT in comp.lang.c++, "NPC"
> <> wrote,
> > 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));
> }
>



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


 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      05-14-2004
On Fri, 14 May 2004 09:20:54 GMT, "NPC" <>
wrote:

>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
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57