定义于头文件
算法库提供大量用途的函数(例如查找、排序、计数、操作),它们在元素范围上操作。注意范围定义为 [first, last) ,其中 last 指代要查询或修改的最后元素的后一个元素。
- std::random_shuffle,
- std::shuffle
| template< class RandomIt > | (1) | (C++14 中弃用) (C++17 中移除) |
| template< class RandomIt, class RandomFunc > | (2) | (C++11 前) |
| template< class RandomIt, class RandomFunc > | (C++11 起) (C++14 中弃用) (C++17 中移除) | |
| template< class RandomIt, class URBG > | (3) | (C++11 起) |
重排序给定范围 [first, last) 中的元素,使得这些元素的每个排列拥有相等的出现概率。
1) 随机数生成器是实现定义的,但经常使用函数 std::rand 。
2) 随机数生成器为函数对象 r 。
3) 随机数生成器为函数对象 g 。
| first, last | - | 要随机打乱的元素范围 |
| r | - | 函数对象。若以 r(n) 调用,则返回可转换为 std::iterator_traits |
| g | - | 均匀随机位生成器 (UniformRandomBitGenerator) ,其结果类型可隐式转换为 std::iterator_traits |
| 类型要求 | ||
- RandomIt 必须满足值可交换 (ValueSwappable) 和 遗留随机访问迭代器 (LegacyRandomAccessIterator) 的要求。 | ||
- std::remove_reference_t 必须满足均匀随机位生成器 (UniformRandomBitGenerator) 的要求。 | ||
(无)
与 first 和 last 间的距离成线性。
标准未钦定实现,故即使你用准确相同的 RandomFunc 或 URBG ,也可能通过不同的标准库实现得到不同结果。
- template< class RandomIt >
- void random_shuffle( RandomIt first, RandomIt last )
- {
- typename std::iterator_traits<RandomIt>::difference_type i, n;
- n = last - first;
- for (i = n-1; i > 0; --i) {
- using std::swap;
- swap(first[i], first[std::rand() % (i+1)]);
- // rand() % (i+1) 实际上不准确,因为生成的数对于多数 i 值不均匀分布。
- // 正确实现将实际上需要重新实现 C++11 std::uniform_distributtion ,
- // 这超出了此示例的范畴。
- }
- }
- template<class RandomIt, class RandomFunc>
- void random_shuffle(RandomIt first, RandomIt last, RandomFunc&& r)
- {
- typename std::iterator_traits<RandomIt>::difference_type i, n;
- n = last - first;
- for (i = n-1; i > 0; --i) {
- using std::swap;
- swap(first[i], first[r(i+1)]);
- }
- }
- template<class RandomIt, class URBG>
- void shuffle(RandomIt first, RandomIt last, URBG&& g)
- {
- typedef typename std::iterator_traits<RandomIt>::difference_type diff_t;
- typedef std::uniform_int_distribution<diff_t> distr_t;
- typedef typename distr_t::param_type param_t;
-
- distr_t D;
- diff_t n = last - first;
- for (diff_t i = n-1; i > 0; --i) {
- using std::swap;
- swap(first[i], first[D(g, param_t(0, i))]);
- }
- }
- #include <iostream>
- #include <algorithm>
- #include <functional>
- #include <vector>
- #include <iterator>
-
- using namespace std;
-
- struct Cell
- {
- int x;
- int y;
-
- Cell &operator +=(const Cell &cell)
- {
- x += cell.x;
- y += cell.y;
- return *this;
- }
- };
-
- std::ostream &operator<<(std::ostream &os, const Cell &cell)
- {
- os << "{" << cell.x << "," << cell.y << "}";
- return os;
- }
-
- int main()
- {
- auto func1 = [](Cell & cell, const Cell & t)
- {
- cell += t;
- return cell;
- };
-
- Cell cell{99, 100};
- Cell t{2, 3};
- auto func2 = std::bind(func1, cell, t);
-
- vector<Cell> cells(8);
- std::generate(cells.begin(), cells.end(), func2);
- std::cout << "original : ";
- std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
- std::cout << std::endl;
-
- std::random_shuffle(cells.begin(), cells.end());
- std::cout << "random_shuffle : ";
- std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
- std::cout << std::endl;
- std::cout << std::endl;
-
- std::generate(cells.begin(), cells.end(), func2);
- std::cout << "original : ";
- std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
- std::cout << std::endl;
-
- auto func3 = [](int n)
- {
- return std::rand() % n;
- };
-
- std::random_shuffle(cells.begin(), cells.end(), func3);
- std::cout << "random_shuffle : ";
- std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
- std::cout << std::endl;
- std::cout << std::endl;
-
- std::generate(cells.begin(), cells.end(), func2);
- std::cout << "original : ";
- std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
- std::cout << std::endl;
-
- std::random_device rd;
- std::mt19937 g(rd());
-
- std::shuffle(cells.begin(), cells.end(), g);
- std::cout << "shuffle : ";
- std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
- std::cout << std::endl;
- std::cout << std::endl;
-
- return 0;
- }
