get a member of each element in an STL container

C

Christopher Pisz

Is there a way to build a seperate array of objects, when those objects are
members of other objects, which are contained in a STL data structure?
, without iterating through the whole list one by one and getting them
seperatlely.


class A
{
public:
int * m_data;
};

typdef list<A> MyList;
MyList g_mylist;

void Foo()
{
// for each A object I need the data member
int ** datalist = ??
}
 
V

Victor Bazarov

Christopher said:
Is there a way to build a seperate array of objects, when those
objects are members of other objects, which are contained in a STL
data structure? , without iterating through the whole list one by one
and getting them seperatlely.


class A
{
public:
int * m_data;
};

typdef list<A> MyList;
MyList g_mylist;

void Foo()
{
// for each A object I need the data member
int ** datalist = ??
}

No, there is no way to do that without iterating through the whole
list. However, you could avoid iterating yourself if you write
a particular _functor_ and then use 'std::for_each'. Of course it
only means you're hiding the actual iteration behind the call to
'for_each' (and internally it does iterate through the range of
iterators you pass in), but it might be what you're asking about.

V
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top