Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <type_traits>
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <numeric>
- #include <utility>
- #include <random>
- #include <iterator>
- #include <cstdlib>
- int
- main()
- {
- constexpr std::size_t size = 10;
- std::vector< std::size_t > v;
- for (std::size_t i = 0; i < size; ++i) {
- v.push_back(i);
- }
- std::random_device rd;
- std::mt19937 g(rd());
- std::shuffle(std::begin(v), std::end(v), g);
- std::copy(v.begin(), v.end(), std::ostream_iterator< std::size_t >(std::cout, " "));
- std::cout << std::endl;
- for (std::size_t from_ = 0; from_ < size; ++from_) {
- std::size_t & to_ = v[from_];
- while (to_ != from_) {
- std::swap(to_, v[to_]);
- }
- }
- std::copy(v.begin(), v.end(), std::ostream_iterator< std::size_t >(std::cout, " "));
- std::cout << std::endl;
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment