C++中的关键比较图不工作

C++中的关键比较图不工作,c++,map,C++,Map,我创建了一个映射,其中ClassExpression作为键,std::string作为值。下面给出了关键比较器 class ClassExpressionComparator { public: bool operator()(const ClassExpression& lhs, const ClassExpression& rhs) const { return ((lhs.quantifier == rhs.quantifier) &a

我创建了一个映射,其中ClassExpression作为键,std::string作为值。下面给出了关键比较器

class ClassExpressionComparator {
public:
    bool operator()(const ClassExpression& lhs,
    const ClassExpression& rhs) const {
           return ((lhs.quantifier == rhs.quantifier) &&
                   (lhs.property.compare(rhs.property) == 0) &&
                   (lhs.concept.compare(rhs.concept) == 0));
    }
 };
ClassExpression包含比较器中提到的3个字段。我比较了所有3个字段。当我使用find of map时,即使该键不在map中,它也会说它找到了该键,并给出一个现有键作为结果,得到第一个键作为结果

我尝试了以下方法

boost::shared_ptr< std::map<ClassExpression, std::string, 
ClassExpressionComparator> > cemap(
                new std::map<ClassExpression, 
                std::string, ClassExpressionComparator>());
ClassExpression ce1;
ce1.quantifier = com::xxxx::kb::SOME;
ce1.property = "<http://www.w3.org/2002/07/acts-on>";
ce1.concept = "<http://www.w3.org/2002/07/Tissue>";
populateMap(cemap, ce1);

ClassExpression ce2;
ce2.quantifier = com::xxxx::kb::SOME;
ce2.property = "<http://www.w3.org/2002/07/contained-in>";
ce2.concept = "<http://www.w3.org/2002/07/HeartValve>";
populateMap(cemap, ce2);

ClassExpression ce3;
ce3.quantifier = com::xxxx::kb::SOME;
ce3.property = "<http://www.w3.org/2002/07/has-location>";
ce3.concept = "<http://www.w3.org/2002/07/Endocardium>";

std::map<ClassExpression, std::string, ClassExpressionComparator>::iterator
                       ceIterator = cemap->find(ce3);
if (ceIterator == cemap->end()) {
         std::cout << "Not found" << std::endl;
}
else {
     std::cout << "Found; concept = " << ceIterator->second << std::endl;
}
ClassExpressionComparator cmp;
std::cout << "compare: " << cmp(ce1, ce3) << std::endl; 
populateMap只是做了一个插入,在我的实际代码中,我在其中做了更多的事情,我想保持相同的流,所以就这样保留了它。 cmpce1,ce3的输出是0,但当我执行findce3时,结果是它在第一个键位置找到它,而不是返回end。我哪里出了问题

多谢各位


拉格瓦。

你写了一个平等比较。映射需要小于的比较。或大于您希望键按降序排列的值

我通常的习惯用法是:

bool operator()(const ClassExpression& lhs,
const ClassExpression& rhs) const {
       return lhs.quantifier < rhs.quantifier? true
            : rhs.quantifier < lhs.quantifier? false
            : lhs.property.compare(rhs.property) < 0? true
            : lhs.property.compare(rhs.property) > 0? false
            : lhs.concept.compare(rhs.concept) < 0;
}

映射是一个已排序的容器,它所寻找的比较器操作符是一个实现严格弱排序的操作符,就像这是一种在所有成员中检查<的聪明而简短的方法一样:。如果按键必须是<或>,那么findkey如何操作?如何使用b都是false。a>b与b