안녕하세요, 간단한 질문이 있습니다.
class A 
{
public:
    A(int);
    A(const A&);
    A& operator=(const A&);
    ~A();
private:
    int* ptr_;
    friend bool operator<(const A&, const A&);
    friend void swap(A&, A&);
};
A::A(int x) : 
    ptr_(new int(x))
{}
A::A(const A& rhs) :
    ptr_(rhs.ptr_ ? new int(*rhs.ptr_) : nullptr)
{}
A& A::operator = (const A & rhs)
{
    int* tmp = rhs.ptr_ ? new int(*rhs.ptr_) : nullptr;
    delete ptr_;
    ptr_ = tmp;
    return *this;
}
A::~A()
{
    delete ptr_;
}
bool operator<(const A& lhs, const A& rhs)
{
    cout << "operator<(const A&, const A&)" << endl;
    return *lhs.ptr_ < *rhs.ptr_;
}
void swap(A& lhs, A& rhs)
{
    cout << "swap(A&, A&)" << endl;
    using std::swap;
    swap(lhs.ptr_, rhs.ptr_);
}
int main()
{
    std::vector<A> v{ 33,32,31,30,29,28,27,26,25,24,23,22, 21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5, 4,3,2,1 };
    std::sort(v.begin(), v.end());
}
32 개 이상의 요소를 가진 정렬은을 호출합니다 swap. 32 개 이하의 요소를 사용하면 요소는 여전히 정렬되지만 swap호출되지는 않습니다.
- x64에서 MSVC ++ 2019를 사용하고 있습니다.
 - 언제 
swap부름을 받았으며 언제 그렇지 않습니까? 감사합니다! - 나는 
swap복사 할당에서 복사 할당 연산자와 정렬 호출을 구별하기 위해 복사 할당에 사용하지 않았습니다 . 
답변
Microsoft std::sort구현 은 다음 과 같습니다.
const int ISORT_MAX = 32;  // maximum size for insertion sort
template<class RanIt, class Diff, class Pr>
void Sort(RanIt First, RanIt Last, Diff Ideal, Pr Pred)
{
    Diff Count;
    for (; ISORT_MAX < (Count = Last - First) && 0 < Ideal; )
    {   // divide and conquer by quicksort
        pair<RanIt, RanIt> Mid = Unguarded_partition(First, Last, Pred);
        // ...
    }
    if (ISORT_MAX < Count)
    {   // heap sort if too many divisions
        Make_heap(First, Last, Pred);
        Sort_heap(First, Last, Pred);
    }
    else if (1 < Count)
        Insertion_sort(First, Last, Pred);  // small
}
정렬 할 범위가 32 개 이하인 경우 Sort삽입 정렬을 사용합니다. 삽입 정렬 사용하지 않는 swap그것의 구현 . 그렇지 않으면, 분할 및 정복 빠른 정렬이 사용됩니다. 에서 구현 이 호출 iter_swap(내부 Unguarded_partition), 한 차례 통화에서 swap:
template<class FwdIt1, class FwdIt2>
void iter_swap(FwdIt1 Left, FwdIt2 Right)
{   // swap *Left and *Right
    swap(*Left, *Right);
}
이것들은 모두 구현 세부 사항입니다. 표준 라이브러리 구현마다 다릅니다.