- #include <iostream>
- #include <algorithm>
- #include <array>
- #include <bitset>
- int main()
- {
- std::array<int, 10> arr;
- std::generate(arr.begin(), arr.end(), []{static int index; return index++;});
- std::cout << "before: ";
- std::for_each(arr.begin(), arr.end(), [](int value){ std::cout << std::bitset<4>(value) << " "; });
- std::cout << std::endl;
- std::sort(arr.begin(), arr.end(), [](const int &l, const int &r){ return __builtin_popcount(l) > __builtin_popcount(r); });
- std::cout << "after : ";
- std::for_each(arr.begin(), arr.end(), [](int value){ std::cout << std::bitset<4>(value) << " "; });
- std::cout << std::endl;
- return 0;
- }
- /* Output
- before: 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
- after : 0111 0011 0101 0110 1001 0001 0010 0100 1000 0000
- */
2012年12月11日火曜日
int型の変数においてbitが立っている数に関して降順にソートする
登録:
コメントの投稿 (Atom)
0 件のコメント:
コメントを投稿