ranges::stable_partition
ranges::stable_partition (안정 분할 — ranges)
범위를 술어 기준으로 참/거짓 그룹으로 분할하되, 그룹 내 상대 순서를 유지하는 Stable partition이에요. <algorithm> 헤더에 있어요.
출처: cppreference
본문
std::ranges::stable_partition은 범위를 술어 pred 기준으로 분할하면서, 각 그룹 내부의 원래 순서를 보존해요.
namespace std::ranges {
template< std::bidirectional_iterator I, std::sentinel_for<I> S,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
requires std::permutable<I>
constexpr ranges::subrange<I> stable_partition( I first, S last,
Pred pred, Proj proj = {} );
}
ranges::partition이 순서를 보장하지 않는 데 비해, 이 버전은 안정적이에요.- 복잡도: 충분한 임시 메모리가 있으면
N·log N의 비교/이동.
std::vector<int> v{1, 2, 3, 4, 5, 6};
std::ranges::stable_partition(v, [](int x){ return x % 2 == 0; });
// 짝수들이 원래 순서대로 앞으로
순서를 유지하며 분할해야 할 때 쓰는 ranges 버전이에요. partition보다 비싸지만 안정적이에요.