member function to shuffle content

Hi

Is there member functions for STL's like vector,list etc., to shuffle the content of that STL.

eg:
1
2
int arr[] = {1,2,3,4};
std::vector<int> v1(arr, arr+ sizeof(arr) / sizeof(int) );

now i need to shuffle the data randomly like

Now vector may contain {3,2,4,1} or {2,1,4,3} ...

I have taken vector as an example. please let me know if there is any alternative way with other STL's

Thanks In Advance
Last edited on
Not a member function, but you can use the function templates std::random_shuffle or std::shuffle. It works with both vectors and arrays.

1
2
std::random_shuffle(std::begin(arr), std::end(arr));
std::random_shuffle(std::begin(v1), std::end(v1));

http://en.cppreference.com/w/cpp/algorithm/random_shuffle
Hi Peter

Thanks for the information.... :)
Topic archived. No new replies allowed.