C++ partition_copy(STL partition_copy)算法使用详解
2023-06-13 09:11:55 时间
partition_copy() 算法以和 stable_partition() 相同的方式对序列进行分区,但那些使谓词返回 true 的元素会被复制到一个单独的序列中,使谓词返回 false 的那些元素会被复制到第三个序列中。这个操作不会改变原始序列。
原始序列由前两个参数指定,它们必须是输入迭代器。第 3 个参数用来确定目的序列的开始位置,它会保存那些使谓词返回 true 的元素。第 4 个参数用来确定另一个目的序列的开始位置,它会保存那些使谓词返回 false 的元素。第 5 个参数是用来分区元素的谓词。下面是一个展示 partition_copy() 用法的完整程序:
// Using partition_copy() to find values above average and below average #include iostream // For standard streams #include vector // For vector container #include algorithm // For partition_copy(), copy() #include numeric // For accumulate() #include iterator // For back_inserter, ostream_iterator int main() std::vector double temperatures {65, 75, 56, 48, 31, 28, 32, 29, 40, 41, 44, 50}; std::vector double low_t; // Stores below average temperatures std::vector double high_t; // Stores average or above temperatures auto average = std::accumulate(std::begin(temperatures), std::end(temperatures), 0.0) / temperatures.size(); std::partition_copy(std::begin(temperatures), std::end(temperatures), std::back_inserter(low_t), std::back_inserter(high_t),[average](double t) { return t average; }); // Output below average temperatures std::copy(std::begin(low_t), std::end(low_t), std::ostream_iterator double {std::cout, }); std::cout std::endl; // Output average or above temperatures std::copy(std::begin(high_t), std::end(high_t), std::ostream_iterator double {std::cout, }); std::cout std::endl; }
这段代码所做的事情和先前介绍的 stable_partition() 相同,但小于平均值的元素会被复制到 low_t 容器中,大于等于平均值的元素会被复制到 high_t 容器中。输出语句可以对此进行验证,它们产生的输出如下:
31 28 32 29 40 41 44
65 75 56 48 50
注意,main() 中的这段代码使用辅助函数 back_inserter() 创建的 back_insert_iterator 对象作为 partition_copy() 调用中两个目的容器的迭代器。back_insert_iterator 通过调用 push_back() 向容器中添加新元素,使用这种方式可以不需要提前知道容器中保存了多少元素。如果对目的序列使用开始迭代器,在执行这个操作前,为了可以复制尽可能多的元素,目的序列中必须有足够的元素。
注意,如果输入序列和输出序列重叠,这个算法将无法正常工作。
21791.html
gohtml相关文章
- c++string截取字符串
- EasyC++46,静态持续变量
- 算法(c/c++入门)第一章第一节
- C++算法与数据结构之map
- C++基础——C++面向对象之类、对象与继承基础总结(类和对象概念、构造函数与析构函数、this指针、继承)
- 详细步骤讲解matlab代码通过Coder编译为c++并用vs2019调用
- C++ 不知算法系列之深入动态规划算法思想
- C++ 数学与算法系列之认识格雷码
- C++数学与算法系列之排列和组合
- C++数学与算法系列之初等数论
- c++的链表-链表入门(C++)
- 【C++】手撕红黑树
- C++关联容器,STL关联容器
- C++ vector的使用、创建及初始化
- C++ set_union(STL set_union)算法详解
- C++ merge和inplace_merge(STL merge和inplace_merge)算法详解
- C++ adjacent_find(STL adjacent_find)算法使用详解
- C++ partition_point(STL partition_point)算法使用详解
- C++ upper_bound(STL upper_bound)二分查找算法详解
- C++汉诺塔递归算法完全攻略
- 为什么C++ map容器emplace()、emplace_hint()的执行效率比insert()高?
- C++基本算法冒泡法、交换法、选择法、实现代码集合
- 海量数据处理系列之:用C++实现Bitmap算法
- c++基础语法:构造函数与析构函数
- C++基本算法思想之递推算法思想
- C++采用openfilename打开文件对话框用法实例
- C++基础入门教程(三):数组、字符串、结构体、共用体
- VC++实现通过API来查看程序错误信息的方法
- C++实现查找中位数的O(N)算法和Kmin算法
- C++实现N个骰子的点数算法