常用STL算法5_算术和生成

9 篇文章 0 订阅
#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值