Chưa phân loại

C++ Tutorial – Find min/max value in map

Here is a snippet for you to find a min value within a map with C++

// Find min value in map

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;
}

I think you can get your way to find a max one.

Happy coding!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s