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

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:

 #ifdef WIN32
#pragma warning (disable: 4786)
#endif #include <string>
#include <vector>
#include <set> #include <algorithm> // 算法
#include <numeric> // 算法
#include <functional> // 算法 using namespace std; void main()
{
vector<int> vecIntA;
vecIntA.push_back();
vecIntA.push_back();
vecIntA.push_back();
vecIntA.push_back();
vecIntA.push_back(); int iSum = accumulate(vecIntA.begin(), vecIntA.end(), ); //iSum==125
printf("%d\n", iSum);
}

ZC:控制台输出 - 1:

 125
Press any key to continue

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

 // numeric_accum.cpp
// compile with: /EHsc
#include <vector>
#include <numeric>
#include <functional>
#include <iostream> int MultipliesZ(const int& _iLeft, const int& _iRight) // ZC: 我的求乘积的functor
{
return (_iLeft * _iRight);
} int PlusZ(const int& _iLeft, const int& _iRight) // ZC: 我的求和的functor
{
return (_iLeft + _iRight);
} int Test01(const int& _iLeft, const int& _iRight)
{
return (_iLeft*_iLeft + _iRight*_iRight);
} int Test02(const int& _iLeft)
{
return ;
} int main( )
{
using namespace std; vector <int> v1, v2();
vector <int>::iterator iter1, iter2; int i;
for (i = ; i < ; i++)
{
v1.push_back(i);
} cout << "The original vector v1 is:\n ( " ;
for (iter1 = v1.begin(); iter1 != v1.end(); iter1++)
cout << *iter1 << " ";
cout << ")." << endl; // The first member function for the accumulated sum
int total;
total = accumulate(v1.begin(), v1.end(), ); cout << "The sum of the integers from 1 to 20 is: "
<< total << "." << endl; // Constructing a vector of partial sums
int j = , partotal;
for (iter1 = v1.begin(); iter1 != v1.end(); iter1++)
{
partotal = accumulate(v1.begin(), iter1 + , );
v2[j] = partotal;
j++;
} cout << "The vector of partial sums is:\n ( " ;
for (iter2 = v2.begin(); iter2 != v2.end(); iter2++)
cout << *iter2 << " ";
cout << ")." << endl << endl; // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // The second member function for the accumulated product
vector <int> v3, v4();
vector <int>::iterator iter3, iter4; int s;
for (s = ; s < ; s++)
{
v3.push_back(s);
} cout << "The original vector v3 is:\n ( " ;
for (iter3 = v3.begin(); iter3 != v3.end(); iter3++)
cout << *iter3 << " ";
cout << ")." << endl; int ptotal;
ptotal = accumulate(v3.begin(), v3.end(), , multiplies<int>()); cout << "The product of the integers from 1 to 10 is: "
<< ptotal << "." << endl; // Constructing a vector of partial products
int k = , ppartotal;
for (iter3 = v3.begin(); iter3 != v3.end(); iter3++) {
ppartotal = accumulate(v3.begin(), iter3 + , , 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; // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** printf("\n"); int iSumZ = accumulate(v1.begin(), v1.end(), , PlusZ);
int iProductZ = accumulate(v3.begin(), v3.end(), , MultipliesZ);
printf("ZC: sum ==> %d\n", iSumZ);
printf("ZC: product ==> %d\n", iProductZ); printf("\n"); vector<int> vecZ;
vector<int>::iterator itZ;
int z;
for (z = ; z <= ; z++)
{
vecZ.push_back(z);
//vecZ.push_back(1);
}
for (itZ = vecZ.begin(); itZ != vecZ.end(); itZ++)
{
printf("%d ", *itZ);
}
printf("\n");
int iTest01 = accumulate(vecZ.begin(), vecZ.end(), , Test01);
//int iTest01 = accumulate(vecZ.begin(), vecZ.end(), 5, Test02);
printf("ZC: iTest01 ==> %d\n", iTest01); system("pause"); // plus,minus,multiplies,divides,negate ==> 加,减,乘,除,取反
// 参考网址:http://blog.csdn.net/liusoftware5/article/details/6389050
}

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.0\VC\include\numeric 36

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

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

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

1.2、第6讲 PPT.38

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

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

ZC: VC6 测试代码:

 #ifdef WIN32
#pragma warning (disable: 4786)
#endif #include <string>
#include <vector>
#include <set> #include <algorithm> // 算法
#include <numeric> // 算法
#include <functional> // 算法 using namespace std; void main()
{
vector<int> vecIntA;
vecIntA.push_back();
vecIntA.push_back();
vecIntA.push_back();
vecIntA.push_back();
vecIntA.push_back(); fill(vecIntA.begin(), vecIntA.end(), ); //8, 8, 8, 8, 8 int iIdx = ;
vector<int>::iterator it = vecIntA.begin();
while (it != vecIntA.end())
{
printf("[%02d] ==> %d\n", iIdx, *it);
it ++;
iIdx ++;
}
}

ZC:控制台输出:

 [00] ==> 8
[01] ==> 8
[02] ==> 8
[03] ==> 8
[04] ==> 8
Press any key to continue

?.?、第6讲 PPT.?

ZC: VC6 测试代码:

ZC:控制台输出:

X

STL_算法_04_算术和生成算法的更多相关文章

  1. C++ STL 常用算术和生成算法

    C++ STL 常用算术和生成算法 accumulate() accumulate: 对指定范围内的元素求和,然后结果再加上一个由val指定的初始值. #include<numeric> ...

  2. [迷宫中的算法实践]迷宫生成算法——递归分割算法

    Recursive division method        Mazes can be created with recursive division, an algorithm which wo ...

  3. ID3算法 决策树的生成(2)

    # coding:utf-8 import matplotlib.pyplot as plt import numpy as np import pylab def createDataSet(): ...

  4. ID3算法 决策树的生成(1)

    # coding:utf-8 import matplotlib.pyplot as plt import numpy as np import pylab def createDataSet(): ...

  5. 一个UUID生成算法的C语言实现 --- WIN32版本 .

    一个UUID生成算法的C语言实现——WIN32版本   cheungmine 2007-9-16   根据定义,UUID(Universally Unique IDentifier,也称GUID)在时 ...

  6. 分布式全局不重复ID生成算法

    分布式全局不重复ID生成算法 算法全局id唯一id  在分布式系统中经常会使用到生成全局唯一不重复ID的情况.本篇博客介绍生成的一些方法. 常见的一些方式: 1.通过DB做全局自增操作 优点:简单.高 ...

  7. C++ 基于凸包的Delaunay三角网生成算法

    Delaunay三角网,写了用半天,调试BUG用了2天……醉了. 基本思路比较简单,但效率并不是很快. 1. 先生成一个凸包: 2. 只考虑凸包上的点,将凸包环切,生成一个三角网,暂时不考虑Delau ...

  8. C++ 凸包生成算法

    由于我的极差记忆力,我打算把这个破玩意先记下来.因为以后会有改动(Delaunay三角网生成算法),我不想把一个好的东西改坏了... 好吧-- 凸包生成算法,: 1.先在指定的宽(width)高(he ...

  9. RocketMQ msgId生成算法

    当我们用RocketMQ发送信息的时候通常都会返回如下信息: SendResult [sendStatus=SEND_OK, msgId=0A42333A0DC818B4AAC246C290FD000 ...

随机推荐

  1. Amazon SES SPF和DKIM设置教程

    SPF和DKIM设置是争对域名邮箱而言的(公共邮件也不会给你修改DNS的权限),主要作用就是防止邮箱伪造提升邮件信用度 首先到亚马逊添加域名并验证 添加后,给出了域名验证的方法,就是在dns记录里添加 ...

  2. CentOS6.5安装Twemproxy集群

    Twemproxy,也叫Nutcraker.是一个Twtter开源的一个Redis和Memcache代理服务器. Redis作为一个高效的缓存服务器,非常具有应用价值.但是当使用比较多的时候,就希望可 ...

  3. unity3d-绘制贴图

    准备贴图 在屏幕在绘制一张静态贴图,需要用到GUI.DrawTexture()方法, 该方法可以设定图片的显示位置.缩放比例和渲染混合等 /* Rect position:表示图片的绘制区域 * Te ...

  4. http协议基础(七)通用首部字段

    通用首部字段的意思,就是:请求和响应报文双方都会使用的首部 1.Cache-Control 通过指定它的指令,能操作缓存的工作机制 指令参数是可选的,多个指令通过“,”分隔 Cache-Control ...

  5. class A where T:new()是什么意思

    这是C#泛型类声明的语法 class A<T> 表示 A类接受某一种类型,泛型类型为T,需要运行时传入where表明了对类型变量T的约束关系.   where T:new()指明了创建T的 ...

  6. python 爬取文章

    这里我们利用强大的python爬虫来爬取一篇文章.仅仅做一个示范,更高级的用法还要大家自己实践. 好了,这里就不啰嗦了,找到一篇文章的url地址:http://www.duanwenxue.com/a ...

  7. MFC工具栏的创建、设计与使用实例

    本文通过实例说明MFC工具栏的创建.设计和使用方法,包括三个demo.       demo1:创建一个工具栏 C++代码 //摘抄自MSDN demo1 (创建一个工具栏) 1.Create a t ...

  8. 在windows上构建LLVM 7.0.1

    关于在windows上构建LLVM,网上有不少文章,但都是互相抄来的,写作时极不认真,不是少这个,就是少那个,没有一篇是可以完整照着做下来的,实在气人. 本文的安装和配置过程,我亲自操作过好几遍,不惜 ...

  9. java网络基础知识的简述

    TCP/UDP的介绍 TCP协议:面向连接的,字节流无差错地传输协议. UDP协议:一个不可靠的无连接的数据传输协议. 说明:TCP可以想象成电话通讯,双方在通话时必须建立连接,一方没听清,会要求对方 ...

  10. kafka-python的API简单介绍

    在上一篇文章中说明了kafka-python的API使用的理论概念,这篇文章来说明API的实际使用. 在官方文档详细列出了kafka-python的API接口https://kafka-python. ...