1. 程式人生 > >c++ 容器填充指定長度(fill_n)

c++ 容器填充指定長度(fill_n)

 

#include <iostream>     // cout
#include <algorithm>    // fill_n
#include <vector>       // vector
using namespace std;
int main () {
    vector<int> myvector (8,10);        // myvector: 10 10 10 10 10 10 10 10
    
    fill_n (myvector.begin(),4,20);     //位置長度元素// myvector: 20 20 20 20 10 10 10 10
fill_n (myvector.begin()+3,3,33); // myvector: 20 20 20 33 33 33 10 10 cout << "myvector contains:"; for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) cout << ' ' << *it; cout << '\n'; return 0; }