Namespaces
Variants

std:: qsort

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
qsort
Numeric operations
Operations on uninitialized memory
헤더 파일에 정의됨 <cstdlib>
void qsort ( void * ptr, std:: size_t count,

std:: size_t size, /* c-compare-pred */ * comp ) ;
void qsort ( void * ptr, std:: size_t count,

std:: size_t size, /* compare-pred */ * comp ) ;
(1)
extern "C" using /* c-compare-pred */ = int ( const void * , const void * ) ;
extern "C++" using /* compare-pred */ = int ( const void * , const void * ) ;
(2) ( 설명 전용* )

ptr 가 가리키는 주어진 배열을 오름차순으로 정렬합니다. 배열은 count 개의 요소를 포함하며, 각 요소의 크기는 size 바이트입니다. comp 가 가리키는 함수는 객체 비교에 사용됩니다.

만약 comp 가 두 요소를 동등하다고 표시하면, 그들의 순서는 지정되지 않습니다.

배열 요소의 타입이 PODType (C++11 이전) TriviallyCopyable type (C++11 이후) 가 아닌 경우, 동작은 정의되지 않습니다.

목차

매개변수

ptr - 정렬할 배열에 대한 포인터
count - 배열의 요소 개수
size - 배열 내 각 요소의 바이트 단위 크기
comp - 비교 함수로, 첫 번째 인수가 두 번째 인수보다 작으면 음의 정수 값을, 첫 번째 인수가 두 번째 인수보다 크면 양의 정수 값을, 인수들이 동등하면 0을 반환합니다.

비교 함수의 시그니처는 다음에 상응해야 합니다:

int cmp ( const void * a, const void * b ) ;

이 함수는 전달된 객체를 수정해서는 안 되며, 배열 내 위치에 관계없이 동일한 객체에 대해 호출될 때 일관된 결과를 반환해야 합니다.

반환값

(없음)

참고 사항

이름과는 달리, C++, C 및 POSIX 표준은 이 함수가 Quicksort 를 사용하여 구현될 것을 요구하지 않으며, 어떠한 복잡도나 안정성 보장도 하지 않습니다.

C++ 표준 라이브러리가 제공하는 두 오버로드는 comp 매개변수의 타입이 서로 다르기 때문에 구별됩니다 ( language linkage 는 해당 타입의 일부입니다).

예제

다음 코드는 qsort() 를 사용하여 정수 배열을 정렬합니다:

#include <array>
#include <climits>
#include <compare>
#include <cstdlib>
#include <iostream>
int main()
{
    std::array a{-2, 99, 0, -743, INT_MAX, 2, INT_MIN, 4};
    std::qsort
    (
        a.data(),
        a.size(),
        sizeof(decltype(a)::value_type),
        [](const void* x, const void* y)
        {
            const int arg1 = *static_cast<const int*>(x);
            const int arg2 = *static_cast<const int*>(y);
            const auto cmp = arg1 <=> arg2;
            if (cmp < 0)
                return -1;
            if (cmp > 0)
                return 1;
            return 0;
        }
    );
    for (int ai : a)
        std::cout << ai << ' ';
    std::cout << '\n';
}

출력:

-2147483648 -743 -2 0 2 4 99 2147483647

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 405 C++98 배열 요소가 어떤 타입이든 가질 수 있었음 PODType 으로 제한됨

참고 항목

지정되지 않은 타입의 요소를 배열에서 검색합니다
(함수)
범위를 오름차순으로 정렬합니다
(함수 템플릿)
(C++11) (C++26에서 사용 중단됨)
타입이 trivial인지 확인합니다
(클래스 템플릿)
C 문서 for qsort