Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 984013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:56:51+00:00 2026-05-16T04:56:51+00:00

I have created a map with ClassExpression as the key and std::string as the

  • 0

I have created a map with ClassExpression as the key and std::string as the value. The key comparator is given below

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 contains the 3 fields mentioned in the comparator. I compare all the 3 fields. When I use find() of map, even if the key is not present in the map, it says that it found the key and gives an existing key as the result (getting first key as the result).

I tried the following

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() just does an insert, in my actual code, I do a few more things in it, I wanted to keep the same flow, so left it that way.
The output of cmp(ce1, ce3) is 0 but when I do a find(ce3), the result is that it found it at the first key position instead of returning end(). Where am I going wrong here?

Thank you.

Raghava.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:56:52+00:00Added an answer on May 16, 2026 at 4:56 am

    You wrote an equality comparison. map requires a less-than comparison. (Or greater-than if you want the keys in decreasing order.)

    My usual idiom for doing this:

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created one class that directly map to ConfigSection of web. config. My
I have created a StringToMapConverter for converting string in predefined format to a Map
I have created a map (experimental) which has a '#' value if there is
I have created a Map as follows: private Map<String, List<Client>> clientCatalogue; this.clientCatalogue = new
I have created a site-map using the following HTML: <ul class=main-menu> <li> <div> <a
I have a map created from a json string that is sorted in the
I have created a hash table that will keep as a key a string
I am new to iphone development.I have created map applications and displayed the cuurent
I have created a map application to show near by places on the map.When
I have created a map marker that is positioned on my map by finding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.