1. 程式人生 > >C++ STL(12):填充區間

C++ STL(12):填充區間

#include <iostream>
#include <algorithm>
#include <iterator>
#include <functional>
#include <vector>
 
int rnd()
{
     return rand() % 100;
}
 
//填充區間
int main()
{
     /************************************************************************/
     //fill
     /************************************************************************/
     //將range內的[first,last)內的每個元素賦上值value
     /*
     template<class ForwardIterator, class Type>
     void fill(
          ForwardIterator _First,
          ForwardIterator _Last,
          const Type& _Val
          );
     */
     const int N = 7;
     int a[N];
     std::fill(std::begin(a), std::end(a), 1);
     std::copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " "));    //1 1 1 1 1 1 1
     std::cout << std::endl;
 
     /************************************************************************/
     //fill_n
     /************************************************************************/
     //fill_n:將range[first,first+n)內的n個元素賦上value
     /*
     template<class OutputIterator, class Size, class Type>
     void fill_n(
          OutputIterator _First,
          Size _Count,
          const Type& _Val
          );
     */
     std::vector<int> iv;
     std::copy(std::begin(a), std::end(a), std::back_inserter(iv));
     //上面兩句等價於:std::vector<int> iv(std::begin(a), std::end(a));
     std::copy(iv.begin(), iv.end(), std::ostream_iterator<int>(std::cout, " "));
     //1 1 1 1 1 1 1
     std::cout << std::endl;
 
     std::fill_n(iv.begin(), 3, 2);
     std::copy(iv.begin(), iv.end(), std::ostream_iterator<int>(std::cout, " "));
     //2 2 2 1 1 1 1
     std::cout << std::endl;
 
     std::vector<int>::iterator it = iv.begin();
     std::advance(it, 2);
     std::fill_n(it, 4, 3);
     std::copy(iv.begin(), iv.end(), std::ostream_iterator<int>(std::cout, " "));
     //2 2 3 3 3 3 1
     std::cout << std::endl;
 
     std::fill_n(std::back_inserter(iv), 5, -1);
     std::copy(iv.begin(), iv.end(), std::ostream_iterator<int>(std::cout, " "));
     //2 2 3 3 3 3 1 -1 -1 -1 -1 -1
     std::cout << std::endl;

     /************************************************************************/
     //generate
     /************************************************************************/
     //generate:呼叫Gen生成器,將range[first,last)內的每個元素賦上值
     /*
     template<class ForwardIterator, class Generator>
     void generate(
          ForwardIterator _First,
          ForwardIterator _Last,
          Generator _Gen
          );
     */
     std::vector<int> iv2(N);
     std::generate(iv2.begin(), iv2.end(), rnd);
     std::copy(iv2.begin(), iv2.end(), std::ostream_iterator<int>(std::cout, " "));
     std::cout << std::endl;
 
     /************************************************************************/
     //generate_n
     /************************************************************************/
     //generate_n:呼叫Gen生成器,將range[first,first+n)內的每個元素賦上值
     /*
     template<class OutputIterator, class Diff, class Generator>
     void generate_n(
          OutputIterator First, 
          Diff Count, 
          Generator Gen
          );
     */
     std::generate_n(std::ostream_iterator<int>(std::cout, " "), 10, rnd);
     std::cout << std::endl;
     std::vector<int> iv3;
     std::generate_n(std::back_inserter(iv3), 10, rnd);
     std::copy(iv3.begin(), iv3.end(), std::ostream_iterator<int>(std::cout, " "));
     std::cout << std::endl;
     return 0;
}

====================打個廣告,歡迎關注====================