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

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

          <bdo id='iNtP1'></bdo><ul id='iNtP1'></ul>

      1. <i id='iNtP1'><tr id='iNtP1'><dt id='iNtP1'><q id='iNtP1'><span id='iNtP1'><b id='iNtP1'><form id='iNtP1'><ins id='iNtP1'></ins><ul id='iNtP1'></ul><sub id='iNtP1'></sub></form><legend id='iNtP1'></legend><bdo id='iNtP1'><pre id='iNtP1'><center id='iNtP1'></center></pre></bdo></b><th id='iNtP1'></th></span></q></dt></tr></i><div id='iNtP1'><tfoot id='iNtP1'></tfoot><dl id='iNtP1'><fieldset id='iNtP1'></fieldset></dl></div>
        <legend id='iNtP1'><style id='iNtP1'><dir id='iNtP1'><q id='iNtP1'></q></dir></style></legend>
      2. C++/STL 是否支持按属性对对象进行排序?

        时间:2024-05-11
          <tbody id='XYmD3'></tbody>

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

                <tfoot id='XYmD3'></tfoot>
                  <bdo id='XYmD3'></bdo><ul id='XYmD3'></ul>

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

                • 本文介绍了C++/STL 是否支持按属性对对象进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想知道 STL 是否支持此功能:

                  I wonder if there is support in STL for this:

                  假设我有一个这样的课程:

                  Say I have an class like this :

                  class Person
                  {
                  public:
                    int getAge() const;
                    double getIncome() const;
                    ..
                    ..
                  };
                  

                  和一个向量:

                  vector<Person*> people;
                  

                  我想按年龄对人的向量进行排序:我知道我可以通过以下方式做到这一点:

                  I would like to sort the vector of people by their age: I know I can do it the following way:

                  class AgeCmp
                  {
                  public:
                     bool operator() ( const Person* p1, const Person* p2 ) const
                     {
                       return p1->getAge() < p2->getAge();
                     }
                  };
                  sort( people.begin(), people.end(), AgeCmp() );
                  

                  有没有更简洁的方法来做到这一点?仅仅因为我想根据属性"进行排序而必须定义整个类似乎有些过分.也许是这样的?

                  Is there a less verbose way to do this? It seems overkill to have to define a whole class just because I want to sort based on an 'attribute'. Something like this maybe?

                  sort( people.begin(), people.end(), cmpfn<Person,Person::getAge>() );
                  

                  推荐答案

                  基于成员属性进行比较的通用适配器.虽然它在第一次可重用时相当冗长.

                  Generic adaptor to compare based on member attributes. While it is quite more verbose the first time it is reusable.

                  // Generic member less than
                  template <typename T, typename M, typename C>
                  struct member_lt_type 
                  {
                     typedef M T::* member_ptr;
                     member_lt_type( member_ptr p, C c ) : ptr(p), cmp(c) {}
                     bool operator()( T const & lhs, T const & rhs ) const 
                     {
                        return cmp( lhs.*ptr, rhs.*ptr );
                     }
                     member_ptr ptr;
                     C cmp;
                  };
                  
                  // dereference adaptor
                  template <typename T, typename C>
                  struct dereferrer
                  {
                     dereferrer( C cmp ) : cmp(cmp) {}
                     bool operator()( T * lhs, T * rhs ) const {
                        return cmp( *lhs, *rhs );
                     }
                     C cmp;
                  };
                  
                  // syntactic sugar
                  template <typename T, typename M>
                  member_lt_type<T,M, std::less<M> > member_lt( M T::*ptr ) {
                     return member_lt_type<T,M, std::less<M> >(ptr, std::less<M>() );
                  }
                  
                  template <typename T, typename M, typename C>
                  member_lt_type<T,M,C> member_lt( M T::*ptr, C cmp ) {
                     return member_lt_type<T,M,C>( ptr, cmp );
                  }
                  
                  template <typename T, typename C>
                  dereferrer<T,C> deref( C cmp ) {
                     return dereferrer<T,C>( cmp );
                  }
                  
                  // usage:    
                  struct test { int x; }
                  int main() {
                     std::vector<test> v;
                     std::sort( v.begin(), v.end(), member_lt( &test::x ) );
                     std::sort( v.begin(), v.end(), member_lt( &test::x, std::greater<int>() ) );
                  
                     std::vector<test*> vp;
                     std::sort( v.begin(), v.end(), deref<test>( member_lt( &test::x ) ) );
                  }
                  

                  这篇关于C++/STL 是否支持按属性对对象进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:多线程程序中的 std::string 下一篇:为什么 std::list 上的 push_back 会更改用 rbegin 初始化的反向迭代器?

                  相关文章

                    <tfoot id='hIbVY'></tfoot>

                      <bdo id='hIbVY'></bdo><ul id='hIbVY'></ul>

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

                    1. <legend id='hIbVY'><style id='hIbVY'><dir id='hIbVY'><q id='hIbVY'></q></dir></style></legend>

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