• <small id='AcfyC'></small><noframes id='AcfyC'>

      1. <legend id='AcfyC'><style id='AcfyC'><dir id='AcfyC'><q id='AcfyC'></q></dir></style></legend>
          <bdo id='AcfyC'></bdo><ul id='AcfyC'></ul>

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

        如何根据对的第二个元素对对的向量进行排序?

        时间:2024-05-12

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

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

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

                    <tbody id='U8R97'></tbody>
                • 本文介绍了如何根据对的第二个元素对对的向量进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果我有一个成对的向量:

                  If I have a vector of pairs:

                  std::vector<std::pair<int, int> > vec;
                  

                  是否有一种简单的方法可以根据对的第二个元素以递增顺序对列表进行排序?

                  Is there and easy way to sort the list in increasing order based on the second element of the pair?

                  我知道我可以编写一个小函数对象来完成这项工作,但是有没有办法使用 STLstd::less 的现有部分来直接做工作?

                  I know I can write a little function object that will do the work, but is there a way to use existing parts of the STL and std::less to do the work directly?

                  我知道我可以编写一个单独的函数或类来传递给要排序的第三个参数.问题是我是否可以用标准的东西来构建它.我真的很喜欢这样的东西:

                  I understand that I can write a separate function or class to pass to the third argument to sort. The question is whether or not I can build it out of standard stuff. I'd really something that looks like:

                  std::sort(vec.begin(), vec.end(), std::something_magic<int, int, std::less>());
                  

                  推荐答案

                  EDIT:使用 c++14,最好的解决方案很容易编写,这要归功于 lambdas 现在可以具有类型参数<代码>自动.这是我目前最喜欢的解决方案

                  EDIT: using c++14, the best solution is very easy to write thanks to lambdas that can now have parameters of type auto. This is my current favorite solution

                  std::sort(v.begin(), v.end(), [](auto &left, auto &right) {
                      return left.second < right.second;
                  });
                  


                  原始答案:

                  只需使用自定义比较器(它是 std::sort 的可选第三个参数)

                  Just use a custom comparator (it's an optional 3rd argument to std::sort)

                  struct sort_pred {
                      bool operator()(const std::pair<int,int> &left, const std::pair<int,int> &right) {
                          return left.second < right.second;
                      }
                  };
                  
                  std::sort(v.begin(), v.end(), sort_pred());
                  

                  如果您使用的是 C++11 编译器,则可以使用 lambdas 编写相同的代码:

                  If you're using a C++11 compiler, you can write the same using lambdas:

                  std::sort(v.begin(), v.end(), [](const std::pair<int,int> &left, const std::pair<int,int> &right) {
                      return left.second < right.second;
                  });
                  

                  编辑:为了回应您对问题的编辑,这里有一些想法......如果您真的想要有创意并且能够大量重复使用这个概念,只需制作一个模板:

                  EDIT: in response to your edits to your question, here's some thoughts ... if you really wanna be creative and be able to reuse this concept a lot, just make a template:

                  template <class T1, class T2, class Pred = std::less<T2> >
                  struct sort_pair_second {
                      bool operator()(const std::pair<T1,T2>&left, const std::pair<T1,T2>&right) {
                          Pred p;
                          return p(left.second, right.second);
                      }
                  };
                  

                  那么你也可以这样做:

                  std::sort(v.begin(), v.end(), sort_pair_second<int, int>());
                  

                  甚至

                  std::sort(v.begin(), v.end(), sort_pair_second<int, int, std::greater<int> >());
                  

                  虽然说实话,这有点矫枉过正,只写3行函数就可以了:-P

                  Though to be honest, this is all a bit overkill, just write the 3 line function and be done with it :-P

                  这篇关于如何根据对的第二个元素对对的向量进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:编写自己的 STL 容器 下一篇:为什么不能简单初始化(带大括号)2D std::array?

                  相关文章

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

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