iterator

What's the problem here?
1
2
3
4
using namespace std;
main(){
std::istream_iterator<int,ptrdiff_t> streamIterator1(input), end1; 
}


Error:
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
  2 iterators (3).cpp
iterators.cpp(21): error C2664: 'std::istream_iterator<_Ty,_Elem>::istream_iterator(std::basic_istream<_Elem,_Traits> &)' : cannot convert parameter 1 from 'std::ifstream' to 'std::basic_istream<_Elem,_Traits> &'
          with
          [
              _Ty=int,
              _Elem=ptrdiff_t,
              _Traits=std::char_traits<ptrdiff_t>
          ]
          and
          [
              _Elem=ptrdiff_t,
              _Traits=std::char_traits<ptrdiff_t>
          ]
iterators.cpp(28): error C2664: 'std::istream_iterator<_Ty,_Elem>::istream_iterator(std::basic_istream<_Elem,_Traits> &)' : cannot convert parameter 1 from 'std::ifstream' to 'std::basic_istream<_Elem,_Traits> &'
          with
          [
              _Ty=int,
              _Elem=ptrdiff_t,
              _Traits=std::char_traits<ptrdiff_t>
          ]
          and
          [
              _Elem=ptrdiff_t,
              _Traits=std::char_traits<ptrdiff_t>
          ]


The original:
 
istream_iterator<int,ptrdiff_t> streamIterator1(input), end1; 

http://paste.ofcode.org/nunaqiJ6CytDsPRnZBRNHG
Last edited on
Either specify all the template parameters or just use default values for the last three:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <fstream>
#include <iterator>

int main()
{
    std::ifstream input( "input.txt" ) ;

    std::istream_iterator< int, char, std::char_traits<char>,
                           std::ptrdiff_t > streamIterator1(input), end1 ;

    // simpler: equivalent code using defaults
    std::istream_iterator<int> streamIterator2(input), end2 ;
}
OK, thanks! I did the change to specify all the parameters.

But I do not understand how this method works and why all this parameters are needed. Can you explain, please?
http://paste.ofcode.org/VStWXQjSXN7pNnuVFBdi6p

Especially what is the end1 and end2
Last
> But I do not understand how this method works and why all this parameters are needed.

All the parameters are not needed; except for the first, they have reasonable default values.

And you should be be using the defaults for the traits type and the distance type unless you have created a stream class of your own which uses some different types for these.

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
#include <iostream>
#include <iterator>
#include <sstream>

int main()
{
    {
        std::istringstream stm( "0 1 2 3 4 5 6 7 8 9" ) ;

        // istream iterator iterating over objects of type int
        // use defaults for the remaining three template parameters
        std::istream_iterator<int> iter(stm), end ;
        for( ; iter != end ; ++iter ) std::cout << *iter << ' ' ;
        std::cout << '\n' ;
    }

    {
        // istream iterator iterating over objects of type int
        // the character type of the stream is wchar_t
        // use defaults for the remaining two template parameters
        std::wistringstream stm( L"10 11 12 13 14 15 16 17 18 19" ) ;
        std::istream_iterator<int,wchar_t> iter(stm), end ;
        for( ; iter != end ; ++iter ) std::wcout << *iter << L' ' ;
        std::wcout << L'\n' ;
    }
}

http://coliru.stacked-crooked.com/a/e8db1011d1e8047d
But I still don't understand what does mean the "end" or "end1" in the
template<parameters> new_iterator_instance, end

I know the constructor is defined as:
1
2
3
4
5
istream_iterator(istream_type& _Istr)
		: _Myistr(&_Istr)
		{	// construct with input stream
		_Getval();
		}


But what is not clear to me is what is the "end" used to?
> But I still don't understand what does mean the "end" or "end1" in the


First read the section entitled 'Iterator Basics' here:
http://www.cs.helsinki.fi/u/tpkarkka/alglib/k06/lectures/iterators.html#iterator-basics

Then this:
The default-constructed std::istream_iterator is known as the end-of-stream iterator. When a valid std::istream_iterator reaches the end of the underlying stream, it becomes equal to the end-of-stream iterator. Dereferencing or incrementing it further invokes undefined behavior.
- http://en.cppreference.com/w/cpp/iterator/istream_iterator


And the answer would become obvious.

In std::istream_iterator<int> iter(stm), end ;

end is an iterator that is default-constructed; it signals end of stream.

To iterate over every element in the sequence, we move the iterator forward till it becomes equal to end.
for( ; iter != end ; ++iter ) { /* ... */ }

Once it becomes equal to end, it has gone past the last element in the sequence (here, it is at end-of-stream).
Thanks
Topic archived. No new replies allowed.