ClassExpression
을 키로 사용하고 std::string
을 값으로 사용하여 맵을 만들었습니다. 키 비교기는 아래에 주어진다.C++ 맵의 주요 비교가 작동하지 않습니다.
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()를 사용할 때 키가지도에 없더라도 키를 찾았고 결과로 기존 키를 제공한다고합니다 (첫 번째 키를 결과로 가져옴).
나는 그래서 그런 식으로 왼쪽, 나도 같은 흐름을 유지하고 싶어, 그 안에 다음과 같은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()
단지 내 실제 코드에서, 내가 삽입 할 않는 몇 가지를 시도했다. cmp(ce1, ce3)
의 출력은 0이지만, find(ce3)
을 수행하면 결과는 end()
이 아닌 첫 번째 키 위치에서 발견된다는 것입니다. 내가 여기서 어디로 잘못 가고 있니?
감사합니다.
Raghava.
<모든 회원 중에서 <를 확인하는 것은 영리하고 짧은 방법입니다. 키가 < or > 인 경우 찾기 (키)는 어떻게 작동합니까? 어떻게하면 <연산자를 사용하여 정확한 키를 찾을 수 있습니까? 이를 염두에두고, < – Raghava
@Raghava 대신에 평등을 구현했습니다 :'a == b' 인 경우에만'a < b' and 'a > b '가 모두 false입니다. 그리고'a> b'는'b '. 부울 함수를 사용하여 두 가지를 제거하려면 두 가지 함수 호출이 필요합니다. 따라서 가장 효율적이고 간단한 방법은 동일한 피연산자를 역 피연산자로 두 번 호출하는 것입니다. (물론 컴파일러는 하나의 비교 명령에 대해 둘 다 최적화 할 수 있습니다.) – Potatoswatter
find는! (l
FuleSnabel