Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A Question about std::list

Reply
Thread Tools

A Question about std::list

 
 
JustSomeGuy
Guest
Posts: n/a
 
      11-24-2004
Can you access elements of the std::list like they were an array?
eg.
std::list<int> xlist;
int y;
....
y = xlist[10]; // Returns the 10th element of a list.



 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      11-24-2004
"JustSomeGuy" <> wrote in message
news:...
> Can you access elements of the std::list like they were an array?
> eg.
> std::list<int> xlist;
> int y;
> ...
> y = xlist[10]; // Returns the 10th element of a list.


No. std::vector and std::deque are the only containers that
provide efficient random-access to their contents.
An equivalent function could be implemented, but it will
require linear time ( O(N) to access the Nth element ).

Cheers,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com



 
Reply With Quote
 
 
 
 
Andrew Koenig
Guest
Posts: n/a
 
      11-24-2004
"JustSomeGuy" <> wrote in message
news:...

> Can you access elements of the std::list like they were an array?


No. If you wanted, you could write your own function to achieve the same
effect, but it's not generally possible to do so efficiently.

What are you trying to do?


 
Reply With Quote
 
JustSomeGuy
Guest
Posts: n/a
 
      11-24-2004
Andrew Koenig wrote:

> "JustSomeGuy" <> wrote in message
> news:...
>
> > Can you access elements of the std::list like they were an array?

>
> No. If you wanted, you could write your own function to achieve the same
> effect, but it's not generally possible to do so efficiently.
>
> What are you trying to do?


I think the answer I was looking for was to switch to a vector class instead
of the list class.


 
Reply With Quote
 
Ivan Vecerina
Guest
Posts: n/a
 
      11-25-2004
"JustSomeGuy" <> wrote in message
news:...
> Andrew Koenig wrote:
>
>> "JustSomeGuy" <> wrote in message
>> news:...
>>
>> > Can you access elements of the std::list like they were an array?

>>
>> No. If you wanted, you could write your own function to achieve the same
>> effect, but it's not generally possible to do so efficiently.
>>
>> What are you trying to do?

>
> I think the answer I was looking for was to switch to a vector class
> instead of the list class.


std::vector should be the default-choice container.

However, depending on the operations that you need to perform
(e.g. adding/removing items, FIFO, sorting, etc), another
standard container (std::deque or even std::set) could be
a better choice.
( The information you have provided does not allow for a definite
recommendation, which is why Andrew asked you for more details. )

Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form


 
Reply With Quote
 
Dfschweiss
Guest
Posts: n/a
 
      12-03-2004
That's what std::vector is for!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57