有的有的,再细看一下代码还是可以发现的
fill:Assigns the same new value to every element in a specified range,The destination range must be valid; all pointers must be dereferenceable, and the last position is reachable from the first by incrementation. The complexity is linear with the size of the range.
uninitialized_fill :Copies objects of a specified value into an uninitialized destination range.This algorithm allows the decoupling of memory allocation from object construction.
MSDN上的例子很好了,我就采取“拿来主义”了
// memory_uninit_fill.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
using namespace std;
class Integer { // No default constructor
public:
Integer( int x ) : val( x ) {}
int get( ) { return val; }
private:
int val;
};
int main( )
{
const int N = 10;
Integer val ( 25 );
Integer* Array = ( Integer* ) malloc( N * sizeof( int ) );
uninitialized_fill( Array, Array + N, val );
int i;
cout << "The initialized Array contains: ";
for ( i = 0 ; i < N; i++ )
{
cout << Array [ i ].get( ) << " ";
}
cout << endl;
}
output:
The initialized Array contains: 25 25 25 25 25 25 25 25 25 25
/////////////////////////////////////////////////////////////////
// alg_fill.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <iostream>
int main( )
{
using namespace std;
vector <int> v1;
vector <int>::iterator Iter1;
int i;
for ( i = 0 ; i <= 9 ; i++ )
{
v1.push_back( 5 * i );
}
cout << "Vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
// Fill the last 5 positions with a value of 2
fill( v1.begin( ) + 5, v1.end( ), 2 );
cout << "Modified v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
}
output:
Vector v1 = ( 0 5 10 15 20 25 30 35 40 45 )
Modified v1 = ( 0 5 10 15 20 2 2 2 2 2 )