Sorting a vector of custom objects

后端 未结 13 2976
既然无缘
既然无缘 2020-11-21 05:14

How does one go about sorting a vector containing custom (i.e. user defined) objects.
Probably, standard STL algorithm sort along with a predicate (a fu

相关标签:
13条回答
  • 2020-11-21 05:41

    You could use functor as third argument of std::sort, or you could define operator< in your class.

    struct X {
        int x;
        bool operator<( const X& val ) const { 
            return x < val.x; 
        }
    };
    
    struct Xgreater
    {
        bool operator()( const X& lx, const X& rx ) const {
            return lx.x < rx.x;
        }
    };
    
    int main () {
        std::vector<X> my_vec;
    
        // use X::operator< by default
        std::sort( my_vec.begin(), my_vec.end() );
    
        // use functor
        std::sort( my_vec.begin(), my_vec.end(), Xgreater() );
    }
    
    0 讨论(0)
  • 2020-11-21 05:42

    Yes, std::sort() with third parameter (function or object) would be easier. An example: http://www.cplusplus.com/reference/algorithm/sort/

    0 讨论(0)
  • 2020-11-21 05:50

    Below is the code using lambdas

    #include "stdafx.h"
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    struct MyStruct
    {
        int key;
        std::string stringValue;
    
        MyStruct(int k, const std::string& s) : key(k), stringValue(s) {}
    };
    
    int main()
    {
        std::vector < MyStruct > vec;
    
        vec.push_back(MyStruct(4, "test"));
        vec.push_back(MyStruct(3, "a"));
        vec.push_back(MyStruct(2, "is"));
        vec.push_back(MyStruct(1, "this"));
    
        std::sort(vec.begin(), vec.end(), 
            [] (const MyStruct& struct1, const MyStruct& struct2)
            {
                return (struct1.key < struct2.key);
            }
        );
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-21 05:51

    You are on the right track. std::sort will use operator< as comparison function by default. So in order to sort your objects, you will either have to overload bool operator<( const T&, const T& ) or provide a functor that does the comparison, much like this:

     struct C {
        int i;
        static bool before( const C& c1, const C& c2 ) { return c1.i < c2.i; }
     };
    
     bool operator<( const C& c1, const C& c2 ) { return c1.i > c2.i; }
    
     std::vector<C> values;
    
     std::sort( values.begin(), values.end() ); // uses operator<
     std::sort( values.begin(), values.end(), C::before );
    

    The advantage of the usage of a functor is that you can use a function with access to the class' private members.

    0 讨论(0)
  • 2020-11-21 05:54

    In the interest of coverage. I put forward an implementation using lambda expressions.

    C++11

    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    vector< MyStruct > values;
    
    sort( values.begin( ), values.end( ), [ ]( const MyStruct& lhs, const MyStruct& rhs )
    {
       return lhs.key < rhs.key;
    });
    

    C++14

    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    vector< MyStruct > values;
    
    sort( values.begin( ), values.end( ), [ ]( const auto& lhs, const auto& rhs )
    {
       return lhs.key < rhs.key;
    });
    
    0 讨论(0)
  • 2020-11-21 05:54
        // sort algorithm example
        #include <iostream>     // std::cout
        #include <algorithm>    // std::sort
        #include <vector>       // std::vector
        using namespace std;
        int main () {
            char myints[] = {'F','C','E','G','A','H','B','D'};
            vector<char> myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33
            // using default comparison (operator <):
            sort (myvector.begin(), myvector.end());           //(12 32 45 71)26 80 53 33
            // print out content:
            cout << "myvector contains:";
            for (int i=0; i!=8; i++)
                cout << ' ' <<myvector[i];
            cout << '\n';
            system("PAUSE");
        return 0;
        }
    
    0 讨论(0)
提交回复
热议问题