《C++ primer plus》附录G:STL方法与函数(五) 学习笔记

来源:互联网 发布:logic pro x windows版 编辑:程序博客网 时间:2024/05/22 02:29

数字操作


1.accumulate()

求和,sum初始化为init,第一个版本执行sum = sum + *i,第二个版本执行sum = binary_op(sum,*i),返回值为sum
template< class InputIt, class T >T accumulate( InputIt first, InputIt last, T init );template< class InputIt, class T, class BinaryOperation >T accumulate( InputIt first, InputIt last, T init,              BinaryOperation op );


#include <iostream>#include <vector>#include <numeric>#include <string>#include <functional> int main(){    std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};     int sum = std::accumulate(v.begin(), v.end(), 0);     int product = std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>());     std::string s = std::accumulate(std::next(v.begin()), v.end(),                                    std::to_string(v[0]), // start with first element                                    [](std::string a, int b) {                                        return a + '-' + std::to_string(b);                                    });     std::cout << "sum: " << sum << '\n'              << "product: " << product << '\n'              << "dash-separated string: " << s << '\n';}/*输出结果为:sum: 55product: 3628800dash-separated string: 1-2-3-4-5-6-7-8-9-10 */

2.inner_product()

求内部乘积,acc初始化为init,第一个版本执行acc = *j + *i(i和j分别是两个区间的迭代器),第二个版本执行acc = binary_op(*j,*i) ,返回值为acc

template< class InputIt1, class InputIt2, class T >T inner_product( InputIt1 first1, InputIt1 last1,                 InputIt2 first2, T value );template<class InputIt1, class InputIt2, class T,         class BinaryOperation1, class BinaryOperation2> T inner_product( InputIt1 first1, InputIt1 last1,                 InputIt2 first2, T value,                 BinaryOperation1 op1,                 BinaryOperation2 op2 );

3.adjacent_difference()

函数将*first赋给result,目标区间随后的位置被赋为源区间相邻区间的差集(或binary_op的等价物),即result+1被赋为*(first+1) - *first或binary_op(*(first+1),*first)

template< class InputIt, class OutputIt >OutputIt adjacent_difference( InputIt first, InputIt last,                               OutputIt d_first );template< class InputIt, class OutputIt, class BinaryOperation >OutputIt adjacent_difference( InputIt first, InputIt last,                               OutputIt d_first, BinaryOperation op );

4.partial_sum()

从result开始的序列的第n个元素是从first开始的前n个元素的总和(或binary_op的等价物)

template< class InputIt, class OutputIt >OutputIt partial_sum( InputIt first, InputIt last, OutputIt d_first );template< class InputIt, class OutputIt, class BinaryOperation >OutputIt partial_sum( InputIt first, InputIt last, OutputIt d_first,                      BinaryOperation op );


#include <numeric>#include <vector>#include <iostream>#include <iterator>#include <functional> int main(){    std::vector<int> v = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2}; // or std::vector<int>v(10, 2);     std::cout << "The first 10 even numbers are: ";    std::partial_sum(v.begin(), v.end(),                      std::ostream_iterator<int>(std::cout, " "));    std::cout << '\n';     std::partial_sum(v.begin(), v.end(), v.begin(), std::multiplies<int>());    std::cout << "The first 10 powers of 2 are: ";    for (auto n : v) {        std::cout << n << " ";    }    std::cout << '\n';}/*输出结果为:The first 10 even numbers are: 2 4 6 8 10 12 14 16 18 20 The first 10 powers of 2 are: 2 4 8 16 32 64 128 256 512 1024 */


0 0
原创粉丝点击