fork(19) download
  1. #include <iostream>
  2. #include <map>
  3. #include <algorithm>
  4.  
  5. int cntBits(int value) {
  6. int num_bits=0;
  7. for(size_t i = 0; i < 32 ; ++i, value >>= 1) {
  8. if ((value & 1) == 1) ++num_bits;
  9. }
  10. return num_bits;
  11. }
  12.  
  13. struct cntBitsCmp {
  14. bool operator()(int a, int b) {
  15. return cntBits(a) < cntBits(b);
  16. }
  17. };
  18.  
  19. using namespace std;
  20.  
  21. int main() {
  22. std::map<int,int,cntBitsCmp> myMap = {
  23. {128,2},
  24. {3,4}
  25. };
  26. for(std::map<int,int,cntBitsCmp>::const_iterator i=myMap.begin() ; i != myMap.end() ; ++i) {
  27. cout << i->first << " " << i->second << endl;
  28. }
  29. return 0;
  30. }
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
128 2
3 4