std::vector - works in BCB6 but not in g++

S

Simon Elliott

#include <vector>
#include <iostream>
int main (int argc, char *argv[])
{
std::vector<int> vi;
vi.push_back(1);
vi.push_back(2);
vi.push_back(3);
int* pi = vi.begin();
std::cout << "result:" << *pi << std::endl;
return(0);
}

This compiles and runs as I'd expect in Borland C++ Builder 6, but
gives the following error in g++:

test_vectors.cpp:9: error: cannot convert
`__gnu_cxx::__normal_iterator<int*, std::vector<int,
std::allocator<int> > >' to `int*' in initialization

I can't see where I'm going wrong here: I was under the impression that
random access iterators could be assigned to pointers.
 
J

John Carson

Simon Elliott said:
#include <vector>
#include <iostream>
int main (int argc, char *argv[])
{
std::vector<int> vi;
vi.push_back(1);
vi.push_back(2);
vi.push_back(3);
int* pi = vi.begin();
std::cout << "result:" << *pi << std::endl;
return(0);
}

This compiles and runs as I'd expect in Borland C++ Builder 6, but
gives the following error in g++:

test_vectors.cpp:9: error: cannot convert
`__gnu_cxx::__normal_iterator<int*, std::vector<int,
std::allocator<int> > >' to `int*' in initialization

I can't see where I'm going wrong here: I was under the impression
that random access iterators could be assigned to pointers.

Your impression was mistaken. vector iterators are sometimes implemented as
pointers so you can sometimes get away with it, but it is not guaranteed to
work. Try the following instead:

int* pi = &(*vi.begin());
 
K

Karl Heinz Buchegger

Simon said:
I can't see where I'm going wrong here: I was under the impression that
random access iterators could be assigned to pointers.

What gave you that impression?
An iterator is not a pointer. You *use* an iterator *as if* it
were a pointer, but it *is* an iterator and not a pointer.

int* pi = &(*vi.begin());
 
F

FabioAng

Simon Elliott said:
#include <vector>
#include <iostream>
int main (int argc, char *argv[])
{
std::vector<int> vi;
vi.push_back(1);
vi.push_back(2);
vi.push_back(3);
int* pi = vi.begin();
std::cout << "result:" << *pi << std::endl;
return(0);
}

This compiles and runs as I'd expect in Borland C++ Builder 6, but
gives the following error in g++:

test_vectors.cpp:9: error: cannot convert
`__gnu_cxx::__normal_iterator<int*, std::vector<int,
std::allocator<int> > >' to `int*' in initialization

I can't see where I'm going wrong here: I was under the impression that
random access iterators could be assigned to pointers.

Probably because std::vector::begin() doesn't return a pointer but an
iterator class
More precisely:

typedef __gnu_cxx::__normal_iterator<pointer, vector_type> iterator;

You should not assume that an iterator is a pointer but that it acts like a
pointer.

int main (int argc, char *argv[])
{
std::vector<int> vi;
vi.push_back(1);
vi.push_back(2);
vi.push_back(3);
std::vector<int>::iterator pi = vi.begin();
std::cout << "result:" << *pi << std::endl;
return(0);

Regards,
Fabio
 
B

Ben Pope

Simon said:
#include <vector>
#include <iostream>
int main (int argc, char *argv[])
{
std::vector<int> vi;
vi.push_back(1);
vi.push_back(2);
vi.push_back(3);
int* pi = vi.begin();
std::cout << "result:" << *pi << std::endl;
return(0);
}

This compiles and runs as I'd expect in Borland C++ Builder 6, but
gives the following error in g++:

test_vectors.cpp:9: error: cannot convert
`__gnu_cxx::__normal_iterator<int*, std::vector<int,
std::allocator<int> > >' to `int*' in initialization

I can't see where I'm going wrong here: I was under the impression that
random access iterators could be assigned to pointers.

Then you're under the wrong impression.

An iterator is an iterator, which in fact is a concept, not a type. It
has operations on it and corresponding semantics.

A pointer has the same operations and semantics, and can be used as an
iterator if your container is a C style array (among others), but that
does not mean that a char* is the same as a std::vector<char>::iterator.
In many implementations it is actually a typedef for char*, but not
necessarily so, it's implementation specific.

In much the same way a char* is not the same as an int*, a
std::vector<char>::iterator is not the same as a char*, in many cases
they are interchangable, but you are going against the type system, and
invoking at best implementation specific behaviour.

Stick to the correct types and you'll be fine.

Perhaps you meant to say:
int* pi = &(*(vi.begin()));

Or better:
std::vector<int>::iterator pi = vi.begin();

The reason is in the semantics. This is also how you implement smart
pointers... they behave like a pointer, in that you can dereference them
and get to the pointeee, but are clearly more than a pointer, and not
equivalent.

Hope that helps.

Ben
 
B

benben

What gave you that impression?
An iterator is not a pointer. You *use* an iterator *as if* it
were a pointer, but it *is* an iterator and not a pointer.

Should be rephrased as "An iterator is not necessarily a pointer, even
though a pointer is perfectly an iterator."
int* pi = &(*vi.begin());

Or better,
vector<int>::iterator pi = vi.begin();

Ben
 
F

Fraser Ross

I want a function that takes a parameter that is a pointer or iterator to
any character type but it will be accessed as unsigned char. I haven't
worked on this much yet. I am considering templates of course.

Fraser.
 
S

Simon Elliott

Your impression was mistaken. vector iterators are sometimes
implemented as pointers so you can sometimes get away with it, but it
is not guaranteed to work. Try the following instead:

int* pi = &(*vi.begin());

.... or even int* pi = &vi[0];

I was aware that iterators aren't always implemented as pointers, but I
thought that an operator T* (where T is the value_type) would always
need to be provided to allow the iterator to be used in all the
required forms. Evidently this isn't the case.
 
R

Richard Herring

Simon Elliott said:
Your impression was mistaken. vector iterators are sometimes
implemented as pointers so you can sometimes get away with it, but it
is not guaranteed to work. Try the following instead:

int* pi = &(*vi.begin());

... or even int* pi = &vi[0];

or even int* pi = &vi.front() ;
;-)
I was aware that iterators aren't always implemented as pointers, but I
thought that an operator T* (where T is the value_type) would always
need to be provided to allow the iterator to be used in all the
required forms. Evidently this isn't the case.

It's asking for trouble to provide conversion operators, except in rare
cases like proxy classes.

The requirement for types modelling the iterator concept is that they
should _behave like_ pointers, not _be_ pointers. What "behave like"
means here is that you can indirect through it using * or ->, so
providing appropriate operator-> and operator* is the natural solution,
and safer than a conversion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top