A Question about std::list

Discussion in 'C++' started by JustSomeGuy, Nov 24, 2004.

  1. JustSomeGuy

    JustSomeGuy Guest

    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.
     
    JustSomeGuy, Nov 24, 2004
    #1
    1. Advertising

  2. "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
     
    Ivan Vecerina, Nov 24, 2004
    #2
    1. Advertising

  3. "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?
     
    Andrew Koenig, Nov 24, 2004
    #3
  4. JustSomeGuy

    JustSomeGuy Guest

    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.
     
    JustSomeGuy, Nov 24, 2004
    #4
  5. "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
     
    Ivan Vecerina, Nov 25, 2004
    #5
  6. JustSomeGuy

    Dfschweiss Guest

    That's what std::vector is for!
     
    Dfschweiss, Dec 3, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page