Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <memory>
  5. using namespace std;
  6.  
  7.  
  8. void logState(const vector<vector<int>>& v) {
  9.     auto i = 0;
  10.     for_each(v.begin(), v.end(), [&i](const auto& v2) {
  11.         cout << i << ": ";
  12.         for_each(v2.begin(), v2.end(), [](auto nr) {
  13.             cout << nr << " ";
  14.         });
  15.         i++;
  16.         cout << "\n";
  17.     });
  18.     cout << "\n";
  19. }
  20.  
  21.  
  22. auto radix(vector<int>& a) {
  23.     auto first = make_unique<vector<vector<int>>>(10);
  24.     auto second = make_unique<vector<vector<int>>>(10);
  25.  
  26.     for_each(a.begin(), a.end(), [&first](auto i) {
  27.         (*first)[i%10].push_back(i);
  28.     });
  29.  
  30.     auto stillGoing = true;
  31.     auto exp = 10;
  32.  
  33.     while (stillGoing) {
  34.         stillGoing = false;
  35.         for_each(first->begin(), first->end(), [&second, exp, &stillGoing](auto& fir) {
  36.             for_each(fir.begin(), fir.end(), [&second, exp, &stillGoing](auto i) {
  37.                 if(i >= exp*10) {
  38.                     stillGoing = true;
  39.                 }
  40.                 (*second)[(i/exp)%10].push_back(i);
  41.             });
  42.             fir.clear();
  43.         });
  44.         first.swap(second);
  45.         exp *= 10;
  46.     }
  47.  
  48.     auto it = a.begin();
  49.     for_each(first->begin(), first->end(), [&it](auto& fir) {
  50.         for_each(fir.begin(), fir.end(), [&it](auto i) {*(it++) = i;});
  51.     });
  52. }
  53.  
  54. int main() {
  55.  
  56.     vector<int> a {1, 80, 81, 11, 23, 32, 255, 25, 325};
  57.  
  58.     radix(a);
  59.  
  60.     for_each(a.begin(), a.end(), [](auto i) {
  61.         cout << i << " ";
  62.     });
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement