I am a newbie to C++, and this question may seem obvious to a lot of people.
If I write something like
std::map<int, double> m;
For example,
class own_int_less : public binary_function<int, int, bool>
{
public:
bool operator()( const double &left, const double &right ) const
{
return (abs(left - right) > epsilon) && (left < right);
};
double epsilon;
};
Thanks.
Is m guaranteed to be sorted according to int order?
Yes. The default comparator is std::less<Key>
, which in your case is std::less<int>
, which just uses <
as expected.
Is it necessary to define a comparitor class to enforce the sorting?
No, because the previous answer was "yes"!
When is the sorting actually occurred?
A typical map
implementation uses the comparator to insert a new element into the correct location. The comparator is also used when doing a lookup.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.