473,888 Members | 1,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.cp p:9: error: cannot convert
`__gnu_cxx::__n ormal_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 2621
"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.cp p:9: error: cannot convert
`__gnu_cxx::__n ormal_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.cp p:9: error: cannot convert
`__gnu_cxx::__n ormal_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::be gin() doesn't return a pointer but an
iterator class
More precisely:

typedef __gnu_cxx::__no rmal_iterator<p ointer, 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.cp p:9: error: cannot convert
`__gnu_cxx::__n ormal_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<cha r>::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<cha r>::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>::it erator 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************ ***********@new s.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
12272
by: enzo | last post by:
hi all, i don't understand what's wrong: 1) std::vector<double> p(10); doesn't compile:
20
17877
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; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
32
69732
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: std::vector<int> fun(){ //build the vector v; return v; }
3
2429
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 position struct with it's own x,y and an int called CellType which determines if the Cell is a solid i.e. a wall, or a nonsolid, i.e. empty space. Given... <code>
1
10886
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10439
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9597
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7148
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5824
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6014
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4245
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3252
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.