Topic: How to point to elements in a vector?
Author: david@unico.com.au (David Goh)
Date: 1998/01/22 Raw View
In comp.lang.c++, on 19 Jan 1998 17:17:28 PST
JiJun <jj@et.dtu.dk> wrote:
>Dear STL experts,
> I got a question for you: I use a vector of objects in my program,
> and there are some pointers outside the vector point to some of the
> objects in this vector by assign my pointer like:
> TMyObject *= vector <TMyObject>::iterator FindObject();
Urgh. Um.
I *assume* you mean you're doing something like:
... header file ...
vector <TMyObject>::iterator FindObject(TMyKey something);
... code file ...
TMyKey bar(42);
TMyObject * foo = FindObject(bar);
This is... not good. AFAIK, vector<TMyObject>::iterator is *not*
guaranteed by the standard to be (TMyObject *), nor is it guaranteed
to be convertible to (TMyObject *).
It's quite *likely* that it is, but it's *not* necessarily so.
> everything is Ok as long as I don't modify the vector...
That's to be expected...
> But once I add new element to the vector by calling push_back(), my
> former assigned pointers will desperately lose their targets.:-)
> According to STL documents, that's because the vector is reallocated
> and all iterators are invalidated ..
Exactly. Most STL containers (I seem to recall a discussion which
suggested that list<> was about the only exception) have *all* iterators
become invalid as soon as any insert/delete/etc operation is performed
on them.
> Is there any way I could keep my pointers allways point to the right
> objects? (maybe not:-) , or any other suggestions... need your
> help desperately!
Right. :) A couple of possible solutions come to mind...
Store your objects in a map<TMyKey, TMyObject, less<TMyKey> > structure instead, and
instead of returning a vector<TMyObject>::iterator, have FindObject
return a TMyObject &...
hence:
map<TMyKey, TMyObject, less<TMyKey> > internal_map;
TMyObject & FindObject(TMyKey key)
{
return internal_map[key];
}
Then always refer to TMyObjects by using their key, and don't store the
reference except while you expect the map to become unchanged.
The other solution...
Store your objects in the vector with a ReferenceCountingPtr<> type
(It shouldn't be too hard to write a simple one, or find sample code
for an existing one somewhere), and have FindObject() return a
ReferenceCountingPtr<> to you. Then you can use that, instead of an
iterator, or the naked pointer itself.
Later,
David
--
| david@unico.com.au (David Goh, Unico Computer Systems, +61-3-9866-5688)
You have an unusual magnetic personality. Don't walk too close to
metal objects which are not fastened down.
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "Glen Parker" <_no_spam_please_glenebob@techie.com>
Date: 1998/01/23 Raw View
[Moderator's note: this thread is drifting away from C++
standardization issues. I have set followups to go by default
to comp.lang.c++ only. mha]
> I got a question for you:
> I use a vector of objects in my program, and there are some pointers
outside
>the vector point to some of the objects in this vector by assign my pointer
like:
>TMyObject *= vector <TMyObject>::iterator FindObject();
>
> everything is Ok as long as I don't modify the vector...
>
> But once I add new element to the vector by calling push_back(), my
former
>assigned pointers will desperately lose their targets.:-) According to STL
documents,
>that's because the vector is reallocated and all iterators are invalidated
..
>
> Is there any way I could keep my pointers allways point to the right
objects?
>(maybe not:-) , or any other suggestions... need your help desperately!
Could you just reference the objects through the vector offset?
FindObject() could return an offset rather than an iterator. That way,
push_back won't throw you off. push_front will of course ;-)... I'm sure
you could come up with a way to keep your offsets correct.
Glen
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: JiJun <jj@et.dtu.dk>
Date: 1998/01/19 Raw View
[Moderator's note: this article is crossposted to several newsgroups,
one of which is comp.std.c++. If you respond to this article, please
make sure that your response is appropriate to the newsgroups(s) that
you post to. mha]
This is a multi-part message in MIME format.
--------------F0F5CD130D52BA0C6BCFDE06
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Dear STL experts,
I got a question for you:
I use a vector of objects in my program, and there are some pointers outside
the vector point to some of the objects in this vector by assign my pointer like:
TMyObject *= vector <TMyObject>::iterator FindObject();
everything is Ok as long as I don't modify the vector...
But once I add new element to the vector by calling push_back(), my former
assigned pointers will desperately lose their targets.:-) According to STL documents,
that's because the vector is reallocated and all iterators are invalidated ..
Is there any way I could keep my pointers allways point to the right objects?
(maybe not:-) , or any other suggestions... need your help desperately!
John (also welcome to email to john.chou@bigfoot.com)
--------------F0F5CD130D52BA0C6BCFDE06
Content-Type: text/x-vcard; charset=us-ascii; name="vcard.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for JiJun Zhou
Content-Disposition: attachment; filename="vcard.vcf"
begin: vcard
fn: JiJun Zhou
n: Zhou;JiJun
org: Dept. of Energy Engineering, DTU
adr: Technical University of Denmark;;Building 402;Lyngby;;DK2800;Denmark
email;internet: jj@et.dtu.dk
tel;work: +45 45254031
note: http://www.geocities.com/CollegePark/Quad/8054/
x-mozilla-cpt: ;0
x-mozilla-html: FALSE
version: 2.1
end: vcard
--------------F0F5CD130D52BA0C6BCFDE06--
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Bob Sanford" <bob.sanford@compaq.com>
Date: 1998/01/21 Raw View
Have you tried using a vector of pointers like this?
vector<MyObject *> MyVector;
MyObject *p = new MyObject;
MyVector.push_back(pObject);
One caveat: Don't forget to destroy the objects you create with "new". I
would do my cleanup like this:
while (!MyVector.empty())
{
MyObject *ptr = MyVector[0];
MyVector.erase(MyVector.first());
delete ptr;
}
You could derive from vector and do your cleanup in your derived
container's destructor.
JiJun <jj@et.dtu.dk> wrote in article <34C0CAD7.1CB0CE4D@et.dtu.dk>...
> Dear STL experts,
> I got a question for you:
> I use a vector of objects in my program, and there are some pointers
outside
> the vector point to some of the objects in this vector by assign my
pointer like:
> TMyObject *= vector <TMyObject>::iterator FindObject();
>
> everything is Ok as long as I don't modify the vector...
>
> But once I add new element to the vector by calling push_back(), my
former
> assigned pointers will desperately lose their targets.:-) According to
STL documents,
> that's because the vector is reallocated and all iterators are
invalidated ..
>
> Is there any way I could keep my pointers allways point to the right
objects?
> (maybe not:-) , or any other suggestions... need your help desperately!
>
>
> John (also welcome to email to john.chou@bigfoot.com)
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Michael Davis <mdavis@DELmultipath.com>
Date: 1998/01/21 Raw View
In article <34C0CAD7.1CB0CE4D@et.dtu.dk>, JiJun <jj@et.dtu.dk> wrote:
>
>Dear STL experts,
> I got a question for you:
> I use a vector of objects in my program, and there are some pointers outside
>the vector point to some of the objects in this vector by assign my pointer like:
>TMyObject *= vector <TMyObject>::iterator FindObject();
>
> everything is Ok as long as I don't modify the vector...
>
> But once I add new element to the vector by calling push_back(), my former
>assigned pointers will desperately lose their targets.:-) According to STL documents,
>that's because the vector is reallocated and all iterators are invalidated ..
>
> Is there any way I could keep my pointers allways point to the right objects?
>(maybe not:-) , or any other suggestions... need your help desperately!
Well, what you could do is write your own FindObject function and have it
return the index of the found object instead of its iterator.
Because you're using vectors, you can use the [] operator to access elements:
typedef vector<TMyObject> objVector;
int FindObject( const & objVector obj, int key )
{
int i;
for ( i = 0; i < obj.size(); ++i )
if (obj[i].val == key)
return i;
return -1; // indicates failure
}
objVector obj;
// ...
int anIndex = FindObject( obj, 2 );
// ...
TMyObject o = obj[anIndex];
// ...
Hope this helps.
--
// Michael Davis, Programmer/Analyst I don't speak for Multipath. //
// mdavis@DELmultipath.com Nor do I speak against them. //
// Toronto The 'DEL' in my address is an anti-spam device. //
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]