std::sort does not always call std::swap

后端 未结 3 1318
独厮守ぢ
独厮守ぢ 2020-11-27 21:51

Consider the following code:

#include 
#include 
#include 

namespace my_space
{

struct A
{
    double  a;
           


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 22:05

    I modified the code to be more verbose. The sorting for 20 elements uses many swaps, uses assignment end copy. Sorting for 4 elements uses only assignment and copy. Don't know about the specs, but it might be something to go on.

    #include 
    #include 
    #include 
    
    namespace my_space
    {
    
    struct A
    {
        double  a;
        double* b;
        A()
            : a(0)
            , b(NULL)
        { }
        A(const A &rhs)
            : a(rhs.a)
            , b(rhs.b)
        {
            std::cerr << "copy" << std::endl;
        }
        A& operator=(A const &rhs)
        {
            if(this==&rhs)
                    return *this;
            a = rhs.a;
            b = rhs.b;
            std::cerr << "=" << std::endl;
            return *this;
        }
        bool operator<(const A& rhs) const
        {
            return this->a < rhs.a;
        }
    };
    
    void swap(A& lhs, A& rhs)
    {
        std::cerr << "My swap.\n";
        std::swap(lhs.a, rhs.a);
        std::swap(lhs.b, rhs.b);
    }
    
    } // namespace my_space
    
    int main()
    {
        const int n = 20;
    
            std::cerr << "=== TEST CASE: n = " << n << std::endl;
        std::cerr << "=== FILL ===" << std::endl;
        std::vector vec(n);
        for (int i = 0; i < n; ++i) {
            vec[i].a = -i;
        }
    
        std::cerr << "=== PRINT ===" << std::endl;
        for (int i = 0; i < n; ++i) {
            std::cerr << vec[i].a << " ";
        }
        std::cerr << "\n";
    
        std::cerr << "=== SORT ===" << std::endl;
        std::sort(vec.begin(), vec.end());
    
        std::cerr << "=== PRINT ===" << std::endl;
        for (int i = 0; i < n; ++i) {
            std::cerr << vec[i].a << " ";
        }
        std::cerr << "\n";
    }
    

    Outputs

    === TEST CASE: n = 4
    === FILL ===
    copy
    copy
    copy
    copy
    === PRINT ===
    0 -1 -2 -3
    === SORT ===
    copy
    =
    =
    copy
    =
    =
    =
    copy
    =
    =
    =
    =
    === PRINT ===
    -3 -2 -1 0
    

    And

    === TEST CASE: n = 20
    === FILL ===
    copy
    copy
    copy
    copy
    copy
    copy
    copy
    copy
    copy
    copy
    copy
    copy
    copy
    copy
    copy
    copy
    copy
    copy
    copy
    copy
    === PRINT ===
    0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19
    === SORT ===
    copy
    My swap.
    My swap.
    My swap.
    My swap.
    My swap.
    My swap.
    My swap.
    My swap.
    My swap.
    My swap.
    copy
    copy
    =
    copy
    copy
    =
    copy
    copy
    =
    copy
    copy
    =
    copy
    copy
    =
    copy
    copy
    =
    copy
    copy
    =
    copy
    copy
    =
    copy
    copy
    =
    copy
    copy
    =
    copy
    copy
    =
    copy
    copy
    =
    copy
    copy
    =
    copy
    copy
    =
    copy
    copy
    =
    copy
    =
    copy
    =
    copy
    =
    copy
    =
    === PRINT ===
    -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
    

提交回复
热议问题