Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can I get a ptr to an element from an stl vector iterator?

Reply
Thread Tools

Can I get a ptr to an element from an stl vector iterator?

 
 
Steve555
Guest
Posts: n/a
 
      12-26-2008
Hi,

Given a struct:

struct myStruct{
int a;
float b;
};

and given a vector containing them, how do I get a ptr to one while
iterating through that vector:

myStruct *myStructPtr;
vector<myStruct>::iterator iter;
for(iter = myStructVec.begin(); iter != myStructVec.end(); ++ iter)
{
myStructPtr = iter; (???
ChangeStructValuesFunction( myStructPtr );
...
}

The goal is to modify a & b in-place. I'm doing this in a time-
critical loop, so I want to do it in place because with very big and
complicated structs or classes, it seems wasteful to have copy masses
of data in and out of the vector.

Thanks

Steve
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      12-26-2008
Steve555 wrote:
> Hi,
>
> Given a struct:
>
> struct myStruct{
> int a;
> float b;
> };
>
> and given a vector containing them, how do I get a ptr to one while
> iterating through that vector:
>
> myStruct *myStructPtr;
> vector<myStruct>::iterator iter;
> for(iter = myStructVec.begin(); iter != myStructVec.end(); ++ iter)
> {
> myStructPtr = iter; (???
> ChangeStructValuesFunction( myStructPtr );
> ...
> }
>
> The goal is to modify a & b in-place. I'm doing this in a time-
> critical loop, so I want to do it in place because with very big and
> complicated structs or classes, it seems wasteful to have copy masses
> of data in and out of the vector.
>

Why not just use the iterator?

iter->a = 42;

--
Ian Collins
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      12-26-2008
On Dec 26, 9:26 am, Steve555 <(E-Mail Removed)> wrote:
> Given a struct:


> struct myStruct{
> int a;
> float b;
> };


> and given a vector containing them, how do I get a ptr to one
> while iterating through that vector:


> myStruct *myStructPtr;
> vector<myStruct>::iterator iter;
> for(iter = myStructVec.begin(); iter != myStructVec.end(); ++ iter)
> {
> myStructPtr = iter; (???
> ChangeStructValuesFunction( myStructPtr );
> ...
> }


> The goal is to modify a & b in-place. I'm doing this in a
> time- critical loop, so I want to do it in place because with
> very big and complicated structs or classes, it seems wasteful
> to have copy masses of data in and out of the vector.


If you're interfacing legacy code to do the modifications,
&*iter will do the trick, but most of the time, either the
function should take a reference (so you pass *iter), or you
will be modifying the fields in place iter->fieldName.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      12-26-2008
Steve555 wrote:

> Hi,
>
> Given a struct:
>
> struct myStruct{
> int a;
> float b;
> };
>
> and given a vector containing them, how do I get a ptr to one while
> iterating through that vector:
>
> myStruct *myStructPtr;
> vector<myStruct>::iterator iter;
> for(iter = myStructVec.begin(); iter != myStructVec.end(); ++ iter)
> {
> myStructPtr = iter; (???
> ChangeStructValuesFunction( myStructPtr );
> ...
> }


You can obtain a pointer by taking the address of the pointee:

myStructPtr = &*iter;

> The goal is to modify a & b in-place.


If that is the goal, the signature of ChangeStructValuesFunction() should
be:

ChangeStructValuesFunction( myStruct & my_struct );

Then, you could just do:

ChangeStructValuesFunction( *iter );

[snip]


Best

Kai-Uwe Bux
 
Reply With Quote
 
Steve555
Guest
Posts: n/a
 
      12-26-2008
On 26 Dec, 09:53, Kai-Uwe Bux <(E-Mail Removed)> wrote:
> Steve555 wrote:
> > Hi,

>
> > Given a struct:

>
> > struct myStruct{
> > int * a;
> > float b;
> > };

>
> > and given a vector containing them, how do I get a ptr to one while
> > iterating through that vector:

>
> > myStruct **myStructPtr;
> > vector<myStruct>::iterator * iter;
> > for(iter = myStructVec.begin(); iter != myStructVec.end(); ++ iter)
> > *{
> > * * * * myStructPtr = iter; *(???
> > * * * *ChangeStructValuesFunction( myStructPtr );
> > * * * *...
> > }

>
> You can obtain a pointer by taking the address of the pointee:
>
> * myStructPtr = &*iter;
>
> > The goal is to modify a & b in-place.

>
> If that is the goal, the signature of ChangeStructValuesFunction() should
> be:
>
> * ChangeStructValuesFunction( myStruct & my_struct );
>
> Then, you could just do:
>
> * ChangeStructValuesFunction( *iter );
>
> [snip]
>
> Best
>
> Kai-Uwe Bux


Thanks for everyone's help.
Of course I should have tried &*iter, but I'd got side-tracked and
reasoned that as iter is a kind of pointer, I could just cast:
myStructPtr = (myStruct*)iter

I've modified my function as suggested to accept *iter, which is a
great new technique for me as I've been needlessly dereferencing
iters in many other situations.

Steve
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      12-26-2008
On Dec 26, 1:53*am, Kai-Uwe Bux <(E-Mail Removed)> wrote:
> Steve555 wrote:
> > Hi,

>
> > Given a struct:

>
> > struct myStruct{
> > int * a;
> > float b;
> > };

>
> > and given a vector containing them, how do I get a ptr to one while
> > iterating through that vector:

>
> > myStruct **myStructPtr;
> > vector<myStruct>::iterator * iter;
> > for(iter = myStructVec.begin(); iter != myStructVec.end(); ++ iter)
> > *{
> > * * * * myStructPtr = iter; *(???
> > * * * *ChangeStructValuesFunction( myStructPtr );
> > * * * *...
> > }

>
> You can obtain a pointer by taking the address of the pointee:
>
> * myStructPtr = &*iter;
>
> > The goal is to modify a & b in-place.

>
> If that is the goal, the signature of ChangeStructValuesFunction() should
> be:
>
> * ChangeStructValuesFunction( myStruct & my_struct );
>
> Then, you could just do:
>
> * ChangeStructValuesFunction( *iter );
>


always assuming, of course, that myStruct hasn't redefined operator&.
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      12-26-2008
red floyd wrote:

> On Dec 26, 1:53*am, Kai-Uwe Bux <(E-Mail Removed)> wrote:
>> Steve555 wrote:
>> > Hi,

>>
>> > Given a struct:

>>
>> > struct myStruct{
>> > int * a;
>> > float b;
>> > };

>>
>> > and given a vector containing them, how do I get a ptr to one while
>> > iterating through that vector:

>>
>> > myStruct **myStructPtr;
>> > vector<myStruct>::iterator * iter;
>> > for(iter = myStructVec.begin(); iter != myStructVec.end(); ++ iter)
>> > {
>> > myStructPtr = iter; *(???
>> > ChangeStructValuesFunction( myStructPtr );
>> > ...
>> > }

>>
>> You can obtain a pointer by taking the address of the pointee:
>>
>> myStructPtr = &*iter;


(***)

>> > The goal is to modify a & b in-place.

>>
>> If that is the goal, the signature of ChangeStructValuesFunction() should
>> be:
>>
>> ChangeStructValuesFunction( myStruct & my_struct );
>>
>> Then, you could just do:
>>
>> ChangeStructValuesFunction( *iter );
>>

>
> always assuming, of course, that myStruct hasn't redefined operator&.


(a) I think, this comment is misplaced. It should be put at the position
marked with (***), since operator& is inconsequential for whether

ChangeStructValuesFunction( *iter );

will work.


(b) Fortunately, we were given the definition of myStruct

Anyway, you are correct that an overloaded operator& can be a problem.
Instead of &*iter, one could always use address_of( *iter ) where:

// stolen from Boost:
template < typename T >
T * address_of ( T & t ) {
return (
reinterpret_cast<T*>(
& const_cast<char&>(
reinterpret_cast<const volatile char &>( t ) ) ) );
}

This might be a good idea for templated code that has to cope with user
defined types.


Best

Kai-Uwe Bux
 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      12-26-2008
Steve555 wrote:
> Of course I should have tried &*iter, but I'd got side-tracked and
> reasoned that as iter is a kind of pointer,


It is actually the other way round - a pointer is a kind of random
access iterator!

> I could just cast:
> myStructPtr = (myStruct*)iter


Casts are most often bad, and tells the compiler to "just shut up and
do it!". Whether it actually works or not...


Bo Persson


 
Reply With Quote
 
Steve555
Guest
Posts: n/a
 
      12-27-2008
On 26 Dec, 23:38, "Bo Persson" <(E-Mail Removed)> wrote:
> Steve555 wrote:
> > Of course I should have tried &*iter, but I'd got side-tracked and
> > reasoned that as iter is a kind of pointer,

>
> It is actually the other way round - a pointer is a kind of random
> access iterator!
>
> > I could just cast:
> > myStructPtr = (myStruct*)iter

>
> Casts are most often bad, and tells the compiler to "just shut up and
> do it!". Whether it actually works or not...
>
> Bo Persson


Quite... well learning the hard way is the best way!
Heh,heh, I want to edit my compiler resources so the "Build" button
becomes a "Just shut up and do it!" button
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      12-27-2008
On Dec 27, 6:28 am, Steve555 <(E-Mail Removed)> wrote:
> On 26 Dec, 23:38, "Bo Persson" <(E-Mail Removed)> wrote:


> Heh,heh, I want to edit my compiler resources so the "Build"
> button becomes a "Just shut up and do it!" button


I'm still looking for a system which has a "do what I meant, not
what I said" configuration option.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
is (!ptr) or (ptr) valid way to check for NULL or NOT NULL? G Fernandes C Programming 9 02-27-2005 03:07 AM
const ptr to const ptr ? franco ziade C Programming 3 02-17-2005 04:30 AM
How to convert a double **ptr in a double const ** const ptr? Heiko Vogel C Programming 3 09-14-2004 10:23 AM
what's the difference between delete ptr and ptr=0 -dont they accomplish the same Sid C++ 5 07-29-2004 03:42 AM



Advertisments