C++ 头文件algorithm 1——partition
来源:互联网 发布:playclub捏脸详细数据 编辑:程序博客网 时间:2024/06/07 03:13
partition
ForwardIterator partition (ForwardIterator first, ForwardIterator last, UnaryPredicate pred);
first,last表示元素范围[first,last);
pred:表示分组条件,它是一个一元函数,也可以是函数指针或函数对象。
返回值是一个迭代器,指向第二个元素组的第一个元素;如果这个组是空的,则是最后一个元素。
example
#include<iostream>#include<algorithm>#include<vector>using namespace std;bool isOdd(int i){ return (i % 2) == 1; }int main(){ vector<int> myvector; for (int i = 1; i < 10; ++i) myvector.push_back(i); vector<int>::iterator bound; bound = partition(myvector.begin(), myvector.end(), isOdd); cout << "odd element:"; for (vector<int>::iterator it = myvector.begin(); it != bound; ++it) cout << " " << *it; cout << "\n"; cout << "even element:"; for (vector<int>::iterator it = bound; it != myvector.end(); ++it) cout << " " << *it; cout << "\n"; return 0;}
output
is_partitioned
bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred)
first,last表示元素范围[first,last);
pred: 一元函数接受集合内的一个元素作为参数,并返回一个bool值。返回的值表示元素是否属于第一个组(如果是true,则该元素将在返回false的所有元素之前)。函数不会改变参数值,它也可以是函数指针或函数对象。
若范围[first,last)内所有元素满足pred,返回true;若元素集合为空,也返回true。
example
#include<iostream>#include<algorithm>#include<array>using namespace std;bool isOdd(int i){ return (i % 2) == 1; }int main(){ array<int, 7> test={1,2,3,4,5,6,7}; //print out for (int& x : test) cout << x << " "; if (is_partitioned(test.begin(), test.end(), isOdd)) cout << "partitioned\n"; else cout << "no partitioned\n"; //partition partition(test.begin(), test.end(), isOdd); //print out again for (int& x : test) cout << x << " "; if (is_partitioned(test.begin(), test.end(), isOdd)) cout << "partitioned\n"; else cout << "no partitioned\n";}
output
stable_partition
BidirectionalIterator stable_partition (BidirectionalIterator first,
BidirectionalIterator last,
UnaryPredicate pred);
stable_partition 与partition不同,每个组中元素的相对顺序是保留的。参数first,last,pred同partition。
example
#include<iostream>#include<algorithm>#include<vector>using namespace std;bool isOdd(int i){ return (i % 2) == 1; }int main(){ vector<int> myvector; for (int i = 1; i < 10; ++i) myvector.push_back(i); vector<int>::iterator bound; bound = stable_partition(myvector.begin(), myvector.end(), isOdd); cout << "odd element:"; for (vector<int>::iterator it = myvector.begin(); it != bound; ++it) cout << " " << *it; cout << "\n"; cout << "even element:"; for (vector<int>::iterator it = bound; it != myvector.end(); ++it) cout << " " << *it; cout << "\n"; return 0;}
output
partition_copy
partition_copy (InputIterator first, InputIterator last, OutputIterator1 result_true, OutputIterator2 result_false, UnaryPredicate pred)
First, last表示范围[first,last);Result_true:输出迭代器到该范围的初始位置,在该范围中存储pred返回true的元素;Result_false:输出迭代器到该范围的初始位置,在该范围中存储pred返回false的元素;Pred:同partition,stable_partition。
example
#include<iostream>#include<algorithm>#include<vector>using namespace std;bool isOdd(int i){ return (i % 2) == 1; }int main(){ vector<int> test = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; vector<int> odd, even; //resize vectors to proper size unsigned n = count_if(test.begin(), test.end(), isOdd); odd.resize(n); even.resize(test.size()-n); //partition partition_copy(test.begin(), test.end(), odd.begin(), even.begin(), isOdd); cout << "odd:"; for (int& x : odd) cout << " " << x; cout << "\n"; cout << "even"; for (int& x : even) cout << " " << x; cout << "\n"; return 0;}
output
partition_point
ForwardIterator partition_point (ForwardIterator first, ForwardIterator last, UnaryPredicate pred)
返回值是迭代器,它指向在[first.last)内第一个pred不为true的元素;如果对任何元素都不正确,则指向最后一个元素。
example
#include<iostream>#include<algorithm>#include<vector>using namespace std;bool isOdd(int i){ return (i % 2) == 1; }int main(){ vector<int> test = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; vector<int> odd; stable_partition(test.begin(), test.end(), isOdd); auto it = partition_point(test.begin(), test.end(), isOdd); odd.assign(test.begin(), it); //print odd cout << "odd:"; for (int& x : odd) cout << " " << x; cout << "\n"; return 0;}
output
- C++ 头文件algorithm 1——partition
- C/C++笔试必须熟悉掌握的头文件系列(八)——algorithm
- c++-algorithm 头文件排序sort
- c++-algorithm 头文件排序sort
- C++头文件algorithm 2——Sorting
- C++头文件algorithm 3——Min/Max
- algorithm库函数——sort()和partition()
- Algorithm头文件简介
- algorithm头文件源码
- algorithm头文件
- Algorithm头文件学习
- C++ <algorithm>头文件
- <algorithm>头文件
- c++中algorithm头文件
- C++中的algorithm头文件
- Algorithm 头文件中的算法
- C++——头文件的作用
- C++——头文件的作用
- DTD与XSD区别
- Android 自定义 View
- Docker下,实战mongodb副本集(Replication)
- python的io 与java的io流的使用及对象的序列化
- STM32串口下载
- C++ 头文件algorithm 1——partition
- 1、面向对象的解释
- Tour HDU
- 2017 JUST Programming Contest 3.0 I. Move Between Numbers
- ARC081 E
- CCI(2)-- Arrays and Strings
- 第一次
- linux
- jupyter notebook的安装与使用