C++算法库 (3)划分操作
来源:互联网 发布:java打印倾斜菱形 编辑:程序博客网 时间:2024/06/18 08:52
/*is_partitioned 检测某个范围是否按指定谓词划分过*/#include <iostream>#include <algorithm>#include <array>using namespace std;bool IsOdd(int i) {return (i % 2) == 1;}int main(int argc, const char * argv[]){ array<int, 7> foo = {1,2,3,4,5,6,7}; //print contents: cout<<"foo:"; for (int& x:foo) cout<<" "<<x; if (is_partitioned(foo.begin(), foo.end(), IsOdd)) cout<<" (partitioned)"<<endl; else cout<<" (not partitioned)"<<endl; //partition array: partition(foo.begin(), foo.end(), IsOdd); //print contents again: cout<<"foo:"; for (int& x:foo) cout<<" "<<x; if (is_partitioned(foo.begin(), foo.end(), IsOdd)) cout<<" (partitioned)"<<endl; else cout<<" (not partitioned)"<<endl; return 0;}
输出结果:
foo: 1 2 3 4 5 6 7 (not partitioned)foo: 1 7 3 5 4 6 2 (partitioned)
/*partition 将某个范围划分为两组*/#include <iostream>#include <algorithm>#include <vector>using namespace std;bool IsOdd(int i) {return (i % 2) == 1;}int main(int argc, const char * argv[]){ vector<int> myvector; vector<int>::iterator it; //set some values: for (int i = 1; i < 10; ++i) myvector.push_back(i); vector<int>::iterator bound; bound = partition(myvector.begin(), myvector.end(), IsOdd); //print out content: cout<<"odd elements:"; for (it = myvector.begin(); it != bound; ++it) cout<<" "<<*it; cout<<endl; cout<<"even elements:"; for (it = bound;it != myvector.end(); ++it ) cout<<" "<<*it; cout<<endl; return 0;}
输出结果:
odd elements: 1 9 3 7 5even elements: 6 4 8 2
/*partition_copy 拷贝指定范围的划分结果*/#include <iostream>#include <algorithm>#include <vector>using namespace std;bool IsOdd(int i) {return (i % 2) == 1;}int main(int argc, const char * argv[]){ vector<int> foo; vector<int> odd,even; vector<int>::iterator it; //set some values: for (int i = 1; i < 10; ++i) foo.push_back(i); unsigned n = (unsigned)count_if(foo.begin(), foo.end(), IsOdd); odd.resize(n); even.resize(foo.size() - n); //partition: partition_copy(foo.begin(), foo.end(), odd.begin(), even.begin(), IsOdd); //print contents: cout<<"odd: "; for (int& x:odd) cout<<" "<<x; cout<<endl; cout<<"even: "; for (int& x:even) cout<<" "<<x; cout<<endl; return 0;}
输出结果:
odd: 1 3 5 7 9even: 2 4 6 8
/*partition_point 返回被划分范围的划分点*/#include <iostream>#include <algorithm>#include <vector>using namespace std;bool IsOdd(int i) {return (i % 2) == 1;}int main(int argc, const char * argv[]){ vector<int> foo; vector<int> odd; //set some values: for (int i = 1; i < 10; ++i) foo.push_back(i); partition(foo.begin(), foo.end(), IsOdd); auto it = partition_point(foo.begin(), foo.end(), IsOdd); odd.assign(foo.begin(), it); //print contents of odd: cout<<"odd:"; for (int& x:odd) cout<<" "<<x; cout<<endl; return 0;}
输出结果:
odd: 1 9 3 7 5
/*stable_partition 稳定划分,两组元素各维持相对顺序*/#include <iostream>#include <algorithm>#include <vector>using namespace std;bool IsOdd(int i) {return (i % 2) == 1;}int main(int argc, const char * argv[]){ vector<int> myvector; vector<int>::iterator it; vector<int>::iterator bound; for (int i = 1; i < 10; ++i) myvector.push_back(i); bound = stable_partition(myvector.begin(), myvector.end(), IsOdd); //print out content: cout<<"odd elements:"; for (it = myvector.begin(); it != bound ; ++it) cout<<" "<<*it; cout<<endl; cout<<"even elements:"; for (it = bound; it != myvector.end(); ++it) cout<<" "<<*it; cout<<endl; return 0;}
odd elements: 1 3 5 7 9even elements: 2 4 6 8
0 0
- C++ 算法库(3) 划分操作
- C++算法库 (3)划分操作
- c++: 递归算法整数划分问题
- 编程算法 - 划分数 代码(C)
- 划分算法
- 划分算法
- 划分算法
- 整数划分的非递归算法(C语言)
- 整数划分问题的递归算法-c语言
- C算法-------指针操作
- C算法->指针操作
- 算法设计:整数划分
- 算法设计:整数划分
- [算法]整数划分问题
- 棋盘划分----分治算法
- 【算法】划分 partition
- java划分算法
- 递归算法---整数划分
- log4net:将log实时显示在textbox中
- linux用命令制作再生龙u盘启动
- 内存管理单元-MMU
- c语言迷宫最短路径
- eclipse快捷键 10个最有用的快捷键
- C++算法库 (3)划分操作
- 解读《TCP/IP详解》(卷1):05章:RARP(逆地址解析协议)
- Could not launch xxx failed to get the task for process xxx
- MongoDB设置访问权限、设置用户
- getsockname
- 【ci框架】常用总结
- 非递归遍历文件
- PAXOS算法详解2
- hdu3622 2-sat问题,二分+判断有无解即可。