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

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;
}


 

posted on 2013-12-17 15:41  我的小人生  阅读(217)  评论(0编辑  收藏  举报