Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <memory>
- using namespace std;
- void logState(const vector<vector<int>>& v) {
- auto i = 0;
- for_each(v.begin(), v.end(), [&i](const auto& v2) {
- cout << i << ": ";
- for_each(v2.begin(), v2.end(), [](auto nr) {
- cout << nr << " ";
- });
- i++;
- cout << "\n";
- });
- cout << "\n";
- }
- auto radix(vector<int>& a) {
- auto first = make_unique<vector<vector<int>>>(10);
- auto second = make_unique<vector<vector<int>>>(10);
- for_each(a.begin(), a.end(), [&first](auto i) {
- (*first)[i%10].push_back(i);
- });
- auto stillGoing = true;
- auto exp = 10;
- while (stillGoing) {
- stillGoing = false;
- for_each(first->begin(), first->end(), [&second, exp, &stillGoing](auto& fir) {
- for_each(fir.begin(), fir.end(), [&second, exp, &stillGoing](auto i) {
- if(i >= exp*10) {
- stillGoing = true;
- }
- (*second)[(i/exp)%10].push_back(i);
- });
- fir.clear();
- });
- first.swap(second);
- exp *= 10;
- }
- auto it = a.begin();
- for_each(first->begin(), first->end(), [&it](auto& fir) {
- for_each(fir.begin(), fir.end(), [&it](auto i) {*(it++) = i;});
- });
- }
- int main() {
- vector<int> a {1, 80, 81, 11, 23, 32, 255, 25, 325};
- radix(a);
- for_each(a.begin(), a.end(), [](auto i) {
- cout << i << " ";
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement