473,327 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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

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

Aug 28 '07 #1
11 5930
"kkirtac" <ka**********@gmail.comwrote in message
news:11**********************@y42g2000hsy.googlegr oups.com...
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
Aug 28 '07 #2
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
Aug 28 '07 #3
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 :)
>
Michael
Aug 28 '07 #4
kkirtac wrote:
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<intblah;
...
int& ref = getAtIndex(blah, 42);

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 28 '07 #5
On Aug 28, 5:10 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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<intblah;
...
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

Aug 28 '07 #6
greg wrote:
On Aug 28, 5:10 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
> 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<intblah;
...
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?
FAQ, section 35. And by the time this function is instantiated, the
'C::value_type' is known because 'C' is something concrete.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 28 '07 #7
On Aug 28, 2:57 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>
FAQ, section 35.
Doesn't say anything abut this issue?

Aug 28 '07 #8
greg wrote:
On Aug 28, 2:57 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>>
FAQ, section 35.

Doesn't say anything abut this issue?
Huh? 35.18 does. It explains why 'typename' is needed, as I see it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 28 '07 #9
On Aug 28, 3:23 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
greg wrote:
On Aug 28, 2:57 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
FAQ, section 35.
Doesn't say anything abut this issue?

Huh? 35.18 does. It explains why 'typename' is needed, as I see it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
My bad.

Aug 28 '07 #10
On Aug 29, 9:44 am, greg <greg.johnse...@gmail.comwrote:
On Aug 28, 5:10 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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;
}
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 ?
Aug 28 '07 #11
kkirtac wrote:
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.
Aug 29 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Pemberton | last post by:
I'm sure there's a good explanation for this effect, but I get rather a strange output from this little test: #include <iostream> #include <list> int main() { std::list<int> int_list;
8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
5
by: Eric Lilja | last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope it's readable at least): #include <algorithm> #include <iostream> #include <vector> class Row { public:
6
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't...
44
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
8
by: Spoon | last post by:
Hello, Could someone explain why the following code is illegal? (I'm trying to use a list of (C-style) arrays.) #include <list> typedef std::list < int foo_t; int main() { int v = { 12, 34...
7
by: TBass | last post by:
So I have a class: class Client { unsigned int ClientID; .... }; class MyListenSocket
0
by: Javier | last post by:
Hi all, I have this code: class A { std::list<Bm_observadores; void function() {
3
by: Ray D. | last post by:
Hey all, I'm trying to pass a list into a function to edit it but when I compile using g++ I continue to get the following error: maintainNeighbors.cpp:104: error: invalid initialization of...
12
by: isliguezze | last post by:
template <class T> class List { public: List(); List(const List&); List(int, const T&); void push_back(const T &); void push_front(const T &); void pop_back();
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.