Sign in to follow this  
Haptic

Access list values by index?

Recommended Posts

Haptic    205
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,

Share this post


Link to post
Share on other sites
Haptic    205
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,

Share this post


Link to post
Share on other sites
Brother Bob    10344
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.

Share this post


Link to post
Share on other sites
0xFFFF0000    163
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);
}

Share this post


Link to post
Share on other sites
Endurion    5412
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.

Share this post


Link to post
Share on other sites
Antheus    2409
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Zahlman    1682
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"?


Share this post


Link to post
Share on other sites
Captain P    1092
Quote:
Original post by samoth
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).

True, true. :)

Share this post


Link to post
Share on other sites
Haptic    205
Firstly, a huge thanks for the overwhelming response.

Random access is not necessary, I just need to iterate through the list from start to finish and, only having used arrays before, had a panic when I couldn't use indices.

I am using a list and not an array because of its automated insert and remove functions. In the past I've written my own utility functions but, after learning about lists, I figured they would be more efficient.

I'll sit down and take a deeper look at all of your suggestions now!

Thanks again,

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this