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
|
#include <iostream>
#include <vector>
#include <string>
#include <random>
#include <ctime>
#include <algorithm>
template < typename NESTED_SEQUENCE >
void shuffle_shuffle( NESTED_SEQUENCE& nested_seq )
{
// http://en.cppreference.com/w/cpp/numeric/random
static std::mt19937 rng( std::time(nullptr) ) ;
// http://www.stroustrup.com/C++11FAQ.html#for
// http://www.stroustrup.com/C++11FAQ.html#auto
// http://en.cppreference.com/w/cpp/algorithm/random_shuffle
// http://en.cppreference.com/w/cpp/iterator/begin
for( auto& row : nested_seq ) std::shuffle( std::begin(row), std::end(row), rng ) ;
std::shuffle( std::begin(nested_seq), std::end(nested_seq), rng ) ;
}
template < typename NESTED_SEQUENCE >
void print( const NESTED_SEQUENCE& nested_seq )
{
for( const auto& row : nested_seq )
{
for( const auto& item : row ) std::cout << item << ' ' ;
std::cout << '\n' ;
}
std::cout << '\n' ;
}
int main ()
{
std::vector< std::vector<std::string> > arr = {
{ "et1_a", "et1_b", "et1_c", "et1_d" },
{ "et2_a", "et2_b", "et2_c", "et2_d" },
{ "et3_a", "et3_b", "et3_c", "et3_d" },
{ "et4_a", "et4_b", "et4_c", "et4_d" },
{ "et5_a", "et5_b", "et5_c", "et5_d" },
};
for( int i = 0 ; i < 2 ; ++i )
{
shuffle_shuffle(arr) ;
print(arr) ;
}
}
|