Access list values by index?

Started by
9 comments, last by Haptic 15 years, 4 months ago
Hey all, This seems like a really dumb question but I can't figure it out. I have a list:
list<int> Object;


Is there a way to access values in the middle of the list like you can with an array (by index), and without removing it from the list? For example:
    int someobject = Object[2];
It seems so trivial, I must be missing something. Thanks,
- Haptic
Advertisement
I'm using this as a work around, I just thought there would be a simpler way:
/* Create copy of list (so we can destroy it)*/list<int> ObjectList = Object;int size = (int)Object.size();     /* Cycle through values by popping front */	     for(int s = 0; s < size; s++)     {	int o = ObjectList.front();	DoSomethingToObject(o);	ObjectList.pop_front();     }


Surely not the best way?

Thanks,
- Haptic
Use iterators to step throug the list.
for(list<int>::iterator I = Object.begin(); I != Object.end(); ++I){    int o = *I;}

And just to mention it, lists are not designed for random access, only sequential access like this iterator example. That is why there's no random access interface to the list.
Best to use the for_each algorithm for things like this (within the algorithm header file).

#include<iostream>#include<list>#include<algorithm>void DoSomething(int i){  std::cout << i;}int main(){  std::list<int> Object;  Object.push_back(1);  Object.push_back(2);  Object.push_back(3);  std::for_each(Object.begin(), Object.end(), DoSomething);}
Even if it's not optimal it would be very convenient. But in this regard the STL is failing spectacularely.


Try this:

std::list<int>::iterator it( Object.begin() );
std::advance( it, Index );

Now access the Object at index Index with *it.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

A std::vector or std::deque is probably a better choice for this case. Those do allow random access.
Create-ivity - a game development blog Mouseover for more information.
Quote:Original post by Endurion
Even if it's not optimal it would be very convenient. But in this regard the STL is failing spectacularely.


No, STL is doing the right thing, and telling the user they should use a different container.

Lists are for iteration.
Vectors are for random access.

std::advance however is container-agnostic, so it's the "proper" way to index into containers.
Is there any particular reason you need to use a list? A vector would probably be better suited for that.
Quote:Original post by Captain P
A std::vector or std::deque is probably a better choice for this case. Those do allow random access.
Yes, but random access isn't needed at all in this example.
Iterating from the first to the last element as shown in the code snippet works perfectly well with a std::list (with either iterators or for_each).
Quote:Original post by Haptic
Hey all,

This seems like a really dumb question but I can't figure it out.
I have a list:
*** Source Snippet Removed ***
Is there a way to access values in the middle of the list like you can with an array (by index), and without removing it from the list? For example:
    int someobject = Object[2];
It seems so trivial, I must be missing something.

Thanks,


Do you really want a specific element of the list, whose position in the list you know? If so, why?

Are you sure you don't really want "each element of the list, in order"?


This topic is closed to new replies.

Advertisement