Iterators

From my book:

"Iterators are objects that behave like pointers and are very important for accessing the contents of all STL containers, except for those defi ned by a container adapter; container adapters do not support iterators. You can obtain an iterator from a container, which you can use to access the objects you have stored previously. You can also create iterators that will allow input and output of objects, or data items of a given type, from, or to, a C++ stream. Although basically all iterators behave like pointers, not all iterators provide the same functionality. However, they do share a base level of capability. Given two iterators, iter1 and iter2, accessing the same set of objects, the comparison operations iter1 == iter2, iter1 != iter2, and the assignment iter1 = iter2 are always possible, regardless of the types of iter1 and iter2."


What does it mean they share the base level of capability and then the equal part? Also if they access the same container won't they be the same type?

I got what iterators are, their objects (function objects?) that allow you to access a container. But what does my author mean in those last couple sentences?
Last edited on
If the two iterators to elements in the same container you can always compare them using == and !=, and assign them to each other.

Iterators can be grouped into categories depending on what you can do with them. std::forward_list<T>::iterator are forward iterators which means that you can use the iterator to go from one iterator to the next. std::list<T>::iterator are bidirectional iterators which means you can go in both directions (both back and forth). There are more iterator categories, that you can read about here: http://www.cplusplus.com/reference/iterator/#properties
Last edited on
Ooo ok got it thanks but what does it mean regardless the type of iter1 and iter2?

Wont they be the same if pointing to the same container?
They just mean that there is not type of iterator that you can't do this with.
O alright thanks!
> what does it mean regardless the type of iter1 and iter2?

All the iterators given out by standard containers fall under one of ForwardIterator, BidirectionalIterator, or RandomAccessIterator. All of these kinds of iterators are EqualityComparable.

A pure OutputIterator (for example a std::ostream_iterator<>) is not EqualityComparable; the above generalization does not apply to it.
Oh thats what it meant, I got it thanks!

btw my book says that:

"Input and output iterators"

Can also be used with comparison operations and the assignment operations.

Also what does that iterator do?
Small question about SMART iterators:

My book says:

"Visual C++ supports SCARY iterators, which in spite of the name, are nothing to be frightened of. SCARY is a strange acronym that is less than obvious, standing for “Seemingly erroneous (appearing Constrained by confl icting generic parameters), but Actually work with the Right implementation (unconstrained bY the confl icts due to minimized dependencies).” SCARY iterators are simply iterators that have a type that depends only on the type of element stored in a container, and not on other parameters used to instantiate a container from its template, such as the allocator and comparator types. In previous implementations of the STL, different containers created to store elements of a given type, but with different comparator or allocator types, would have iterators of
different types. There is no necessity for an iterator type to be dependent on the type of comparator or allocator used by a container. With the current implementation of the STL, the iterators will have the same type, just being determined by the element type. SCARY iterators can make the code faster and more compact."


What does it mean comparator or allocator types? Does it mean different comparator or allocators means that the container has a different type? And then you cant use the default iterator because it is a different "container type?" And SMART iterators implemented means that, iterators do not depend on the entire type, just the type of data stored? Or do I have this all wrong?


Last edited on
> my book says that: "Input and output iterators"
> Can also be used with comparison operations and the assignment operations.

If your book said that, it is wrong. An OutputIterator is not EqualityComparable. Both are CopyAssignable.


> My book says: "Visual C++ supports SCARY iterators ...

Ignore this completely for now; it talks about an implementation detail that does not, in any way, affect how you would use these iterators.

I just wanna know what it means by comparators of different types. Does it mean different comparators because obviously they cant be a different "type"

Even in this link: http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=454

This paragraph:

"Assume that C1 and C2 are different comparators, and that A1 and A2 are different allocator types. Now we're getting to the critical question: do vi1 and vi2 have different types of iterators too?"

By types it means different right? That's all I want to know. I understand what SCARY iterators are and stuff and why they are needed.


Also can you provide an example of a output iterator to confirm my book is wrong? Thanks!
Last edited on
> I just wanna know what it means by comparators of different types.

It means just what it says; the comparators in two different associative containers are of two different types.

1
2
3
int a = 7 ;
int b = 9 ;
long c = 12 ;

a and b are two different objects of the same type;
a and c are two different objects of different types.

> Also can you provide an example of a output iterator to confirm my book is wrong?

You could do that yourself; you are never going to learn swimming if all your learning just consists of watching others swim. If you want to be a programmer, start writing some code of your own.

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
#include <iostream>
#include <iterator>
#include <map>
#include <functional>
#include <type_traits>

int main()
{
    /////////////// scary iterators check /////////////////////////

    using map_type_one = std::map< int, char, std::less<int> > ;

    // container with same value_type etc, but different comparator
    using map_type_two = std::map< int, char, std::greater<int> > ;

    bool containers_are_of_same_type = std::is_same<map_type_one,map_type_two>::value ;
    std::cout << "containers are of same type? " << std::boolalpha
               << containers_are_of_same_type << '\n' ; // false

    using iter_type_one = map_type_one::iterator ;
    using iter_type_two = map_type_two::iterator ;

    // is iter_type_one the same as iter_type_two ?
    bool iterators_are_of_same_type = std::is_same<iter_type_one,iter_type_two>::value ;
    std::cout << "iterators are of same type? "
               << iterators_are_of_same_type << '\n' ; // true

    /////////////// OutputIterator EqualityComparable check /////////////////////////

    using iterator = std::ostream_iterator<char> ;
    iterator one(std::cout) ;
    iterator two(std::cout) ;
    one = two ; // fine: CopyAssignable
    // one == two ; // error: not EqualityComparable
}

http://ideone.com/lq1BxK
I tried but I couldn't figure out how to use output iterators, neither my book or that many online resources helped.
When I tried compiling it on Visual Studio 2012 Ultimate I got these errors:

1>------ Build started: Project: Tests, Configuration: Debug Win32 ------
1>  Ex9_13.cpp
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(11): error C2143: syntax error : missing ';' before '='
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(11): error C2873: 'map_type_one' : symbol cannot be used in a using-declaration
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(14): error C2143: syntax error : missing ';' before '='
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(14): error C2873: 'map_type_two' : symbol cannot be used in a using-declaration
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(16): error C2065: 'map_type_one' : undeclared identifier
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(16): error C2065: 'map_type_two' : undeclared identifier
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(16): error C2923: 'std::is_same' : 'map_type_one' is not a valid template type argument for parameter '_Ty1'
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(16): error C2923: 'std::is_same' : 'map_type_two' is not a valid template type argument for parameter '_Ty2'
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(16): error C2955: 'std::is_same' : use of class template requires template argument list
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtr1common(95) : see declaration of 'std::is_same'
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(20): error C2143: syntax error : missing ';' before '='
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(20): error C2873: 'iter_type_one' : symbol cannot be used in a using-declaration
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(20): error C2653: 'map_type_one' : is not a class or namespace name
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(21): error C2143: syntax error : missing ';' before '='
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(21): error C2873: 'iter_type_two' : symbol cannot be used in a using-declaration
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(21): error C2653: 'map_type_two' : is not a class or namespace name
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(24): error C2065: 'iter_type_one' : undeclared identifier
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(24): error C2065: 'iter_type_two' : undeclared identifier
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(24): error C2923: 'std::is_same' : 'iter_type_one' is not a valid template type argument for parameter '_Ty1'
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(24): error C2923: 'std::is_same' : 'iter_type_two' is not a valid template type argument for parameter '_Ty2'
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(24): error C2955: 'std::is_same' : use of class template requires template argument list
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtr1common(95) : see declaration of 'std::is_same'
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(30): error C2143: syntax error : missing ';' before '='
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(30): error C2873: 'iterator' : symbol cannot be used in a using-declaration
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(31): error C2065: 'iterator' : undeclared identifier
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(31): error C2146: syntax error : missing ';' before identifier 'one'
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(31): error C3861: 'one': identifier not found
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(32): error C2065: 'iterator' : undeclared identifier
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(32): error C2146: syntax error : missing ';' before identifier 'two'
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(32): error C3861: 'two': identifier not found
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(33): error C2065: 'one' : undeclared identifier
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_13\ex9_13.cpp(33): error C2065: 'two' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
An alternate version which VC++ will like a little better - using directives replaced with typedefs.

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
#include <iostream>
#include <iterator>
#include <map>
#include <functional>
#include <type_traits>

int main()
{
    /////////////// scary iterators check /////////////////////////

    typedef std::map<int, char, std::less<int>> map_type_one;
    // using map_type_one = std::map< int, char, std::less<int> > ;

    // container with same value_type etc, but different comparator
    typedef std::map<int, char, std::greater<int>> map_type_two ;
    // using map_type_two = std::map< int, char, std::greater<int> > ;

    bool containers_are_of_same_type = std::is_same<map_type_one,map_type_two>::value ;
    std::cout << "containers are of same type? " << std::boolalpha
               << containers_are_of_same_type << '\n' ; // false

    typedef map_type_one::iterator iter_type_one ;
    typedef map_type_two::iterator iter_type_two ;
    // using iter_type_one = map_type_one::iterator ;
    // using iter_type_two = map_type_two::iterator ;

    // is iter_type_one the same as iter_type_two ?
    bool iterators_are_of_same_type = std::is_same<iter_type_one,iter_type_two>::value ;
    std::cout << "iterators are of same type? "
               << iterators_are_of_same_type << '\n' ; // true

    /////////////// OutputIterator EqualityComparable check /////////////////////////

    typedef std::ostream_iterator<char> iterator ;
    // using iterator = std::ostream_iterator<char> ;
    iterator one(std::cout) ;
    iterator two(std::cout) ;
    one = two ; // fine: CopyAssignable
    // one == two ; // error: not EqualityComparable
}
Alright thanks guys.

One question about line 36 and 37. Why is the argument of type cout to ostream_iterator<char>?

So I can conclude from this that SCARY iterators are in VC++ and that my book was wrong.

Btw just to make its not that I interpreted it wrong can you read this from my book:


-----------------------------------------------

Iterators

Iterators are objects that behave like pointers and are very important for accessing the contents of all STL containers, except for those defi ned by a container adapter; container adapters do not support iterators. You can obtain an iterator from a container, which you can use to access the objects you have stored previously. You can also create iterators that will allow input and output of objects, or data items of a given type, from, or to, a C++ stream. Although basically all iterators behave like pointers, not all iterators provide the same functionality. However, they do share a base level of capability. Given two iterators, iter1 and iter2, accessing the same set of objects, the comparison operations iter1 == iter2, iter1 != iter2, and the assignment iter1 = iter2 are always possible, regardless of the types of iter1 and iter2.

Iterator Categories

There are four different categories of iterators; each category supports a different range of operations, as shown in the following table. The operations described for each category are in addition to the three operations that I mentioned in the previous paragraph."

-----------------------------------------------


And then it mentions all the iterators. So it is wrong based on this and I am not misinterpreting it right?
Last edited on
Topic archived. No new replies allowed.