vector-begin

版权声明:我是一只快乐的小妖精 https://blog.csdn.net/qwq1503/article/details/79952264
////////////////////////////////////////
//      2018/04/15 19:00:54
//      vector-begin

#include <iostream>
#include <vector>
#include <iterator>
#include <numeric>

using namespace std;

int main(){
    vector<int> v(5);

    // iota函数:STL序列依次递增函数
    iota(v.begin(), v.end(), 1);
    /*
    iota 源码
    void iota(_FwdIt _First, _FwdIt _Last, _Ty _Val)
    {   // compute increasing sequence into [_First, _Last)
    _DEBUG_RANGE(_First, _Last);
    _Iota(_Unchecked(_First), _Unchecked(_Last), _Val);
    }
    void _Iota(_FwdIt _First, _FwdIt _Last, _Ty _Val)
    {
    // compute increasing sequence into [_First, _Last)
    for (; _First != _Last; ++_First, ++_Val)
    *_First = _Val;
    }
    */

    vector<int>::iterator it = v.begin();
    while (it != v.end()){
        cout << *(it++) << " ";
    }
    cout << endl;

    // third element of the vector
    it = v.begin() + 2;
    cout << *it << endl;
    return 0;
}


/*
OUTPUT:
    1 2 3 4 5
    3
*/ 
阅读更多

扫码向博主提问

小小妖妖

非学,无以致疑;非问,无以广识
去开通我的Chat快问
想对作者说点什么?