If this really concerns performance, depending on the range of characters allowed, something like this might be faster:
std::size_t wordEntropy( const std::string & word ) { unsigned char seen[256] = { 0 }; for( unsigned char c : word ) { ++seen[ c ]; } return std::count_if( & seen[0], & seen[ 0 ] + 256, []( unsigned char c ) { return c != 0; } ); }
But obviously this is a little harder to maintain. This solution has guaranteed O (n) complexity and does not make any dynamic memory allocations.
An alternative version that has no problems if the character occurs more than 255 times:
std::size_t wordEntropy( const std::string & word ) { bool seen[256] = { false }; for( unsigned char c : word ) { seen[ c ] = true; } return std::count_if( & seen[0], & seen[ 0 ] + 256, []( bool t ) { return t; } ); }
source share