Vector class

This is a discussion on Vector class within the C++ Programming forums, part of the General Programming Boards category; Ok maybe this time I'll actually post to the right board. Is there a STL method which will return a ...

  1. #1
    Unregistered
    Guest

    Vector class

    Ok maybe this time I'll actually post to the right board.

    Is there a STL method which will return a random element from a vector? Its easy enough to write such a method, but I was just wondering if one exists already. I've searched on google to no avail.
    Thanks.

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    66

    random

    The whole point of a vector is to have sequential predictable access in O(1).

    If you want random access to a member you can always do:

    std::vector v;

    ...

    v[(int)(rand()*v.size())]

    ...

    or similar depending how random you want it (rand is not new random as you know

  3. #3
    Shadow12345
    Guest
    what is STL?

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21