您的位置:首页 > 编程语言 > Go语言

算法之旅,直奔<algorithm>之十三 fill

2013-12-17 15:41 686 查看

fill(vs2010)

引言

这是我学习总结<algorithm>的第十三篇,fill是一个很好的初始化工具。大学挺好,好好珍惜。。。

作用

fill 的作用是 给容器里一个指定的范围初始为指定的数据。
In English, that is
Fill range with value
Assigns val to all the elements in the range
[first,last)
.

原型

template <class ForwardIterator, class T>
void fill (ForwardIterator first, ForwardIterator last, const T& val)
{
while (first != last) {
*first = val;
++first;
}
}


实验

数据初始化为



std::fill (myvector.begin(),myvector.begin()+4,5);



std::fill (myvector.begin()+3,myvector.end()-2,8);



代码

test.cpp

#include <iostream>     // std::cout
#include <algorithm>    // std::fill
#include <vector>       // std::vector

int main () {
std::vector<int> myvector (8);                       // myvector: 0 0 0 0 0 0 0 0

std::fill (myvector.begin(),myvector.begin()+4,5);   // myvector: 5 5 5 5 0 0 0 0
std::fill (myvector.begin()+3,myvector.end()-2,8);   // myvector: 5 5 5 8 8 8 0 0

std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: