• 引言
这是我学习总结<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;
  }
}


  • 实验
数据初始化为
算法之旅,直奔<algorithm>之十三 fill
std::fill (myvector.begin(),myvector.begin()+4,5);
算法之旅,直奔<algorithm>之十三 fill
std::fill (myvector.begin()+3,myvector.end()-2,8); 
算法之旅,直奔<algorithm>之十三 fill
  • 代码
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;
}


 

相关文章:

  • 2021-11-28
  • 2022-12-23
  • 2021-06-14
  • 2021-08-07
  • 2021-12-24
  • 2021-12-03
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-25
  • 2021-11-14
  • 2022-12-23
  • 2021-09-30
  • 2021-03-31
  • 2022-01-07
  • 2022-12-23
相关资源
相似解决方案