"partition"和“stable_partition”的思考
来源:互联网 发布:苹果cms模板怎么用 编辑:程序博客网 时间:2024/05/17 21:58
"partition"和“stable_partition”设计的目的是根据一个谓词而把容器分为两部分。可是他们具体的区别在哪里呢?下面我们来验证一下。
void elimDups(vector<string> &words){ sort(words.begin(), words.end()); auto it = unique(words.begin(),words.end()); words.erase(it, words.end()); for(auto &a : words) cout << a << " "; cout << endl;}void bigies_partition(vector<string> &words, std::size_t sz){ elimDups(words); auto pivot = partition(words.begin(),words.end(), [sz](const string &a){return a.size() < sz;}); for(auto a = words.begin(); a != pivot; ++a){ cout << *a << " "; }}void bigies_stable_partition(vector<string> &words, std::size_t sz){ elimDups(words); auto pivot = stable_partition(words.begin(),words.end(), [sz](const string &a){return a.size() < sz;}); for(auto a = words.begin(); a != pivot; ++a){ cout << *a << " "; }}int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); vector<string> v{"aa", "c", "ffff","bb","dddddd","eee","aaa","bbb"}; vector<string> v1(v); cout << "partition:" << endl; bigies_partition(v1,3); cout << endl; vector<string> v2(v); cout << "stable_partition:" << endl; bigies_stable_partition(v2,3); cout << endl; return a.exec();}
output:
partitionaa aaa bb bbb c dddddd eee ffffaa c bbstable_partitionaa aaa bb bbb c dddddd eee ffffaa bb c
可以发现,stable_partition除了根据谓词把容器划分为两部分,还可以保持容器原有元素的顺序。
0 0
- "partition"和“stable_partition”的思考
- STL之partition和stable_partition
- STL之partition和stable_partition
- partition/stable_partition详解
- partition,stable_partition,sort,stable_sort
- algorithm库介绍之---- partition()方法和 stable_partition()方法
- STL_算法_重排和分区(random_shuffle、partition、stable_partition)
- 理解你的排序操作(stable_sort,sort,partial_sort,nth_element,stable_partition,partition)
- 变异算法之划分(partition, stable_partition)
- std::stable_partition的使用
- stable_partition
- stable_partition
- stable_partition
- 泛型算法系列30:stable_partition()&&partition()&&stable_sort()
- C++学习【原创】stable_partition函数的应用
- 排序算法(三)--关于快速排序Partition的思考
- 排序算法(三)--关于快速排序Partition的思考
- kafka的partition和offset
- UVA10055 Hashmat the Brave Warrior
- Java 中二维数组的长度是否固定?
- 设计模式-行为型- 策略模式(Strategy)
- Java并发编程:volatile关键字解析
- 排序算法之插入排序(模板类)
- "partition"和“stable_partition”的思考
- 10分钟快速了解Pandas
- hdoj 5615 Jam's math problem (数学-因式分解)
- 2、Java面向对象
- 广播的启动方式,以及优缺点
- C++函数后面加const修饰
- 设计模式之单例模式
- angularjs整合ueditor简介
- tinypy源码笔记(三)——虚拟机启动过程以及字节码分析