How to use overloaded std :: less for std :: map

I have the following snippet:

typedef char OR[12]; class COR { OR m_or; public: COR(const char* or) { strcpy(m_or, or); } COR(const COR& o) { strcpy(m_or, o.m_or); } const char* GetOR() const { return m_or; } #if 0 // I do not wish to use this as it will create a temporary object bool operator<(const COR& left, const COR& right) const { return (strcmp(left.m_or, right.m_or) < 0); } #endif }; namespace std { template<> struct less<COR> { bool operator()(const COR& cor, const char* or) const { return (strcmp(cor.GetOR(), or) < 0); } }; } 

When I try to do this, I get the error message: error: there is no corresponding function to call std::map<COR, SomeStruct*, std::less<COR>, std::allocator<std::pair<const COR, SomeStruct*> > >::find(const char*&)

I do not want to use any method that involves comparing two "COR" objects. I will compare COR with const char *. Can you guys suggest a way to do this?

+4
source share
1 answer

Several methods:
Note. None of them generate additional copies, because the values ​​are always passed by reference (and since we usually do not mutate an object in comparison using a const reference).

Method 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); } }; 

method 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); } }; 

Method 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; 

The method you use will depend on the situation.
In most situations, I would use method (1). If there is a special sort order that I need for one event that I want to use with a sorted container, then I would use method (3). method (2) can be used as an alternative to method (1), and in some situations it is better (but you need to provide more detailed usage information before I say that I use it).

+9
source

Source: https://habr.com/ru/post/1403198/


All Articles