uninitialized_fill_n
複製指定值的物件放入項目的指定數目的字元到一個未初始化的目的範圍。
template<class FwdIt, class Size, class Type>
void uninitialized_fill_n(
ForwardIterator _First,
Size _Count,
const Type& _Val
);
參數
_First
處理順向的 Iterator 會啟始的目的範圍的第一個項目。_Count
要初始化的項目數目。_Val
要使用的值初始化目的範圍。
備註
這個演算法允許分開來建構物件的記憶體配置。
樣板函式有效地執行:
while ( 0 < count-- )
new ( ( void * )&*_First ++ )
iterator_traits<ForwardIterator>::value_type( _Val );
除非程式碼便會擲回例外狀況。 在這種情況下,終結所有建構的物件,並將重新擲回例外狀況。
uninitialized_fill_n 有兩個關聯的表單:
如需這些函式如何運作的詳細資訊,請參閱 檢查過的 Iterator。
範例
// memory_uninit_fill_n.cpp
// compile with: /EHsc /W3
#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 ( 60 );
Integer* Array = ( Integer* ) malloc( N * sizeof( int ) );
uninitialized_fill_n( Array, N, val ); // C4996
int i;
cout << "The uninitialized Array contains: ";
for ( i = 0 ; i < N; i++ )
cout << Array [ i ].get( ) << " ";
}
Output
The uninitialized Array contains: 60 60 60 60 60 60 60 60 60 60
需求
標題: <memory>
命名空間: std