accessing list items

Started by
3 comments, last by stefu 22 years, 9 months ago
I recently found how useful templates are. But here''s my problem with list: list listFrames; // then I add some pointers to list listFrames.push_back( pNewFrame1 ); listFrames.push_back( pNewFrame2 ); ... Then I need to redner frames so I have to get all the frames from the list. I can get first and last easily popping them. This is what I normally do Cframe *f = pFirstFrame; while(f) { // do something here f = f->next; } I use list instead of vector since I need to be able to remove items anywhere. using vector it could be done this way vecFrames[0] but not for list. How can I acces any item in a list?
Advertisement
Several ways of doing it (more I expect).

1) You could just replicate the [] method and write a function that removes etc.. the nth node of a list.

2) You could use the while loop you already have to find the item you are interested in and then remove it. Similar (if not the same as 1)

3) you could keep pointers to the list items and then when you wanted to delete one, you call remove on the node and have it join the list up and delete itself.

I haven''t really explained myself very well and each of the above suggestions have advantages and disadvantages. You should really ask yourself what you need out of your list and if you basically want to be able to access by an index number then you should write the code to do that.
eg, pseudocode/

NodePointer getListItem(int n) {
// n=index of item you want to access
ptr = top of list

for (int i=0;i ptr=ptr->next;
}
}
getListItem(3) will return a pointer to the 4th list element (list indexes
start from 0).

Lists are useful when you want to perform some form of operation on all list items at once. It is natural to access a list by starting at the top and moving past each node to the end. If you want to access your data in different orders, then there are other data structures that are more applicable. Hash tables and other tree based things for example. However, I think you are probably doing the right thing for your application.

Just my tired ramblings...
Regards,
DeVore
I presume you are using STL''s std::list.

If so, use your list like any other STL container with an iterator. Like this :


list< CFrame* >::iterator i = lstFrames.begin( );
for( ; i != lstFrames.end( ); ++i )
{
// do something here
}


Replace ''list'' with ''vector'' and it would work the exact same way.

Have fun


http://www.blastersoft.com
http://www.strategyfirst.com
But you should choose wisely between the two containers. If you need to access the items very often and don''t need to change to list often, you should use std::vector. It''s designed for fast element access. Otherwise use std::list or even std::stack or so.
Visit:http://www.evildawncrew.comAll things which are worth beeing done, are worth beeing donw well.
ok, thanks, I use STL. I replaces list with vector because accessing items is much more time critical than removing them. So I wrote own code to remove items.

This topic is closed to new replies.

Advertisement