STL_算法_04_算术和生成算法

◆ 常用的算术和生成算法:

1.1、求和( accumulate 是求和的意思)(对指定范围内的元素求和,然后结果再加上一个由val指定的初始值。)

T accumulate(iteratorBegin, iteratorEnd, T _initialValue);

T accumulate(iteratorBegin, iteratorEnd, T _initialValue, functor某种计算方式);

1.2、填充(将输入值赋给标志范围内的所有元素)

void fill(iteratorBegin, iteratorEnd, T _value);

1、

1.1、第6讲 PPT.38

◆ accumulate() :  对指定范围内的元素 执行某种计算之后的结果,然后该结果 再和 val指定的初始值 执行一次该计算。

ZC: 默认是执行加法操作

ZC: 有两种 参数格式,返回值是 计算操作的结果。

ZC: VC6 测试代码 - 1:

 1 #ifdef WIN32
 2 #pragma warning (disable: 4786)
 3 #endif
 4 
 5 #include <string>
 6 #include <vector>
 7 #include <set>
 8 
 9 #include <algorithm>    // 算法
10 #include <numeric>    // 算法
11 #include <functional>    // 算法
12 
13 using namespace std;
14 
15 void main()
16 {
17     vector<int> vecIntA;
18     vecIntA.push_back(1);
19     vecIntA.push_back(3);
20     vecIntA.push_back(5);
21     vecIntA.push_back(7);
22     vecIntA.push_back(9);
23 
24     int iSum = accumulate(vecIntA.begin(), vecIntA.end(), 100);        //iSum==125
25     printf("%d
", iSum);
26 }

ZC:控制台输出 - 1:

1 125
2 Press any key to continue

ZC: vs2010 测试代码 - 2(来自:https://msdn.microsoft.com/en-US/library/aawk6wsh(v=vs.80).aspx):

  1 // numeric_accum.cpp
  2 // compile with: /EHsc
  3 #include <vector>
  4 #include <numeric>
  5 #include <functional>
  6 #include <iostream>
  7 
  8 int MultipliesZ(const int& _iLeft, const int& _iRight) // ZC: 我的求乘积的functor
  9 {
 10     return (_iLeft * _iRight);
 11 }
 12 
 13 int PlusZ(const int& _iLeft, const int& _iRight) // ZC: 我的求和的functor
 14 {
 15     return (_iLeft + _iRight);
 16 }
 17 
 18 int Test01(const int& _iLeft, const int& _iRight)
 19 {
 20     return (_iLeft*_iLeft + _iRight*_iRight);
 21 }
 22 
 23 int Test02(const int& _iLeft)
 24 {
 25     return 0;
 26 }
 27 
 28 int main( )
 29 {
 30    using namespace std;
 31 
 32    vector <int> v1, v2(20);
 33    vector <int>::iterator iter1, iter2;
 34 
 35    int i;
 36    for (i = 1; i < 21; i++)
 37    {
 38       v1.push_back(i);
 39    }
 40 
 41    cout << "The original vector v1 is:
 ( " ;
 42    for (iter1 = v1.begin(); iter1 != v1.end(); iter1++)
 43       cout << *iter1 << " ";
 44    cout << ")." << endl;
 45 
 46    // The first member function for the accumulated sum
 47    int total;
 48    total = accumulate(v1.begin(), v1.end(), 0);
 49 
 50    cout << "The sum of the integers from 1 to 20 is: "
 51         << total << "." << endl;
 52 
 53    // Constructing a vector of partial sums
 54    int j = 0, partotal;
 55    for (iter1 = v1.begin(); iter1 != v1.end(); iter1++)
 56    {
 57       partotal = accumulate(v1.begin(), iter1 + 1, 0);
 58       v2[j] = partotal;
 59       j++;
 60    }
 61 
 62    cout << "The vector of partial sums is:
 ( " ;
 63    for (iter2 = v2.begin(); iter2 != v2.end(); iter2++)
 64       cout << *iter2 << " ";
 65    cout << ")." << endl << endl;
 66 
 67 // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
 68 // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
 69 
 70    // The second member function for the accumulated product
 71    vector <int> v3, v4(10);
 72    vector <int>::iterator iter3, iter4;
 73 
 74    int s;
 75    for (s = 1; s < 11; s++)
 76    {
 77       v3.push_back(s);
 78    }
 79 
 80    cout << "The original vector v3 is:
 ( " ;
 81    for (iter3 = v3.begin(); iter3 != v3.end(); iter3++)
 82       cout << *iter3 << " ";
 83    cout << ")." << endl;
 84 
 85    int ptotal;
 86    ptotal = accumulate(v3.begin(), v3.end(), 1, multiplies<int>());
 87 
 88    cout << "The product of the integers from 1 to 10 is: "
 89         << ptotal << "." << endl;
 90 
 91    // Constructing a vector of partial products
 92    int k = 0, ppartotal;
 93    for (iter3 = v3.begin(); iter3 != v3.end(); iter3++) {
 94       ppartotal = accumulate(v3.begin(), iter3 + 1, 1, multiplies<int>());
 95       v4[k] = ppartotal;
 96       k++;
 97    }
 98 
 99    cout << "The vector of partial products is:
 ( " ;
100    for (iter4 = v4.begin(); iter4 != v4.end(); iter4++)
101       cout << *iter4 << " ";
102    cout << ")." << endl;
103 
104 // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
105 // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
106 
107    printf("
");
108 
109    int iSumZ = accumulate(v1.begin(), v1.end(), 0, PlusZ);
110    int iProductZ = accumulate(v3.begin(), v3.end(), 1, MultipliesZ);   
111    printf("ZC: sum ==> %d
", iSumZ);
112    printf("ZC: product ==> %d
", iProductZ);
113 
114    printf("
");
115 
116 
117    vector<int> vecZ;
118    vector<int>::iterator itZ;
119    int z;
120    for (z = 1; z <= 3; z++)
121    {
122       vecZ.push_back(z);
123       //vecZ.push_back(1);
124    }
125    for (itZ = vecZ.begin(); itZ != vecZ.end(); itZ++)
126    {
127        printf("%d ", *itZ);
128    }
129    printf("
");
130    int iTest01 = accumulate(vecZ.begin(), vecZ.end(), 5, Test01);
131    //int iTest01 = accumulate(vecZ.begin(), vecZ.end(), 5, Test02);
132    printf("ZC: iTest01 ==> %d
", iTest01);
133 
134    system("pause");
135 
136    // plus,minus,multiplies,divides,negate ==> 加,减,乘,除,取反
137    // 参考网址:http://blog.csdn.net/liusoftware5/article/details/6389050
138 }

ZC:控制台输出 - 2:

The original vector v1 is:
 ( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ).
The sum of the integers from 1 to 20 is: 210.
The vector of partial sums is:
 ( 1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 210 ).

The original vector v3 is:
 ( 1 2 3 4 5 6 7 8 9 10 ).
The product of the integers from 1 to 10 is: 3628800.
The vector of partial products is:
 ( 1 2 6 24 120 720 5040 40320 362880 3628800 ).

ZC: sum ==> 210
ZC: product ==> 3628800

1 2 3
ZC: iTest01 ==> 462409
请按任意键继续. . .

ZC: 上面的 462409 是怎么计算出来的?貌似是这样算出来的:

    26 = 5*5 + 1*1
   680 = 26*26 + 2*2
462409 = 680*680 + 3*3

ZC: 那这个计算步骤,又是怎么得来的??我是这样得到的:

一开始想不出,为什么结果会是 462409,后来试着 传入错误参数的 functor 看看结果会怎么样,像下面这样:

int iTest01 = accumulate(vecZ.begin(), vecZ.end(), 5, Test02);

编译时 就报错了,信息如下:

Error 1 error C2197: 'int (__cdecl *)(const int &)' : too many arguments for call C:Program Files (x86)Microsoft Visual Studio 10.0VCinclude umeric 36

定位到 如下图所示 的地方:

于是知道了,原来是这样计算的。验证了一下,确实是OK的。

ZC: 其他算法的 functor的具体情况,应该也可以通过该方法来查看。

1.2、第6讲 PPT.38

◆ fill() :   将输入值赋给标志范围内的所有元素。

ZC: 只有一种 参数格式,返回值是 void 。

ZC: VC6 测试代码:

 1 #ifdef WIN32
 2 #pragma warning (disable: 4786)
 3 #endif
 4 
 5 #include <string>
 6 #include <vector>
 7 #include <set>
 8 
 9 #include <algorithm>    // 算法
10 #include <numeric>    // 算法
11 #include <functional>    // 算法
12 
13 using namespace std;
14 
15 void main()
16 {
17     vector<int> vecIntA;
18     vecIntA.push_back(1);
19     vecIntA.push_back(3);
20     vecIntA.push_back(5);
21     vecIntA.push_back(7);
22     vecIntA.push_back(9);
23 
24     fill(vecIntA.begin(), vecIntA.end(), 8);        //8, 8, 8, 8, 8
25     
26     int iIdx = 0;
27     vector<int>::iterator it = vecIntA.begin();
28     while (it != vecIntA.end())
29     {
30         printf("[%02d] ==> %d
", iIdx, *it);
31         it ++;
32         iIdx ++;
33     }
34 }

ZC:控制台输出:

1 [00] ==> 8
2 [01] ==> 8
3 [02] ==> 8
4 [03] ==> 8
5 [04] ==> 8
6 Press any key to continue

?.?、第6讲 PPT.?

◆ 

ZC: VC6 测试代码:

ZC:控制台输出:

X

原文地址:https://www.cnblogs.com/cppskill/p/5241704.html