"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?
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
> 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.
"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?
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"
"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!
> 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.
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 ==========
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?