You can define your comparison function. If you assume that your bitset models have an unsigned integer value, the following function will sort bitset in ascending order (and works for any N ).
template <size_t N> class LessThan { public: bool operator() (const std::bitset<N> &lhs, const std::bitset<N> &rhs) const { size_t i = N; while ( i > 0 ) { if ( lhs[i-1] == rhs[i-1] ) { i--; } else if ( lhs[i-1] < rhs[i-1] ) { return true; } else { return false; } } return false; } };
If you follow the following snippet:
const size_t mysz = 10; std::map< std::bitset<mysz>, size_t, Less<mysz> > mymap; for ( size_t i = 0; i < 10; i++ ) { mymap.insert( std::make_pair(std::bitset<mysz>(i),i) ); }
You will have this card:
mymap[0] is the pair ((0,0,0,0,0,0,0,0,0,0), 0) mymap[1] is the pair ((1,0,0,0,0,0,0,0,0,0), 1) mymap[2] is the pair ((0,1,0,0,0,0,0,0,0,0), 2) mymap[3] is the pair ((1,1,0,0,0,0,0,0,0,0), 3) mymap[4] is the pair ((0,0,1,0,0,0,0,0,0,0), 4) mymap[5] is the pair ((1,0,1,0,0,0,0,0,0,0), 5) mymap[6] is the pair ((0,1,1,0,0,0,0,0,0,0), 6) mymap[7] is the pair ((1,1,1,0,0,0,0,0,0,0), 7) mymap[8] is the pair ((0,0,0,1,0,0,0,0,0,0), 8) mymap[9] is the pair ((1,0,0,1,0,0,0,0,0,0), 9)