What containers are valid to use with std::random_shuffle( RandomIt first, RandomIt last )
?
The API description says the two iterators need to be random-access iterators — I guess I'm not clear what a random-access iterator is, and specifically, why std::vector::begin()
and and ::end()
are, but the same from std::set
and std::unordered_set
are not.
#include <algorithm>
#include <set>
#include <unordered_set>
#include <vector>
int main( int argc, char* argv[] )
{
std::set<int> s = { 1, 2, 3, 4, 5 };
std::unordered_set<int> u = { 1, 2, 3, 4, 5 };
std::vector<int> v = { 1, 2, 3, 4, 5 };
std::random_shuffle( s.begin(), s.end() ); // compile error
std::random_shuffle( u.begin(), u.end() ); // compile error
std::random_shuffle( v.begin(), v.end() ); // :)
return 0;
}
Please login or Register to submit your answer