1. <tfoot id='e5nMV'></tfoot>
      2. <legend id='e5nMV'><style id='e5nMV'><dir id='e5nMV'><q id='e5nMV'></q></dir></style></legend>

        <small id='e5nMV'></small><noframes id='e5nMV'>

        • <bdo id='e5nMV'></bdo><ul id='e5nMV'></ul>
        <i id='e5nMV'><tr id='e5nMV'><dt id='e5nMV'><q id='e5nMV'><span id='e5nMV'><b id='e5nMV'><form id='e5nMV'><ins id='e5nMV'></ins><ul id='e5nMV'></ul><sub id='e5nMV'></sub></form><legend id='e5nMV'></legend><bdo id='e5nMV'><pre id='e5nMV'><center id='e5nMV'></center></pre></bdo></b><th id='e5nMV'></th></span></q></dt></tr></i><div id='e5nMV'><tfoot id='e5nMV'></tfoot><dl id='e5nMV'><fieldset id='e5nMV'></fieldset></dl></div>

        如何在地图中找到最小值?

        How can I find the minimum value in a map?(如何在地图中找到最小值?)
      3. <small id='44qtR'></small><noframes id='44qtR'>

        1. <i id='44qtR'><tr id='44qtR'><dt id='44qtR'><q id='44qtR'><span id='44qtR'><b id='44qtR'><form id='44qtR'><ins id='44qtR'></ins><ul id='44qtR'></ul><sub id='44qtR'></sub></form><legend id='44qtR'></legend><bdo id='44qtR'><pre id='44qtR'><center id='44qtR'></center></pre></bdo></b><th id='44qtR'></th></span></q></dt></tr></i><div id='44qtR'><tfoot id='44qtR'></tfoot><dl id='44qtR'><fieldset id='44qtR'></fieldset></dl></div>

          <legend id='44qtR'><style id='44qtR'><dir id='44qtR'><q id='44qtR'></q></dir></style></legend>
            <tbody id='44qtR'></tbody>
                <tfoot id='44qtR'></tfoot>

                  <bdo id='44qtR'></bdo><ul id='44qtR'></ul>

                  本文介绍了如何在地图中找到最小值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 map,我想在地图中找到最小值(右侧).我是这样做的:

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

                  我怎样才能让代码与我的班级一起工作?另外,是否有更好的解决方案不需要编写额外的 compare 函数?

                  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?

                  推荐答案

                  您有几个选择.做到这一点的最佳"方法是使用 函子,这保证是最快的调用:

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

                  (您也可以将 CompareSecond 类嵌套在 MyClass 中.

                  (You can also nest the CompareSecond class inside MyClass.

                  但是,使用您现在拥有的代码,您可以轻松修改它以使其正常工作.只需使函数 static 并使用正确的语法:

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

                  这篇关于如何在地图中找到最小值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                  How should a size-limited stl-like container be implemented?(应该如何实现大小受限的 stl 类容器?)
                  Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                  STL BigInt class implementation(STL BigInt 类实现)
                  Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                  Move list element to the end in STL(在 STL 中将列表元素移动到末尾)

                        • <bdo id='q92KG'></bdo><ul id='q92KG'></ul>

                        • <tfoot id='q92KG'></tfoot>

                            <small id='q92KG'></small><noframes id='q92KG'>

                          • <i id='q92KG'><tr id='q92KG'><dt id='q92KG'><q id='q92KG'><span id='q92KG'><b id='q92KG'><form id='q92KG'><ins id='q92KG'></ins><ul id='q92KG'></ul><sub id='q92KG'></sub></form><legend id='q92KG'></legend><bdo id='q92KG'><pre id='q92KG'><center id='q92KG'></center></pre></bdo></b><th id='q92KG'></th></span></q></dt></tr></i><div id='q92KG'><tfoot id='q92KG'></tfoot><dl id='q92KG'><fieldset id='q92KG'></fieldset></dl></div>
                            <legend id='q92KG'><style id='q92KG'><dir id='q92KG'><q id='q92KG'></q></dir></style></legend>
                              <tbody id='q92KG'></tbody>