Is there support in C++/STL for sorting objects by attribute?

后端 未结 8 1182
耶瑟儿~
耶瑟儿~ 2020-11-30 05:16

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         


        
8条回答
  •  一整个雨季
    2020-11-30 05:26

    This isn't really so much an answer in itself, as a reply to AraK's reply to my comment that sorting with a function instead of a functor can be slower. Here's some (admittedly ugly -- far too much CnP) test code that compares various sorting: qsort, std::sort of vector vs. array, and std::sort using a template class, template function, or plain function for comparison:

    #include 
    #include 
    #include 
    #include 
    
    int compare(void const *a, void const *b) {
        if (*(int *)a > *(int *)b)
            return -1;
        if (*(int *)a == *(int *)b)
            return 0;
        return 1;
    }
    
    const int size = 200000;
    
    typedef unsigned long ul;
    
    void report(char const *title, clock_t ticks) { 
        printf("%s took %f seconds\n", title, ticks/(double)CLOCKS_PER_SEC);
    }
    
    void wait() { 
        while (clock() == clock())
            ;
    }
    
    template 
    struct cmp1 { 
        bool operator()(T const &a, T const &b) { 
            return a < b;
        }
    };
    
    template 
    bool cmp2(T const &a, T const &b) { 
        return a());
            total += clock()- start;
        }
        report("std::sort (template class comparator)", total);
    
        total = 0;
        for (int i=0; i);
            total += clock()- start;
        }
        report("std::sort (template func comparator)", total);
    
        total = 0;
        for (int i=0; i array3(array1, array1+size);
            wait();
            clock_t start = clock();
            std::sort(array3.begin(), array3.end());
            total += clock()-start;
        }
        report("std::sort (vector)", total);
    
        return 0;
    } 
    

    Compiling this with VC++ 9/VS 2008 using cl /O2b2 /GL sortbench3.cpp, I get:

    qsort took 3.393000 seconds
    std::sort (array) took 1.724000 seconds
    std::sort (template class comparator) took 1.725000 seconds
    std::sort (template func comparator) took 2.725000 seconds
    std::sort (func comparator) took 2.505000 seconds
    std::sort (vector) took 1.721000 seconds
    

    I believe these fall fairly cleanly into three groups: using sort with the default comparison, and using the template class produced the fastest code. Using either the function or template function is clearly slower. Using qsort is (surprisingly to some) the slowest of all, by around a 2:1 margin.

    The difference between cmp2 and cmp3 appears to stem entirely from passing by reference vs. value -- if you change cmp2 to take its arguments by value, its speed matches cmp3 exactly (at least in my testing). The difference is that if you know the type is going to be int, you'll almost certainly pass by value, whereas for generic T, you'll usually pass by const reference (just in case it's something that's more expensive to copy).

提交回复
热议问题