Несколько методов:
Примечание. Ни один из них не создает дополнительных копий, поскольку значения всегда передаются по ссылке (и, поскольку мы обычно не изменяем объект при сравнении, передается по ссылке).
Метод 1:
class COR
{
public:
// 1: Make it a member function
// Thus you only specify the right hand side.
// The left is implicit.
bool operator<(COR const& right) const
{
return (strcmp(m_or, right.m_or) < 0);
}
};
метод 2:
class COR
{
public:
// 2: Make it a friend non member function
// Note: Just because I declare it here does not make it part of the class.
// This is a separate non member function
// The compiler makes the destinction because of the `friened`
friend bool operator<(COR const& left, COR const& right)
{
return (strcmp(left.m_or, right.m_or) < 0);
}
};
Метод 3:
class COR
{
public:
// Just an example. Just need some way for the functor to access members
// In a way that will allow a strict weak ordering.
bool test(COR const& right) const {return (strcmp(m_or, right.m_or) < 0);}
};
// Define a functor.
// This is just a class with the operator() overloaded so that it can
// act like a function. You can make it do whatever you like but for
// comparisons it will be passed two members of the container (accept by const
// reference and make sure the functor is const member and things will go well).
struct CorTest
{
bool operator()(COR const& left, COR const& right) const
{
return left.test(right);
}
};
// When you declare the set you just pass as the second template parameter.
// (or third if it is a map)
std::set<COR, CorTest> mySet;
std::map<COR, int, CorTest> myMap;
Метод, который вы используете, будет зависеть от ситуации.
В большинствеситуации я бы использовал метод (1).Если существует особый порядок сортировки, который мне нужен для одного события, которое я хочу использовать с отсортированным контейнером, то я бы использовал метод (3).Метод (2) может использоваться в качестве альтернативы методу (1), и в некоторых ситуациях он лучше (но вам нужно предоставить больше подробностей об использовании, прежде чем я бы сказал, используйте это).