how to reach an arbitrary index in a std::list

K

kkirtac

Hello, i want to reach a specified index in a list, as we can achieve
this by the "[]" operator in a std::vector. I couldnt find a way to do
it without iterators, stepping one by one..i need to point to a
specified index and retrieve the value and then remove the value of
the entry...

Regards
 
V

Victor Bazarov

kkirtac said:
Hello, i want to reach a specified index in a list, as we can achieve
this by the "[]" operator in a std::vector. I couldnt find a way to do
it without iterators, stepping one by one..i need to point to a
specified index and retrieve the value and then remove the value of
the entry...

template<class C>
typename C::value_type& getAtIndex(C &L, size_t i)
{
if (i >= L.size())
throw "Out of bounds";
typename C::iterator it = L.begin();
std::advance(it, i);
return *it;
}

...
list<int> blah;
...
int& ref = getAtIndex(blah, 42);

V
 
M

Michael DOUBEZ

kkirtac a écrit :
Hello, i want to reach a specified index in a list, as we can achieve
this by the "[]" operator in a std::vector. I couldnt find a way to do
it without iterators, stepping one by one..i need to point to a
specified index and retrieve the value and then remove the value of
the entry...

Use the std::advance algorithm:

list<T>::iterator it=list.begin();
int n=42;

std::advance(it,n);

//it is 42th element or list.end()

Michael
 
M

Michael DOUBEZ

Michael DOUBEZ a écrit :
kkirtac a écrit :
Hello, i want to reach a specified index in a list, as we can achieve
this by the "[]" operator in a std::vector. I couldnt find a way to do
it without iterators, stepping one by one..i need to point to a
specified index and retrieve the value and then remove the value of
the entry...

Use the std::advance algorithm:

list<T>::iterator it=list.begin();
int n=42;

std::advance(it,n);

//it is 42th element or list.end()

oups it is the 43th :)
 
A

Abdo Haji-Ali

kkirtac said:
Hello, i want to reach a specified index in a list, as we can achieve
this by the "[]" operator in a std::vector. I couldnt find a way to do
it without iterators, stepping one by one..i need to point to a
specified index and retrieve the value and then remove the value of
the entry...
Then why don't you use a vector? Generally if you want to randomly access
elements in a collection (by index) rather than sequentially (from begin()
to end()) you'll need a vector. Or you'll risk the added overhead of
traversing the whole list to get to the specific index you want.

-
Abdo Haji-Ali
Programmer
In|Framez
 
G

greg

template<class C>
typename C::value_type& getAtIndex(C &L, size_t i)
{
if (i >= L.size())
throw "Out of bounds";
typename C::iterator it = L.begin();
std::advance(it, i);
return *it;
}

...
list<int> blah;
...
int& ref = getAtIndex(blah, 42);

I was wondering: why do you need `typename C::' instead of `C::' in
both of the cases? Also how does the compiler know what value_type is?

thanks,
Greg
 
V

Victor Bazarov

greg said:
I was wondering: why do you need `typename C::' instead of `C::' in
both of the cases? Also how does the compiler know what value_type is?

FAQ, section 35. And by the time this function is instantiated, the
'C::value_type' is known because 'C' is something concrete.

V
 
O

Old Wolf

I was wondering: why do you need `typename C::' instead of `C::' in
both of the cases? Also how does the compiler know what value_type is?

The 'typename' keyword tells the compiler that C::value_type
is the name of a type (as opposed to the name of a variable),
so it can parse the source file correctly.

In this particular code it's not ambiguous, but in other
code it is; does:
C::foo & x;

declare a reference, or perform a bitwise AND ?
 
J

Juha Nieminen

kkirtac said:
Hello, i want to reach a specified index in a list, as we can achieve
this by the "[]" operator in a std::vector. I couldnt find a way to do
it without iterators, stepping one by one..

There isn't. advance() has been suggested, but it just does exactly
that.
 

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

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top