Namespaces
Variants

std:: stable_partition

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
stable_partition
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
Numeric operations
Operations on uninitialized memory
헤더 파일에 정의됨 <algorithm>
template < class BidirIt, class UnaryPred >
BidirIt stable_partition ( BidirIt first, BidirIt last, UnaryPred p ) ;
(1) (C++26부터 constexpr)
template < class ExecutionPolicy, class BidirIt, class UnaryPred >

BidirIt stable_partition ( ExecutionPolicy && policy,

BidirIt first, BidirIt last, UnaryPred p ) ;
(2) (C++17부터)
1) 범위 [ first , last ) 내의 요소들을 재배열하여, 조건자 p true 를 반환하는 모든 요소들이 조건자 p false 를 반환하는 요소들 앞에 오도록 합니다. 요소들의 상대적 순서는 유지됩니다.
2) (1) 과 동일하지만, policy 에 따라 실행됩니다.
이 오버로드는 다음의 모든 조건이 만족될 때만 오버로드 해결에 참여합니다:

std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> true 입니다.

(C++20 이전)

std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> true 입니다.

(C++20 이후)

다음 조건 중 하나라도 충족되면, 동작은 정의되지 않습니다:

(C++11 이전)
(C++11 이후)

목차

매개변수

first, last - 재정렬할 요소들의 범위 를 정의하는 반복자 쌍
policy - 사용할 실행 정책
p - 요소가 다른 요소들보다 먼저 정렬되어야 하는 경우 ​ true 를 반환하는 단항 predicate.

표현식 p ( v ) VT 타입의 (const 가능성 있는) 모든 인수 v 에 대해 bool 로 변환 가능해야 하며, 값 범주 와 관계없이 v 를 수정해서는 안 됩니다. 따라서 VT & 매개변수 타입은 허용되지 않으며 , VT 에 대해 이동이 복사와 동등하지 않는 한 VT 타입도 허용되지 않습니다 (C++11부터) . ​

타입 요구사항
-
BidirIt LegacyBidirectionalIterator 요구사항을 충족해야 합니다.
-
UnaryPred Predicate 요구사항을 충족해야 합니다.

반환값

두 번째 그룹의 첫 번째 요소에 대한 반복자.

복잡도

주어진 N std:: distance ( first, last ) 인 경우:

1) 정확히 N 번의 p 적용.
O(N) 교환은 충분한 추가 메모리가 있는 경우이며, 그렇지 않으면 최대 N⋅log 2 (N) 교환입니다.
2) O(N) 번의 p 적용.
N⋅log(N) 교환.

예외

ExecutionPolicy 라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:

  • 알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고 ExecutionPolicy 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른 ExecutionPolicy 의 경우 동작은 구현에 따라 정의됩니다.
  • 알고리즘이 메모리 할당에 실패하는 경우, std::bad_alloc 이 throw됩니다.

참고 사항

이 함수는 임시 버퍼 할당을 시도합니다. 할당이 실패할 경우, 덜 효율적인 알고리즘이 선택됩니다.

libc++ libstdc++ 의 구현체들도 확장 기능으로 LegacyForwardIterator s 로 표시된 범위들을 허용합니다.

기능 테스트 매크로 표준 기능
__cpp_lib_constexpr_algorithms 202306L (C++26) constexpr 안정 정렬 ( 1 )

예제

#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
    std::vector<int> v{0, 0, 3, -1, 2, 4, 5, 0, 7};
    std::stable_partition(v.begin(), v.end(), [](int n) { return n > 0; });
    for (int n : v)
        std::cout << n << ' ';
    std::cout << '\n';
}

출력:

3 2 4 5 7 0 0 -1 0

결함 보고서

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

DR 적용 대상 게시된 동작 올바른 동작
LWG 2150 C++98 std::stable_partition p 를 만족하는 하나의 요소를
p 를 만족하지 않는 하나의 요소 앞에
배치하도록만 요구되었음
요구 사항을
수정함

참고 항목

원소들의 범위를 두 그룹으로 나눔
(function template)
원소들의 상대적 순서를 유지하면서 두 그룹으로 나눔
(algorithm function object)