C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

iterator points to entry of vector

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Stephen Vavasis
Guest





PostPosted: Fri Mar 25, 2005 11:01 am    Post subject: iterator points to entry of vector Reply with quote



In some C++ code that I wrote around 2000, I needed an iterator that points
to an entry of a vector. I used the following statement:
vector<T>::iterator i = &u[j];
where u is of type vector<T> and j is an int. Type T is a struct.

At that time, all the compilers I tried accepted this statement and produced
a program that ran correctly, so I didn't realize that the above statement
is perhaps nonconforming.

But gcc-3.2.2 is now rejecting that statement (giving an error message that
the type of the right-hand-side cannot be converted to the type of the
left-hand side), and I suspect that it may be nonconforming. Can someone
confirm that the above statement is nonconforming? What is the preferred
way to get the desired outcome?

-- Steve Vavasis



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Sat Mar 26, 2005 4:33 am    Post subject: Re: iterator points to entry of vector Reply with quote



In article <d1uurt$kfp$1 (AT) news01 (DOT) cit.cornell.edu>, Stephen Vavasis
<vavasis (AT) cs (DOT) cornell.edu> writes
Quote:
In some C++ code that I wrote around 2000, I needed an iterator that points
to an entry of a vector. I used the following statement:
vector<T>::iterator i = &u[j];
where u is of type vector<T> and j is an int. Type T is a struct.

I assume that u is a vector<T> then

vector<T>::iterator i(u.begin() + j);

would seem to do what you want.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Sascha Müller
Guest





PostPosted: Sat Mar 26, 2005 4:34 am    Post subject: Re: iterator points to entry of vector Reply with quote



Stephen Vavasis wrote:
Quote:
vector<T>::iterator i = &u[j];
Can someone
confirm that the above statement is nonconforming?

If u is a vector<T>, then u[j] returns a T and has no information about
the vector it came from.

Quote:
What is the preferred
way to get the desired outcome?

What about

vector<int>::iterator i = u.begin() + j;


Sascha Müller

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Torsten Robitzki
Guest





PostPosted: Sat Mar 26, 2005 4:35 am    Post subject: Re: iterator points to entry of vector Reply with quote

Stephen Vavasis wrote:

Quote:
In some C++ code that I wrote around 2000, I needed an iterator that points
to an entry of a vector. I used the following statement:
vector<T>::iterator i = &u[j];
where u is of type vector<T> and j is an int. Type T is a struct.

At that time, all the compilers I tried accepted this statement and produced
a program that ran correctly, so I didn't realize that the above statement
is perhaps nonconforming.

But gcc-3.2.2 is now rejecting that statement (giving an error message that
the type of the right-hand-side cannot be converted to the type of the
left-hand side), and I suspect that it may be nonconforming. Can someone
confirm that the above statement is nonconforming? What is the preferred
way to get the desired outcome?

You've assumed that vector::iterator have to be a pointer or to be
constrctable from a pointer. Just use u.begin() + j; for a vector or the
more generale form std::advance(u.begin(), j) for any container type.

regards
Torsten


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Bo Persson
Guest





PostPosted: Sat Mar 26, 2005 4:48 am    Post subject: Re: iterator points to entry of vector Reply with quote


"Stephen Vavasis" <vavasis (AT) cs (DOT) cornell.edu> skrev i meddelandet
news:d1uurt$kfp$1 (AT) news01 (DOT) cit.cornell.edu...
Quote:
In some C++ code that I wrote around 2000, I needed an iterator that
points
to an entry of a vector. I used the following statement:
vector<T>::iterator i = &u[j];
where u is of type vector<T> and j is an int. Type T is a struct.

At that time, all the compilers I tried accepted this statement and
produced
a program that ran correctly, so I didn't realize that the above
statement
is perhaps nonconforming.

But gcc-3.2.2 is now rejecting that statement (giving an error message
that
the type of the right-hand-side cannot be converted to the type of the
left-hand side), and I suspect that it may be nonconforming. Can
someone
confirm that the above statement is nonconforming? What is the
preferred
way to get the desired outcome?


The code is not conforming, but just happens to work in implementations
where the iterator is a typedef for T*. That is *one* possible way to
implement vector, but not the only one.

To get an iterator to an element j positions from the start of the
vector, you can use

v.begin() + j

That is guaranteed to work, as long as j is convertible to
vector<T>::size_type. :-)


Bo Persson



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Antoun Kanawati
Guest





PostPosted: Sat Mar 26, 2005 9:31 am    Post subject: Re: iterator points to entry of vector Reply with quote

Stephen Vavasis wrote:
Quote:
In some C++ code that I wrote around 2000, I needed an iterator that points
to an entry of a vector. I used the following statement:
vector<T>::iterator i = &u[j];
where u is of type vector<T> and j is an int. Type T is a struct.

&u[j] has the type T*, which may, or may not, be the same as
vector<T>::iterator.

Quote:
At that time, all the compilers I tried accepted this statement and produced
a program that ran correctly, so I didn't realize that the above statement
is perhaps nonconforming.

But gcc-3.2.2 is now rejecting that statement (giving an error message that
the type of the right-hand-side cannot be converted to the type of the
left-hand side), and I suspect that it may be nonconforming. Can someone
confirm that the above statement is nonconforming? What is the preferred
way to get the desired outcome?

vector<T>::iterator i = u.begin() + j;

--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Randy
Guest





PostPosted: Sat Mar 26, 2005 9:32 am    Post subject: Re: iterator points to entry of vector Reply with quote


Stephen Vavasis wrote:
Quote:
In some C++ code that I wrote around 2000, I needed an iterator that
points
to an entry of a vector. I used the following statement:
vector<T>::iterator i = &u[j];
where u is of type vector<T> and j is an int. Type T is a struct.

At that time, all the compilers I tried accepted this statement and
produced
a program that ran correctly, so I didn't realize that the above
statement
is perhaps nonconforming.

But gcc-3.2.2 is now rejecting that statement (giving an error
message that
the type of the right-hand-side cannot be converted to the type of
the
left-hand side), and I suspect that it may be nonconforming. Can
someone
confirm that the above statement is nonconforming? What is the
preferred
way to get the desired outcome?

-- Steve Vavasis




A vector iterator may, but is not required to, be a pointer, but &u[j]
is by definition a pointer. Use iterators instead:

vector<T>::iterator i = u.begin() + j;

Now everybody's happy. :-)

Randy.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Nemra®
Guest





PostPosted: Sat Mar 26, 2005 9:42 am    Post subject: Re: iterator points to entry of vector Reply with quote

Hello Stephen,

This is a common problem. Usually library developers just typedef-ed
the T* to be an iterator type, but in the latest realization it is a
new practice to provide a random-access-type iteration classes, which
in some cases (for example in release build) can still be replaced by
pointers. The purpose is to have more consistency checking in debug
mode.

Thus it is not supposed now and never was in standard that the return
type of the &u[j] can be an iterator of the same type. It was just an
implementation characteristics which made possible your example. You
have to replace it with:

vector<T>::iterator i = u.begin() + j;

Regards


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Maeder
Guest





PostPosted: Sat Mar 26, 2005 9:46 am    Post subject: Re: iterator points to entry of vector Reply with quote

"Stephen Vavasis" <vavasis (AT) cs (DOT) cornell.edu> writes:

Quote:
In some C++ code that I wrote around 2000, I needed an iterator that points
to an entry of a vector. I used the following statement:
vector<T>::iterator i = &u[j];
where u is of type vector<T> and j is an int. Type T is a struct.

At that time, all the compilers I tried accepted this statement and produced
a program that ran correctly, so I didn't realize that the above statement
is perhaps nonconforming.

But gcc-3.2.2 is now rejecting that statement (giving an error message that
the type of the right-hand-side cannot be converted to the type of the
left-hand side), and I suspect that it may be nonconforming. Can someone
confirm that the above statement is nonconforming?

Yes. I can.

&u[i] is of type T *, which isn't necessarily implicitly convertible
into vector<T>::iterator


Quote:
What is the preferred way to get the desired outcome?

vector<T>::iterator i(u.begin()+j);

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carl Barron
Guest





PostPosted: Sat Mar 26, 2005 5:24 pm    Post subject: Re: iterator points to entry of vector Reply with quote

In article <d1uurt$kfp$1 (AT) news01 (DOT) cit.cornell.edu>, Stephen Vavasis
<vavasis (AT) cs (DOT) cornell.edu> wrote:

Quote:
In some C++ code that I wrote around 2000, I needed an iterator that points
to an entry of a vector. I used the following statement:
vector<T>::iterator i = &u[j];
where u is of type vector<T> and j is an int. Type T is a struct.

At that time, all the compilers I tried accepted this statement and produced
a program that ran correctly, so I didn't realize that the above statement
is perhaps nonconforming.

nowhere did/does the standard requre or state tbat a

vector<T>::iterator and T * were the same type. In many early and some
'production' versions it is/was just a typedef. A way to do the above
that should work regardless if vector<...>::iterator is a typedefed
pointer or a special class. is:
vector<T>::iterator i = u.begin()+j;

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ali Çehreli
Guest





PostPosted: Sat Mar 26, 2005 5:32 pm    Post subject: Re: iterator points to entry of vector Reply with quote

Stephen Vavasis wrote:
Quote:
In some C++ code that I wrote around 2000, I needed an iterator that
points to an entry of a vector. I used the following statement:
vector<T>::iterator i = &u[j];
where u is of type vector<T> and j is an int. Type T is a struct.

That code used to work by an unlucky coincidence. In the library(ies) that
was compiled with, vector<T>::iterator was highly likely a typedef of a
pointer to the stored type (T*). But it never had to be that way.

Quote:
At that time, all the compilers I tried accepted this statement and
produced a program that ran correctly, so I didn't realize that the
above statement is perhaps nonconforming.

You've been unlucky not to discover the problem before so long.

Quote:
But gcc-3.2.2 is now rejecting that statement (giving an error
message that the type of the right-hand-side cannot be converted to
the type of the left-hand side), and I suspect that it may be
nonconforming.

vector::iterator can be obtained through the vector interface; anywhere else
may or may not work.

Quote:
Can someone confirm that the above statement is
nonconforming?

I don't think that the standard defines the behavior of that statement. It
may work as a result of implementation choices (as it did in the past for
you.)

Quote:
What is the preferred way to get the desired outcome?

For a random access iterator like vector<T>::iterator:

vector<T>::iterator i = u.begin() + j;

or more generally for any type of iterator, still at constant time for
random access iterators:

vector<T>::iterator i = u.begin();
advance(i, j);

Ali


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
JK
Guest





PostPosted: Sat Mar 26, 2005 5:39 pm    Post subject: stl - containers Reply with quote

question regarding stl containers... is there a container which would
allow me to search the container using the value field, not just the
key, e.g., i have a list of people names and phone numbers which i wanna
search using both the phone number and the person name? With maps, you
can only search using the key and not the value?
Thanks for any suggestions.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Stefan Strasser
Guest





PostPosted: Sun Mar 27, 2005 10:20 am    Post subject: Re: stl - containers Reply with quote

JK schrieb:
Quote:
question regarding stl containers... is there a container which would
allow me to search the container using the value field, not just the
key, e.g., i have a list of people names and phone numbers which i wanna
search using both the phone number and the person name? With maps, you
can only search using the key and not the value?
Thanks for any suggestions.


have a look at boost multiindex containers:
http://www.boost.org/libs/multi_index/doc/index.html

I've never used them so I don't know if it's possible to access the key
of one index by the key of another index but you could still also store
the keys inside the value.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carl Barron
Guest





PostPosted: Sun Mar 27, 2005 10:24 am    Post subject: Re: stl - containers Reply with quote

In article <pha1e.9339$e_7.7638 (AT) fe10 (DOT) lga>, JK <fakeemail (AT) yahoo (DOT) com>
wrote:

Quote:
question regarding stl containers... is there a container which would
allow me to search the container using the value field, not just the
key, e.g., i have a list of people names and phone numbers which i wanna
search using both the phone number and the person name? With maps, you
can only search using the key and not the value?
Thanks for any suggestions.

boost has a multi_index container. [www.boost.org], and other

goodies.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.