一、is_partitioned
头文件algorithm
template <class InputIterator, class UnaryPredicate>
bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred);
测试范围是否已分区
如果pred返回true的范围[first,last]中的所有元素都返回false,则返回true。
如果范围为空,则该函数返回true。
此函数模板的行为等效于:
template <class InputIterator, class UnaryPredicate>
bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred)
{
while (first!=last && pred(*first)) {
++first;
}
while (first!=last) {
if (pred(*first)) return false;
++first;
}
return true;
}
参数
- first,last
将迭代器输入到序列的初始和最终位置。 使用的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。 - pred
一元函数接受范围内的元素作为参数,并返回可转换为bool的值。 返回的值指示元素是否属于第一个组(如果为true,则在返回false的所有元素之前预期该元素)。
该函数不得修改其参数。这可以是函数指针或函数对象。
返回值
如果pred返回true的范围[first,last]中的所有元素都在其返回false的元素之前,则返回true。
否则返回false。
如果范围为空,则该函数返回true。
// is_partitioned example
#include <iostream> // std::cout
#include <algorithm> // std::is_partitioned
#include <array> // std::array
bool IsOdd (int i) { return (i%2)==1; }
int main () {
std::array<int,7> foo {1,2,3,4,5,6,7};
// print contents:
std::cout << "foo:"; for (int& x:foo) std::cout << ' ' << x;
if ( std::is_partitioned(foo.begin(),foo.end(),IsOdd) )
std::cout << " (partitioned)\n";
else
std::cout << " (not partitioned)\n";
// partition array:
std::partition (foo.begin(),foo.end(),IsOdd);
// print contents again:
std::cout << "foo:"; for (int& x:foo) std::cout << ' ' << x;
if ( std::is_partitioned(foo.begin(),foo.end(),IsOdd) )
std::cout << " (partitioned)\n";
else
std::cout << " (not partitioned)\n";
return 0;
}
复杂度
第一个和最后一个之间的距离最多为线性:为每个元素调用pred,直到找到不匹配为止。
数据范围
访问[first,last]范围内的一些(或全部)对象(最多一次)。
二、partition
头文件algorithm
【C++98】
template <class BidirectionalIterator, class UnaryPredicate>
BidirectionalIterator partition (BidirectionalIterator first,
BidirectionalIterator last, UnaryPredicate pred);
【C++11】
template <class ForwardIterator, class UnaryPredicate>
ForwardIterator partition (ForwardIterator first,
ForwardIterator last, UnaryPredicate pred);
分区范围分为两部分
重新排列范围[first,last]中的元素,使得pred返回true的所有元素都在其返回false的所有元素之前。 迭代器返回指向第二组的第一个元素。
每个组内的相对排序不一定与呼叫前相同。 有关具有类似行为但在每个组内具有稳定排序的函数,请参阅stable_partition。
此函数模板(C ++ 98)的行为等效于:
template <class BidirectionalIterator, class UnaryPredicate>
BidirectionalIterator partition (BidirectionalIterator first,
BidirectionalIterator last, UnaryPredicate pred)
{
while (first!=last) {
while (pred(*first)) {
++first;
if (first==last) return first;
}
do {
--last;
if (first==last) return first;
} while (!pred(*last));
swap (*first,*last);
++first;
}
return first;
}
参数
- first,last
【C ++98】
双向迭代器到序列的初始和最终位置进行分区。 使用的范围是[first,last),它包含first和last1之间的所有元素,包括first指向的元素,但不包括last指向的元素。
【C++11】
将迭代器转发到要分区的序列的初始和最终位置。 使用的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。
ForwardIterator应指向定义了swap的类型,并交换其参数的值。 - 一元函数接受范围内的元素作为参数,并返回可转换为bool的值。 返回的值指示元素是否要放在之前(如果为true,则将其放置在返回false的所有元素之前)。
该函数不得修改其参数。这可以是函数指针或函数对象。
返回值
迭代器,指向第二组元素的第一个元素(pred返回false的元素),如果该组为空,则为last。
// partition algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::partition
#include <vector> // std::vector
bool IsOdd (int i) { return (i%2)==1; }
int main () {
std::vector<int> myvector;
// set some values:
for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
std::vector<int>::iterator bound;
bound = std::partition (myvector.begin(), myvector.end(), IsOdd);
// print out content:
std::cout << "odd elements:";
for (std::vector<int>::iterator it=myvector.begin(); it!=bound; ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << "even elements:";
for (std::vector<int>::iterator it=bound; it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
复杂度
第一个和最后一个之间的距离线性:对每个元素应用pred,并且可能交换它们中的一些(如果迭代器类型是双向的,最多是交换的一半,否则最多那么多)。
数据范围
[first,last]范围内的对象被修改。
三、stable_partition
头文件algorithm
template <class BidirectionalIterator, class UnaryPredicate>
BidirectionalIterator stable_partition (BidirectionalIterator first,
BidirectionalIterator last,
UnaryPredicate pred);
分区范围为两个 - 稳定排序
重新排列范围[first,last]中的元素,使得pred返回true的所有元素都返回到它返回false的所有元素之前,并且与函数分区不同,每个组中元素的相对顺序被保留。
这通常使用内部临时缓冲区实现。
参数
- first,last
双向迭代器到序列的初始和最终位置进行分区。使用的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。
BidirectionalIterator应指向定义了swap的类型(并交换其参数的值),并且该类型是可移动构造和可移动分配的。 - pred
一元函数接受范围内的元素作为参数,并返回可转换为bool的值。返回的值指示元素是否要放在之前(如果为true,则将其放置在返回false的所有元素之前)。
该函数不得修改其参数。这可以是函数指针或函数对象。
返回值
迭代器,指向第二组元素的第一个元素(pred返回false的元素),如果该组为空,则为last。
// stable_partition example
#include <iostream> // std::cout
#include <algorithm> // std::stable_partition
#include <vector> // std::vector
bool IsOdd (int i) { return (i%2)==1; }
int main () {
std::vector<int> myvector;
// set some values:
for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
std::vector<int>::iterator bound;
bound = std::stable_partition (myvector.begin(), myvector.end(), IsOdd);
// print out content:
std::cout << "odd elements:";
for (std::vector<int>::iterator it=myvector.begin(); it!=bound; ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << "even elements:";
for (std::vector<int>::iterator it=bound; it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
复杂度
如果有足够的额外内存可用,则第一个和最后一个之间的距离是线性的:对每个元素应用pred一次,并执行多个元素移动。
否则,最多为linearithmic:执行最多N * log(N)个元素交换(其中N是上面的距离)。 它也只对每个元素应用了一次pred。
数据范围
[first,last]范围内的对象被修改。
四、partition_copy
头文件algorithm
template <class InputIterator, class OutputIterator1,
class OutputIterator2, class UnaryPredicate pred>
pair<OutputIterator1,OutputIterator2>
partition_copy (InputIterator first, InputIterator last,
OutputIterator1 result_true, OutputIterator2 result_false,
UnaryPredicate pred);
分区范围分为两部分
将pred返回true的范围[first,last]中的元素复制到result_true指向的范围中,将那些元素中的元素复制到result_false指向的范围内。
此函数模板的行为等效于:
template <class InputIterator, class OutputIterator1,
class OutputIterator2, class UnaryPredicate pred>
pair<OutputIterator1,OutputIterator2>
partition_copy (InputIterator first, InputIterator last,
OutputIterator1 result_true, OutputIterator2 result_false,
UnaryPredicate pred)
{
while (first!=last) {
if (pred(*first)) {
*result_true = *first;
++result_true;
}
else {
*result_false = *first;
++result_false;
}
++first;
}
return std::make_pair (result_true,result_false);
}
参数
- first,last
将迭代器输入到要复制分区的范围的初始位置和最终位置。使用的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。 - result_true
将迭代器输出到存储器返回true的元素所在范围的初始位置。 - result_false
将迭代器输出到存储器返回false的元素的范围的初始位置。 - pred
一元函数接受InputIterator指向的元素作为参数,并返回一个可转换为bool的值。返回的值指示元素复制到的结果范围。
该函数不得修改其参数。这可以是函数指针或函数对象。
范围不得重叠。 输出迭代器类型指向的类型应支持为[first,last]范围内的元素赋值。
返回值
生成的序列末尾的一对迭代器由result_true和result_false指向,各自。
它的成员首先指向复制到pred返回true的元素序列的最后一个元素之后的元素。
其成员第二个指向复制到pred返回false的元素序列的最后一个元素后面的元素。
// partition_copy example
#include <iostream> // std::cout
#include <algorithm> // std::partition_copy, std::count_if
#include <vector> // std::vector
bool IsOdd (int i) { return (i%2)==1; }
int main () {
std::vector<int> foo {1,2,3,4,5,6,7,8,9};
std::vector<int> odd, even;
// resize vectors to proper size:
unsigned n = std::count_if (foo.begin(), foo.end(), IsOdd);
odd.resize(n); even.resize(foo.size()-n);
// partition:
std::partition_copy (foo.begin(), foo.end(), odd.begin(), even.begin(), IsOdd);
// print contents:
std::cout << "odd: "; for (int& x:odd) std::cout << ' ' << x; std::cout << '\n';
std::cout << "even: "; for (int& x:even) std::cout << ' ' << x; std::cout << '\n';
return 0;
}
复杂度
第一个和最后一个之间的距离线性:调用pred并为每个元素执行一次赋值。
数据范围
访问[first,last]范围内的对象(每个对应一次)。
result_true和result_false指向的范围中的对象直到返回的迭代器指向的元素被修改(每个都恰好一次)。
五、partition_point
头文件algorithm
template <class ForwardIterator, class UnaryPredicate>
ForwardIterator partition_point (ForwardIterator first, ForwardIterator last,
UnaryPredicate pred);
获取分区点
返回分区范围[first,last]中第一个元素的迭代器,其中pred不为true,表示其分区点。
范围中的元素已经被分区,就好像使用相同的参数调用了分区一样。
该函数通过比较排序范围的非连续元素来优化执行的比较次数,这对于随机访问迭代器特别有效。
此函数模板的行为等效于:
template <class ForwardIterator, class UnaryPredicate>
ForwardIterator partition_point (ForwardIterator first, ForwardIterator last,
UnaryPredicate pred)
{
auto n = distance(first,last);
while (n>0)
{
ForwardIterator it = first;
auto step = n/2;
std::advance (it,step);
if (pred(*it)) { first=++it; n-=step+1; }
else n=step;
}
return first;
}
参数
- first,last
将迭代器转发到分区序列的初始和最终位置。 检查的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。 - pred
一元函数接受范围内的元素作为参数,并返回可转换为bool的值。 返回的值表示元素是否在分区点之前(如果为true,则在之前;如果为false则在其之前或之后)。
该函数不得修改其参数。这可以是函数指针或函数对象。
返回值
分区范围[first,last]中第一个元素的迭代器,其中pred不为true,如果不是任何元素,则为last。
// partition_point example
#include <iostream> // std::cout
#include <algorithm> // std::partition, std::partition_point
#include <vector> // std::vector
bool IsOdd (int i) { return (i%2)==1; }
int main () {
std::vector<int> foo {1,2,3,4,5,6,7,8,9};
std::vector<int> odd;
std::partition (foo.begin(),foo.end(),IsOdd);
auto it = std::partition_point(foo.begin(),foo.end(),IsOdd);
odd.assign (foo.begin(),it);
// print contents of odd:
std::cout << "odd:";
for (int& x:odd) std::cout << ' ' << x;
std::cout << '\n';
return 0;
}
复杂度
平均而言,第一个和最后一个距离的对数:执行近似log2(N)+2个元素比较(其中N是此距离)。
在非随机访问迭代器上,迭代器的进展产生了N平均额外的线性复杂度。
数据范围
访问[first,last]范围内的一些对象。