#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <set>
#include <list>
#include <algorithm>
#include <functional>
#include <numeric>
#include <string>
#include <iterator> //输出流
using namespace std;
template <typename T>
void printElem(T &t)
{
T::iterator it;
for (it = t.begin(); it != t.end(); it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
void main101_accumulate()
{
vector<int> vecIntA;
vecIntA.push_back(1);
vecIntA.push_back(3);
vecIntA.push_back(5);
vecIntA.push_back(7);
vecIntA.push_back(9);
/*
_Ty accumulate(_InIt _First, _InIt _Last, _Ty _Val)
{
// return sum of _Val and all in [_First, _Last)
_DEBUG_RANGE(_First, _Last);
return (_Accumulate(_Unchecked(_First), _Unchecked(_Last), _Val));
}
*/
//int iSum = accumulate(vecIntA.begin(), vecIntA.end(), 0); //iSum==125
int iSum = accumulate(vecIntA.begin(), vecIntA.end(), 100); //iSum==125
cout<<"iSum:"<<iSum<<endl;
}
void main102_fill()
{
vector<int> vecIntA;
vecIntA.push_back(1);
vecIntA.push_back(3);
vecIntA.push_back(5);
vecIntA.push_back(7);
vecIntA.push_back(9);
/*
void fill(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)
{
// copy _Val through [_First, _Last)
_DEBUG_RANGE(_First, _Last);
_Fill(_Unchecked(_First), _Unchecked(_Last), _Val);
}
*/
fill(vecIntA.begin(), vecIntA.end(), 8); //8, 8, 8, 8, 8
printElem(vecIntA);
}
int main()
{
//main101_accumulate(); //对指定范围内的元素求和,然后结果再加上一个由val指定的初始值
main102_fill();//将输入值赋给标志范围内的所有元素
cout<<"\nhello"<<endl;
system("pause");
return 0;
}
常用STL算法5_算术和生成
最新推荐文章于 2023-09-25 10:21:41 发布