By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,246 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

istream_iterator<>

P: n/a
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::ostream_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
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Thu, 13 May 2004 18:54:36 GMT in comp.lang.c++, "NPC"
<eu*********@verizon.net> 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));
}

Jul 22 '05 #2

P: n/a
NPC

"David Harmon" <so****@netcom.com.invalid> wrote in message
news:40***************@news.west.earthlink.net...
On Thu, 13 May 2004 18:54:36 GMT in comp.lang.c++, "NPC"
<eu*********@verizon.net> wrote,
Is there any way to use an istream_iterator<> in a way which inserts eachelement 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.
Jul 22 '05 #3

P: n/a
On Fri, 14 May 2004 09:20:54 GMT, "NPC" <eu*********@verizon.net>
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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.