C++ partition_copy 图片看不了?点击切换HTTP 返回上层
partition_copy() 算法以和 stable_partition() 相同的方式对序列进行分区,但那些使谓词返回 true 的元素会被复制到一个单独的序列中,使谓词返回 false 的那些元素会被复制到第三个序列中。这个操作不会改变原始序列。
原始序列由前两个参数指定,它们必须是输入迭代器。第 3 个参数用来确定目的序列的开始位置,它会保存那些使谓词返回 true 的元素。第 4 个参数用来确定另一个目的序列的开始位置,它会保存那些使谓词返回 false 的元素。第 5 个参数是用来分区元素的谓词。下面是一个展示 partition_copy() 用法的完整程序:
这段代码所做的事情和先前介绍的 stable_partition() 相同,但小于平均值的元素会被复制到 low_t 容器中,大于等于平均值的元素会被复制到 high_t 容器中。输出语句可以对此进行验证,它们产生的输出如下:
注意,如果输入序列和输出序列重叠,这个算法将无法正常工作。
原始序列由前两个参数指定,它们必须是输入迭代器。第 3 个参数用来确定目的序列的开始位置,它会保存那些使谓词返回 true 的元素。第 4 个参数用来确定另一个目的序列的开始位置,它会保存那些使谓词返回 false 的元素。第 5 个参数是用来分区元素的谓词。下面是一个展示 partition_copy() 用法的完整程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // 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; } |
31 28 32 29 40 41 44
65 75 56 48 50
注意,如果输入序列和输出序列重叠,这个算法将无法正常工作。