Dukales

permutation matrix reclaiming

May 14th, 2015
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <type_traits>
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <numeric>
  6. #include <utility>
  7. #include <random>
  8. #include <iterator>
  9.  
  10. #include <cstdlib>
  11.  
  12. int
  13. main()
  14. {
  15.     constexpr std::size_t size = 10;
  16.     std::vector< std::size_t > v;
  17.     for (std::size_t i = 0; i < size; ++i) {
  18.         v.push_back(i);
  19.     }
  20.     std::random_device rd;
  21.     std::mt19937 g(rd());
  22.     std::shuffle(std::begin(v), std::end(v), g);
  23.     std::copy(v.begin(), v.end(), std::ostream_iterator< std::size_t >(std::cout, " "));
  24.     std::cout << std::endl;
  25.     for (std::size_t from_ = 0; from_ < size; ++from_) {
  26.         std::size_t & to_ = v[from_];
  27.         while (to_ != from_) {
  28.             std::swap(to_, v[to_]);
  29.         }
  30.     }
  31.     std::copy(v.begin(), v.end(), std::ostream_iterator< std::size_t >(std::cout, " "));
  32.     std::cout << std::endl;
  33.     return EXIT_SUCCESS;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment