定义于头文件
算法库提供大量用途的函数(例如查找、排序、计数、操作),它们在元素范围上操作。注意范围定义为 [first, last) ,其中 last 指代要查询或修改的最后元素的后一个元素。
std::stable_partition
| template< class BidirIt, class UnaryPredicate > | (1) | |
| template< class ExecutionPolicy, class BidirIt, class UnaryPredicate > | (2) | (C++17 起) |
重排序范围 [first, last) 中的元素,使得所有谓词 p 对其返回 true 的元素先于谓词 p 对其返回 false 的元素。保持元素的相对顺序。
2) 同 (1) ,但按照 policy 执行。此重载仅若 std::is_execution_policy_v
| first, last | - | 要重排序的元素范围 |
| policy | - | 所用的执行策略。细节见执行策略。 |
| p | - | 若元素应先序于其他元素则返回 true 的一元谓词。 对每个(可为 const 的) |
| 类型要求 | ||
- BidirIt 必须满足值可交换 (ValueSwappable) 和 遗留双向迭代器 (LegacyBidirectionalIterator) 的要求。 | ||
- 解引用 BidirIt 结果的类型必须满足可移动赋值 (MoveAssignable) 和可移动构造 (MoveConstructible) 的要求。 | ||
- UnaryPredicate 必须满足谓词 (Predicate) 的要求。 | ||
指向第二范围首元素的迭代器
给定 N = last - first
1) 若有充足内存,则准确应用 N 次谓词及交换 O(N) 次。若内存不充足,则至多交换 N log N 次。
2) O(N log N) 次交换及应用 O(N) 次谓词。
拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:
ExecutionPolicy 为标准策略之一,则调用 std::terminate 。对于任何其他 ExecutionPolicy ,行为是实现定义的。此函数试图分配临时缓冲区。若分配失败,则选择较低效的算法。
- #include <iostream>
- #include <algorithm>
- #include <functional>
- #include <vector>
- #include <iterator>
- #include <time.h>
-
- using namespace std;
-
- struct Cell
- {
- int x;
- int y;
-
- Cell &operator +=(const Cell &cell)
- {
- x += cell.x;
- y += cell.y;
- return *this;
- }
-
- bool operator <(const Cell &cell) const
- {
- if (x == cell.x)
- {
- return y < cell.y;
- }
- else
- {
- return x < cell.x;
- }
- }
- };
-
- std::ostream &operator<<(std::ostream &os, const Cell &cell)
- {
- os << "{" << cell.x << "," << cell.y << "}";
- return os;
- }
-
- int main()
- {
- srand((unsigned)time(NULL));;
-
- std::cout.setf(std::ios_base::boolalpha);
-
- auto func1 = []()
- {
- int n = std::rand() % 10 + 100;
- Cell cell{n, n};
- return cell;
- };
-
- vector<Cell> cells(8);
- std::generate(cells.begin(), cells.end(), func1);
- std::cout << "cells : ";
- std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
- std::cout << std::endl;
-
- auto is_even = [](const Cell & cell)
- {
- return cell.x % 2 == 1 && cell.y % 2 == 1;
- };
- std::cout << "is_partitioned: " << std::is_partitioned(cells.begin(), cells.end(), is_even);
- std::cout << std::endl << std::endl;
-
- std::stable_partition(cells.begin(), cells.end(), is_even);
- std::cout << "cells : ";
- std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
- std::cout << std::endl;
- std::cout << "is_partitioned: " << std::is_partitioned(cells.begin(), cells.end(), is_even);
- std::cout << std::endl << std::endl;
-
- std::reverse(cells.begin(), cells.end());
- std::cout << "cells : ";
- std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
- std::cout << std::endl;
- std::cout << "is_partitioned: " << std::is_partitioned(cells.begin(), cells.end(), is_even);
- std::cout << std::endl << std::endl;
-
- std::stable_partition(cells.begin(), cells.end(), is_even);
- std::cout << "cells : ";
- std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
- std::cout << std::endl;
- std::cout << "is_partitioned: " << std::is_partitioned(cells.begin(), cells.end(), is_even);
- std::cout << std::endl << std::endl;
-
- return 0;
- }
