Questions tagged [istream-iterator]

istream_iterator is an input iterator that reads successive elements from an input stream

istream_iterators require a template parameter this parameter is used to select the appropriate operator>> from the istream they are constructed with.

The istream_iterator::operator++ calls operator>>. Because the extraction is performed when the operator++ is used, the construction of an istream_iterator extracts a seed value from the istream so that it can immediately be read with the istream_iterator::operator*.

The default constructed istream_iterator is considered the "end-of-stream" iterator. Any istream_iterator of the same type which has reached the end of the istream it was constructed with will evaluate equal to the default constructed istream_iterator.

For more information: http://en.cppreference.com/w/cpp/iterator/istream_iterator

129 questions
27
votes
3 answers

Limiting the range for std::copy with std::istream_iterator

I have constructed a minimal working example to show a problem I've encountered using STL iterators. I'm using istream_iterator to read floatss (or other types) from a std::istream: #include #include #include int…
Flexo
  • 87,323
  • 22
  • 191
  • 272
27
votes
4 answers

std::istream_iterator<> with copy_n() and friends

The snippet below reads three integers from std::cin; it writes two into numbers and discards the third: std::vector numbers(2); copy_n(std::istream_iterator(std::cin), 2, numbers.begin()); I'd expect the code to read exactly two integers…
wilhelmtell
  • 57,473
  • 20
  • 96
  • 131
21
votes
1 answer

C++ istream_iterator is not a member of std

Can anyone tell me why the below piece of code I wrote when compiling keeps complaining istream_iterator is not a member of std please can you tell? Thanks guys #include #include #include #include #include…
Lexka
  • 359
  • 1
  • 3
  • 9
14
votes
5 answers

Multi stream iterators c++

The purpose of my program is to open a text file of m lines of the same length n, read the file column by column and print each column. For example, for this text file abcd efgh jklm I would like to print a e j b f k c g l d h m As one line…
B. Hel
  • 170
  • 10
13
votes
1 answer

Seeking istreambuf_iterator clarifications, reading a complete text file of Unicode characters

In the book “Effective STL” by Scott Meyers, there is a nice example of reading an entire text file into a std::string object: std::string sData; /*** Open the file for reading, binary mode ***/ std::ifstream ifFile (“MyFile.txt”,…
Chris Wiesner
  • 131
  • 1
  • 3
11
votes
2 answers

Why doesn't range-for find my overloads of begin and end for std::istream_iterator?

I have code like this std::ifstream file(filename, std::ios_base::in); if(file.good()) { file.imbue(std::locale(std::locale(), new delimeter_tokens())); for(auto& entry : std::istream_iterator(file)) { std::cout <<…
Veksi
  • 3,556
  • 3
  • 30
  • 69
10
votes
1 answer

What's the point of constexpr end istream (sentinel) iterators?

N2976 suggested adding constexpr to some spots in the standard library. It notes that iostreams are inappropriate for constexpr EXCEPT end iterators. So istream_iterator and istreambuf_iterator were given constexpr default constructors and that's…
user5353075
  • 101
  • 3
9
votes
3 answers

Run two s side by side on the same input iterator range

If I want to calculate the sum of a bunch of numbers retrieved from an std::istream, I can do the following: // std::istream & is = ... int total = std::accumulate(std::istream_iterator(is), …
isekaijin
  • 19,076
  • 18
  • 85
  • 153
8
votes
8 answers

How to read arbitrary number of values using std::copy?

I'm trying to code opposite action to this: std::ostream outs; // properly initialized of course std::set my_set; // ditto outs << my_set.size(); std::copy( my_set.begin(), my_set.end(), std::ostream_iterator( outs ) ); it should be…
Miro Kropacek
  • 2,742
  • 4
  • 26
  • 41
7
votes
2 answers

istream_iterator to iterate through bytes in a binary file

Given a file containing the following hex code: 0B 00 00 00 00 00 20 41 I'm trying to populate an std::vector and then checking each byte manually. Here's the code where I create my vector from two std::istream_iterators using the…
Burtonium
  • 101
  • 1
  • 5
7
votes
4 answers

Using istream_iterator and reading from standard input or file

I'm writing in Microsoft Visual C++ and I'd like my program to either read from standard input or a file using the istream_iterator. Googling the internets hasn't shown how simple I think it must be. So for example, I can write this pretty easily…
GrantJ
  • 8,162
  • 3
  • 52
  • 46
6
votes
1 answer

copy algorithm with ifstream

the following code is not behaving like I would expect. Please help me understand how it works. #include #include #include #include #include using namespace std; struct user { string name; …
flumpb
  • 1,707
  • 3
  • 16
  • 33
5
votes
1 answer

Can I use istream_iterator to copy some istream content into std::string?

I have an istream and need to copy the content between two delimiters to a std::string. I can find the delimiters' streampos, but when trying to use istream_iterator to iterate over the section of the stream, it does not work. Here's what I…
RL-S
  • 734
  • 6
  • 21
5
votes
2 answers

Using a regex_iterator on an istream

I want to be able to solve problems like this: Getting std :: ifstream to handle LF, CR, and CRLF? where an istream needs to be tokenized by a complex delimiter; such that the only way to tokenize the istream is to: Read it in the istream a…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
5
votes
3 answers

Some bytes are missing after reading an std::ifstream to a vector of lines

For copying what I read from input file to vector, I used std::copy() as recommended in Reading an std::ifstream to a vector of lines. The problem occurs if I use: std::copy(std::istream_iterator(inputfile), …
Kadir Erdem Demir
  • 3,531
  • 3
  • 28
  • 39
1
2 3
8 9