Iterator to pointer?

A

André Castelo

Hi

I have a class X and I'm using a list to store X instances. Later in
the project I realized that I needed to search that list for certain
instances and I thought of storing the found objects in a vector<X *>.

My problem is - how can I run through list<X>, with a
list<X>::iterator, and use a pointer to point to the selected objects?

I'm thinking of rewriting all instances of list<X> and use list<X *>
in order to be compatible with the search function.
 
M

Martin York

Hi

I have a class X and I'm using a list to store X instances. Later in
the project I realized that I needed to search that list for certain
instances and I thought of storing the found objects in a vector<X *>.

My problem is - how can I run through list<X>, with a
list<X>::iterator, and use a pointer to point to the selected objects?

I'm thinking of rewriting all instances of list<X> and use list<X *>
in order to be compatible with the search function.

class X
{};
std::list<X> myList;
std::list<X>::iterator find = myList.begin();
X* pointer = &(*find); // * -> get a reference to
the objct. & get the addrss.
std::vector<X*> data;
data.push_back(pointer);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top