I can define a compare class for a map like this:
struct classcomp {
bool operator() (const string& lhs, const string& rhs) const
{
if(lhs < rhs)
return true;
else
return false;
}
};
but here lhs and rhs represent keys. What if I want to compare by values instead of key?
How will I do that?
It’s not about what you want; it’s about what
std::mapwants. The ordering is based on the layout of elements in memory (usually in a tree structure) and it is this process that uses the comparator. Wishing this were not the case does not make it so!It sounds to me like
std::mapas a container choice does not fit your requirements. Consult a container choice flow chart to decide what to do next.