473,399 Members | 4,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

istream_iterator<>

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
3 2132
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Richo11 | last post by:
This is probably a silly question but I am learning sorry... I am trying to read in a series of strings from standard input using the istream_iterator member function, how does...
2
by: alberto | last post by:
I am learning STL with the book STL Tutorial and Reference guide (1 edition), the following example don't run : int main() { // Initialize array a with 10 integers: int a = {12, 3, 25, 7, 11,...
9
by: sshock | last post by:
Hi all, I want to read from a file into a vector<unsigned char>. Right now my code looks like this: FILE* f = fopen( "datafile", "rb" ); enum { SIZE = 100 }; vector<unsigned char>...
2
by: Ilkinho | last post by:
I`ve got class Stock and operator >> defined for it: istream &operator>>(istream &is, Stock& e){ is>>e.stockName>>e.producer>>e.date.m_day; return is; } Also class BunchOfStocks which...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
12
by: arnuld | last post by:
It works fine. any advice on making it better or if I can improve my C++ coding skills: /* C++ Primer - 4/e * * Chapter 9 - Sequential Containers * exercise 9.18 - STATEMENT * ...
10
by: arnuld | last post by:
It is quite an ugly hack but it is all I am able to come up with for now :-( and it does the requires work. I want to improve the program, I know you people have much better ideas ;-) /* C++...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.