473,396 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

#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.

--
Simon Elliott http://www.ctsn.co.uk
Sep 21 '05 #1
8 2593
"Simon Elliott" <Simon at ctsn.co.uk> wrote in message
news:43***********************@news.gradwell.net
#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());

--
John Carson

Sep 21 '05 #2
Simon Elliott wrote:

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());
--
Karl Heinz Buchegger
kb******@gascad.at
Sep 21 '05 #3

"Simon Elliott" <Simon at ctsn.co.uk> wrote in message
news:43***********************@news.gradwell.net.. .
#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.

--
Simon Elliott http://www.ctsn.co.uk
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
Sep 21 '05 #4
Simon Elliott wrote:
#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
--
I'm not just a number. To many, I'm known as a string...
Sep 21 '05 #5
>
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


--
Karl Heinz Buchegger
kb******@gascad.at

Sep 22 '05 #6
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.
Sep 22 '05 #7
On 21/09/2005, John Carson wrote:
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());


.... 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.
--
Simon Elliott http://www.ctsn.co.uk
Sep 23 '05 #8
In message <43***********************@news.gradwell.net>, Simon Elliott
<Si***@at.ctsn.co.uk.invalid> writes
On 21/09/2005, John Carson wrote:
> 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());


... 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.

--
Richard Herring
Sep 26 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: enzo | last post by:
hi all, i don't understand what's wrong: 1) std::vector<double> p(10); doesn't compile:
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
3
by: DevNull | last post by:
I have a program where we load a mapfile, comprised of a .csv with numbers which represent object types at a given position. Each position is in the map is called a Cell, a cell contains only a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.