Re: Input iterators?
On Apr 24, 10:32 pm, desktop <f...@sss.com> wrote:
Erik Wikstr=F6m wrote:
[...]
Ok I thought that a pointer or an iterator was just a number, but it
seems that the compiler treats them as different types:
At a certain level, everything is just a number (or a collection
of numbers). At that level, you're generally programming in
assembler. C++ is a typed language, and pointers are definitly
not numbers.
int myints[] = {1,2,3,4,5,1,2,3,4,5};
std::vector<int> myvector (myints,myints+10);
int match1[] = {1,2,3};
int ff = match1; // gives an error indicating that match1 is an int*
int gg = myvector.begin(); // gives an error indicating that
myvector.begin() is an iterator.
BTW: Why is this legal:
std::vector<int> myvector (myints,myints+10);
on this page:http://www.cppreference.com/cppvector/vector_constructors.ht=
ml
it should match:
vector( input_iterator start, input_iterator end );
but as just described 'myints' like 'match1' are int* and not
iterators.
Well, the template mechanism doesn't care. All it requires is
that the two parameters have the same type. The actual
implementation of vector supposes, however, that these two types
meet the constraints of an iterator, i.e. you can increment
them, dereference them, etc. And iterators (and their
constraints) were designed so that pointers meet these
constraints: a pointer acts like an iterator.
(Note that there is a very special case here, and something
like:
std::vector< int > v( 20, 42 ) ;
also invokes this constructor... which due to some tricky
template programming, doesn't treat the integers as iterators.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34