-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshuffles.cpp
More file actions
42 lines (36 loc) · 1.21 KB
/
Copy pathshuffles.cpp
File metadata and controls
42 lines (36 loc) · 1.21 KB
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
#include <iostream>
#include <vector>
#include <list>
#include <numeric> // iota
#include <random> // mt19937
#include <algorithm> // shuffle
#include "utils/printing.hpp"
using std::cout;
int main()
{
// Randomly shuffle a vector
int N = 13;
std::vector<int> v1 (N);
std::iota(v1.begin(), v1.end(), -3);
cout << "----- Vector -----\n";
cout << "before shuffle: ";
printVec(v1);
std::shuffle(v1.begin(), v1.end(), std::mt19937{std::random_device{}()});
cout << "after shuffle : ";
printVec(v1);
// Randomly shuffle a list
// The following example applies std::shuffle to a vector of std::list iterators
// since std::shuffle cannot be applied to a std::list directly.
// see: https://en.cppreference.com/w/cpp/algorithm/iota
std::list<int> l1(N);
std::iota(l1.begin(), l1.end(), 0);
cout << "----- List -----\n";
cout << "before shuffle: ";
printList(l1);
std::vector<std::list<int>::iterator> vit(l1.size());
std::iota(vit.begin(), vit.end(), l1.begin());
std::shuffle(vit.begin(), vit.end(), std::mt19937{std::random_device{}()});
cout << "after shuffle : ";
for (auto i:vit) std::cout << *i << " ";
std::cout << "\n";
}