 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Stephen Vavasis Guest
|
Posted: Fri Mar 25, 2005 11:01 am Post subject: iterator points to entry of vector |
|
|
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
|
Posted: Sat Mar 26, 2005 4:33 am Post subject: Re: iterator points to entry of vector |
|
|
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
|
Posted: Sat Mar 26, 2005 4:34 am Post subject: Re: iterator points to entry of vector |
|
|
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
|
Posted: Sat Mar 26, 2005 4:35 am Post subject: Re: iterator points to entry of vector |
|
|
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
|
Posted: Sat Mar 26, 2005 4:48 am Post subject: Re: iterator points to entry of vector |
|
|
"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
|
Posted: Sat Mar 26, 2005 9:31 am Post subject: Re: iterator points to entry of vector |
|
|
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
|
Posted: Sat Mar 26, 2005 9:32 am Post subject: Re: iterator points to entry of vector |
|
|
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
|
Posted: Sat Mar 26, 2005 9:42 am Post subject: Re: iterator points to entry of vector |
|
|
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
|
Posted: Sat Mar 26, 2005 9:46 am Post subject: Re: iterator points to entry of vector |
|
|
"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
|
Posted: Sat Mar 26, 2005 5:24 pm Post subject: Re: iterator points to entry of vector |
|
|
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
|
Posted: Sat Mar 26, 2005 5:32 pm Post subject: Re: iterator points to entry of vector |
|
|
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
|
Posted: Sat Mar 26, 2005 5:39 pm Post subject: stl - containers |
|
|
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
|
Posted: Sun Mar 27, 2005 10:20 am Post subject: Re: stl - containers |
|
|
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
|
Posted: Sun Mar 27, 2005 10:24 am Post subject: Re: stl - containers |
|
|
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 |
|
 |
|
|
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
|
|