c++ - Iterate through a list of objects and point to the node that matches a certain criteria -
i using custom data structure 'block' , have stored several blocks in list:
list openlist<block>
i need iterate through each block, access data member 'f' (an integer) , check has lowest f cost.
my logic have pointer pointing best block value, rather each time copying best blocks values.
here's code:
for (std::list<block>::iterator openi = openlist.begin(); openi != openlist.end(); openi++){ if ((*openi).f < bestfcost){ bestfcost = (*openi).f; // here's need help.... } }
i don't think i'm asking of iterator possible. wish set
block * q
to data iterator points to, need entire object not data members.
edit: simple solution! initilised q typo (q* instead of *q)!
block * q = 0;
then in part of loop commented:
q = &(*openi)
:)
*openi
of type block
, right? can take address of that:
block * q = &*openi;
the &*
might seem redundant, *
operator on iterator, returning block
type, opposed scenario's *
dereference, , &
references again.
or can store iterator itself:
std::list<block>::iterator q = openi;
or can store object value, assuming implemented copy constructor:
block q = *openi;
Comments
Post a Comment