C++ STL 算法库学习之adjacent_difference,count,partial_sum,accumulate,count_if详解

1.连续计算类

adjacent_difference,count,partial_sum,accumulate,count_if 均是以线性复杂度

accumulate
#include <numeric>
TYPE accumulate( iterator start, iterator end, TYPE val );
TYPE accumulate( iterator start, iterator end, TYPE val, BinaryFunction f );

The accummulate() function computes the sum of val and all of the elements in the range[start,end).

If the binary function f if specified, it is used instead of the + operator to perform thesummation

#include "stdafx.h"
#include <iostream>
#include <functional>
#include <numeric>
#include <vector>


#if  0
int main()
{
    using namespace std;
    vector<int>v1, v2(20);
    vector<int>::iterator iter1, iter2;

    for (auto i = 1; i < 21; i++)
    {
        v1.push_back(i);
    }

    cout << "V1:" << endl;
    for (auto i = 0; i < 20; i++)
    {
        cout << v1[i] << " ";
    }
    cout << endl;

    auto total = accumulate(v1.begin(), v1.end(), 0);
    cout << "0 begin:"<<total << endl;
    total = accumulate(v1.begin(), v1.end(), 10);
    cout << "10 begin"<<total << endl;

    int j = 0;
    int partotal;
    for (iter1 = v1.begin(); iter1 != v1.end(); iter1++)
    {
        partotal = accumulate(v1.begin(), iter1 + 1, 0);
        v2[j] = partotal;
        j++;
    }

    cout << "partotal:" << partotal << endl;
    cout << "The vector of partial sums is:\n ( ";
    for (iter2 = v2.begin(); iter2 != v2.end(); iter2++)
        cout << *iter2 << " ";
    cout << ")." << endl << endl;

    //output: 1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 210 

    // The second member function for the accumulated product
    int tmp[10] = {1,2,3,4,5,6,7,8,9,10};
    vector <int> v3(tmp,tmp+10), v4(10);
    int ptotal;
    ptotal = accumulate(v3.begin(), v3.end(), 1, multiplies<int>());
    cout << "The product of the integers from 1 to 10 is: "
        << ptotal << "." << endl;

    vector <int>::iterator iter3, iter4;

    // Constructing a vector of partial products
    int k = 0, ppartotal;
    for (iter3 = v3.begin(); iter3 != v3.end(); iter3++) {
        ppartotal = accumulate(v3.begin(), iter3 + 1, 1, multiplies<int>());
        v4[k] = ppartotal;
        k++;
    }

    cout << "The vector of partial products is:\n ( ";
    for (iter4 = v4.begin(); iter4 != v4.end(); iter4++)
        cout << *iter4 << " ";
    cout << ")." << endl;

   //output: 1 2 6 24 120 720 5040 40320 362880 3628800

    return 0;
}
#endif

adjacent_difference

Syntax:

#include <numeric>
iterator adjacent_difference( iterator start, iterator end, iterator result );
iterator adjacent_difference( iterator start, iterator end, iterator result, BinaryFunction f );

The adjacent_difference() function calculates the differences between adjacent elements

**in the range [start,end) and stores the result starting at result.
If a binary function f is given, it is used instead of the - operator to compute the differences.**

#include <vector>
#include <list>
#include <numeric>
#include <functional>
#include <iostream>

#if 0
int main()
{
    using namespace std;

    vector<int> V1(10), V2(10);
    vector<int>::iterator VIter1, VIter2, VIterend, VIterend2;

    list <int> L1;
    list <int>::iterator LIter1, LIterend, LIterend2;

    int t;
    for (t = 1; t <= 10; t++)
    {
        L1.push_back(t * t);
    }

    cout << "The input list L1 is:\n ( ";
    for (LIter1 = L1.begin(); LIter1 != L1.end(); LIter1++)
        cout << *LIter1 << " ";
    cout << ")." << endl;

    // The first member function for the adjacent_differences of
    // elements in a list output to a vector
    VIterend = adjacent_difference(L1.begin(), L1.end(),
        V1.begin());

    cout << "Output vector containing adjacent_differences is:\n ( ";
    for (VIter1 = V1.begin(); VIter1 != VIterend; VIter1++)
        cout << *VIter1 << " ";
    cout << ")." << endl;

    //output: 1 3 5 7 9 11 13 15 17 19 

    // The second member function used to compute
    // the adjacent products of the elements in a list
    VIterend2 = adjacent_difference(L1.begin(), L1.end(), V2.begin(),multiplies<int>());

    cout << "The output vector with the adjacent products is:\n ( ";
    for (VIter2 = V2.begin(); VIter2 != VIterend2; VIter2++)
        cout << *VIter2 << " ";
    cout << ")." << endl;

    //output: 1 4 36 144 400 900 1764 3136 5184 8100 

    // Computation of adjacent_differences in place
    LIterend2 = adjacent_difference(L1.begin(), L1.end(), L1.begin());
    cout << "In place output adjacent_differences in list L1 is:\n ( ";
    for (LIter1 = L1.begin(); LIter1 != LIterend2; LIter1++)
        cout << *LIter1 << " ";
    cout << ")." << endl;
    //output: 1 3 5 7 9 11 13 15 17 19 
}
#endif

partial_sum

#include <numeric>
iterator partial_sum( iterator start, iterator end, iterator result );
iterator partial_sum( iterator start, iterator end, iterator result,BinOp p );

The partial_sum() function calculates the partial sum of a range defined by [start,end),storing the output at result.
- start is assigned to *result, the sum of *start and (start + 1) is assigned to (result + 1), etc.*

#include <vector>
#include <list>
#include <numeric>
#include <functional>
#include <iostream>

#if 0
int main()
{
    using namespace std;
    vector<int> V1(10), V2(10);
    vector<int>::iterator VIter1, VIter2, VIterend, VIterend2;

    list <int> L1;
    list <int>::iterator LIter1, LIterend;

    int t;
    for (t = 1; t <= 10; t++)
    {
        L1.push_back(t);
    }

    cout << "The input list L1 is:\n ( ";
    for (LIter1 = L1.begin(); LIter1 != L1.end(); LIter1++)
        cout << *LIter1 << " ";
    cout << ")." << endl;

    // The first member function for the partial sums of
    // elements in a list output to a vector
    VIterend = partial_sum(L1.begin(), L1.end(),
        V1.begin());

    cout << "The output vector containing the partial sums is:\n ( ";
    for (VIter1 = V1.begin(); VIter1 != VIterend; VIter1++)
        cout << *VIter1 << " ";
    cout << ")." << endl;

   //output: 1 3 6 10 15 21 28 36 45 55

    // The second member function used to compute
    // the partial product of the elements in a list
    VIterend2 = partial_sum(L1.begin(), L1.end(), V2.begin(),
        multiplies<int>());

    cout << "The output vector with the partial products is:\n ( ";
    for (VIter2 = V2.begin(); VIter2 != VIterend2; VIter2++)
        cout << *VIter2 << " ";
    cout << ")." << endl;

   // output:1 2 6 24 120 720 5040 40320 362880 3628800 

    // Computation of partial sums in place
    LIterend = partial_sum(L1.begin(), L1.end(), L1.begin());
    cout << "The in place output partial_sum list L1 is:\n ( ";
    for (LIter1 = L1.begin(); LIter1 != LIterend; LIter1++)
        cout << *LIter1 << " ";
    cout << ")." << endl;

   //output:1 3 6 10 15 21 28 36 45 55 
}
#endif

count_if(count)

#include <algorithm>
size_t count_if( iterator start, iterator end, UnaryPred p );
size_t count( iterator start, iterator end, const TYPE& val );

The count_if() function returns the number of elements between start and end for which the predicate p returns true

The count() function returns the number of elements between start and end that match val

int nums[] = { 0, 1, 2, 3, 4, 5, 9, 3, 13 };
int start = 0;
int end = 9;
int target_value = 3;
int num_items = count_if( nums+start,nums+end,
bind2nd(equal_to<int>(), target_value) );
cout << "nums[] contains " << num_items << " items matching "<< target_value << endl;

output:nums[] contains 2 items matching 3
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值