473,698 Members | 2,632 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 2611
"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
12241
by: enzo | last post by:
hi all, i don't understand what's wrong: 1) std::vector<double> p(10); doesn't compile:
20
17817
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
69678
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
2411
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>
0
8685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8880
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...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4373
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...
1
3053
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
3
2008
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.