Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何为std::map使用重载的std::less_C++_Stl - Fatal编程技术网

C++ 如何为std::map使用重载的std::less

C++ 如何为std::map使用重载的std::less,c++,stl,C++,Stl,我有以下片段: 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

我有以下片段:

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); }
   };
}
typedef char或[12];
类COR
{
或m_或;
公众:
COR(const char*or){strcpy(m_or,or);}
COR(const COR&o){strcpy(m_或,o.m_或)}
常量char*GetOR()常量{返回m_或;}
#如果0//,我不希望使用它,因为它将创建一个临时对象
布尔算子几种方法:
注意:这些都不会生成额外的副本,因为值总是通过引用传递(而且因为我们通常不会在比较时通过常量引用传递对象)

方法1:
类COR
{
公众:
//1:让它成为一个成员函数
//因此,您只能指定右侧。
//左派是含蓄的。

bool操作符您正在寻找C++14中添加的find()、lower_-bound()和upper_-bound()的新重载版本。它们完全满足您的要求


之所以出现此错误,是因为您犯了一个错误,
运算符()的参数类型必须相同。因此,此操作有效:

template <>
struct std::less<COR>
{
    bool operator()(const COR &a, const COR &b) const { return (strcmp(a.GetOR(), b.GetOR()) < 0); }
};
模板
struct std::less
{
bool操作符()(const COR&a,const COR&b)const{return(strcmp(a.GetOR(),b.GetOR())<0);}
};

您的代码具有未定义的行为,因为您对
std::less
的专门化不符合
std::less
的标准库要求。为了进行操作,
std::map
必须有一种方法来比较两个
key\u type
对象。执行此比较时,不会创建任何临时对象。如果正确理解您的意思,您希望能够使用
const char*
而不是
COR
来查找元素。这(不幸的)是不可能直接实现的。您担心的临时元素的构造是什么?(例如,我发现很难相信它会导致性能问题)。此外,它实际上不会创建一个临时对象。这些对象是通过引用传递的,这会导致对库函数的调用,而数组会衰减为指针。@Karl:虽然他没有显示代码,但我认为他有一个
map
,他想调用
my\u map.find(“foo”)
。在正常运行中,参数表达式
“foo”
导致
COR
的临时实例被传递到
map::find
。Mankarse是正确的,尽管构造临时实例只是复制几个字节。这些相同的字节将被
find
多次比较,因此很难想象在什么情况下副本会成为性能关键的副本。This没有回答OP的问题。这个问题是专门关于std::less()的,这个答案给出了很多,但是这一点。@iantonuk这个答案对于这个问题是正确的。比较类
std::less
使用
从操作角度来看重载std::less()本身;答案应该类似于命名空间std{template struct less{bool operator()(const pair&a,const pair&b){return(a.first模板STD的类STD::?为什么不用那个例子添加一个答案?当然,已经有这样的答案了,但是值得添加/链接它。好的,你的答案已经很满了,尽管把它加在这里是有意义的。
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);
   }
};
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;
template <>
struct std::less<COR>
{
    bool operator()(const COR &a, const COR &b) const { return (strcmp(a.GetOR(), b.GetOR()) < 0); }
};