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

  • Home
  • SEARCH
  • 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 745575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T09:35:57+00:00 2026-05-14T09:35:57+00:00

I have a map and I want to find the minimum value (right-hand side)

  • 0

I have a map and I want to find the minimum value (right-hand side) in the map. Here is how I did it:

bool compare(std::pair<std::string ,int> i, pair<std::string, int> j) {
  return i.second < j.second;
}
////////////////////////////////////////////////////
std::map<std::string, int> mymap;

mymap["key1"] = 50;
mymap["key2"] = 20;
mymap["key3"] = 100;

std::pair<char, int> min = *min_element(mymap.begin(), mymap.end(), compare); 
std::cout << "min " << min.second<< " " << std::endl;

The code above works fine and I’m able to get the minimum value. However, when I put this code inside my class as follows, it doesn’t seem to work:

int MyClass::getMin(std::map<std::string, int> mymap) {
  std::pair<std::string, int> min = *min_element(mymap.begin(), mymap.end(), 
                                                 (*this).compare);
                                                 // Error probably due to "this".
  return min.second; 
}

bool MyClass::compare(
    std::pair<std::string, int> i, std::pair<std::string, int> j) { 
  return i.second < j.second; 
}

How can I make the code work with my class? Also, is there a better solution which doesn’t require writing the additional compare function?

  • 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-14T09:35:57+00:00Added an answer on May 14, 2026 at 9:35 am

    You have a few options. The “best” way to do this is with a functor, this is guaranteed to be the fastest to call:

    typedef std::pair<std::string, int> MyPairType;
    struct CompareSecond
    {
        bool operator()(const MyPairType& left, const MyPairType& right) const
        {
            return left.second < right.second;
        }
    };
    
    
    
    int MyClass::getMin(std::map<std::string, int> mymap) 
    {
      std::pair<std::string, int> min 
          = *min_element(mymap.begin(), mymap.end(), CompareSecond());
      return min.second; 
    }
    

    (You can also nest the CompareSecond class inside MyClass.

    With the code you have now, you can easily modify it to work, however. Just make the function static and use the correct syntax:

    static bool 
    MyClass::compare(std::pair<std::string, int> i, std::pair<std::string, int> j) 
    { 
      return i.second < j.second; 
    }
    
    int MyClass::getMin(std::map<std::string, int> mymap) 
    {
      std::pair<std::string, int> min = *min_element(mymap.begin(), mymap.end(), 
                                                     &MyClass::compare);
      return min.second; 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a std::vector or std::map variable, and I want to see the
I have a large map of String->Integer and I want to find the highest
I want to have a map that has a homogeneous key type but heterogeneous
I have this object graph, I want to map: abstract Account (username, password, ...)
I have a database field called abCode , that I want to map to
I have an image and on it are logos (it's a map), I want
So I have a control (a map) on an aspx page. I want to
I have unsorted map of key value pairs. input = { xa => xavalue,
I have a map defined like this std::map<some_key_type, std::string::iterator> mIteratorMap; And a huge string
I have an std::map<std::string, float> so I can do quick lookups for float values

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.