STL_算法_填充新值(fill、fill_n、generate、generate_n)

时间:2023-02-09 20:21:47

C++ Primer 学习中。。。

 

简单记录下我的学习过程 (代码为主)


所有容器适用

fill(b,e,v)             //[b,e)   填充成v
fill_n(b,n,v)           //[b,b+n) 填充成v
generate(b,e,p)         //[b,e)   按照p方法填充
generate_n(b,n,p)       //[b,b+n) 按照p方法填充


/**------http://blog.csdn.net/u010579068------**/
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<ctime>
#include<algorithm>
using namespace std;

/*****************************************
//所有容器适用
fill(b,e,v) //[b,e) 填充成v
fill_n(b,n,v) //[b,b+n) 填充成v
generate(b,e,p) //[b,e) 按照p方法填充
generate_n(b,n,p) //[b,b+n) 按照p方法填充

*****************************************/
/**----------------------------------------------------------------------------------

----------------------------------------------------------------------------------**/
/*************************************************************************************
std::fill 所有排序容器适用 algorithm
--------------------------------------------------------------------------------------
template < class ForwardIterator, class T >
void fill ( ForwardIterator first, ForwardIterator last, const T& value );

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

/*************************************************************************************
std::fill_n 所有排序容器适用 algorithm
--------------------------------------------------------------------------------------
template < class OutputIterator, class Size, class T >
void fill_n ( OutputIterator first, Size n, const T& value );

//eg:
template < class OutputIterator, class Size, class T >
void fill_n ( OutputIterator first, Size n, const T& value )
{
for (; n>0; --n) *first++ = value;
}
*************************************************************************************/

/*************************************************************************************
std::generate 所有排序容器适用 algorithm
--------------------------------------------------------------------------------------
template <class ForwardIterator, class Generator>
void generate ( ForwardIterator first, ForwardIterator last, Generator gen );

//eg:
template <class ForwardIterator, class Generator>
void generate ( ForwardIterator first, ForwardIterator last, Generator gen )
{
while (first != last) *first++ = gen();
}
*************************************************************************************/

/*************************************************************************************
std::generate_n 所有排序容器适用 algorithm
--------------------------------------------------------------------------------------
template <class OutputIterator, class Size, class Generator>
void generate_n ( OutputIterator first, Size n, Generator gen );

//eg:
template <class OutputIterator, class Size, class Generator>
void generate_n ( OutputIterator first, Size n, Generator gen )
{
for (; n>0; --n) *first++ = gen();
}
*************************************************************************************/

// function generator:
int RandomNumber ()
{
return (rand()%100);
}

// class generator:
struct c_unique
{
int current;
c_unique()
{
current=0;
}
int operator()()
{
return ++current;
}
} UniqueNumber;

int current(0);
int Unique_Number ()
{
return ++current;
}

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

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

cout << "myvector contains:";
for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it;

cout << endl;
/**---------------------------------------------------------------------------------**/
deque<int> mydeque (8,10); // mydeque: 10 10 10 10 10 10 10 10

fill_n (mydeque.begin(),4,20); // mydeque: 20 20 20 20 10 10 10 10
fill_n (mydeque.begin()+3,3,33); // mydeque: 20 20 20 33 33 33 10 10

cout << "mydeque contains:";
for (deque<int>::iterator it=mydeque.begin(); it!=mydeque.end(); ++it)
cout << " " << *it;

cout << endl;
/**---------------------------------------------------------------------------------**/

srand ( unsigned ( time(NULL) ) );

// vector<int> myvector (8);
vector<int>::iterator it;

generate (myvector.begin(), myvector.end(), RandomNumber);

cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it;

generate (myvector.begin(), myvector.end(), UniqueNumber);

cout << "\nmyvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it;

cout << endl;
/**---------------------------------------------------------------------------------**/

int myarray[9];

generate_n (myarray, 9, Unique_Number);

cout << "myarray contains:";
for (int i=0; i<9; ++i)
cout << " " << myarray[i];

cout << endl;

return 0;
}
/******
Output:
myvector contains: 5 5 5 8 8 8 0 0
mydeque contains: 20 20 20 33 33 33 10 10
myvector contains: 64 12 31 25 35 67 20 6
myvector contains: 1 2 3 4 5 6 7 8
myarray contains: 1 2 3 4 5 6 7 8 9


******/