uninitialized_copy

初期化されていない先の範囲に指定したソース範囲内からのコピーのオブジェクト。

template<class InputIterator, class ForwardIterator>
   ForwardIterator uninitialized_copy(
      InputIterator _First, 
      InputIterator _Last,
      ForwardIterator _Dest
   );

パラメーター

  • _First
    ソース範囲内の先頭の要素を示す入力反復子。

  • _Last
    ソース範囲内の最後の要素を示す入力反復子。

  • _Dest
    割り当て先範囲の最初の要素を指定する前方反復子。

戻り値

ソース範囲内に空と反復子のアドレスの _Firstでない場合は先の範囲を超える最初の位置を示す前方反復子。

解説

このアルゴリズムは、オブジェクトの構造からのメモリ割り当てを分離できます。

テンプレート関数は、実装します:

while ( _First!= _Last )
   new ( ( void * )&*_Dest ++)
      iterator_traits<InputIterator>::value_type ( *_First ++ );
return _First;

コードが例外をスローします。その場合は、すべての構築されたオブジェクトは破棄され、例外がスローされます。

uninitialized_copy 2 種類の関連フォームを含んでいます:

checked_uninitialized_copy

unchecked_uninitialized_copy

詳細については、これらの関数がどのようにするか、チェックを行う反復子を参照してください。

使用例

// memory_uninit_copy.cpp
// compile with: /EHsc /W3
#include <memory>
#include <iostream>

using namespace std;

   class Integer 
   {
   public:
      Integer( int x ) : val( x ) {}
      int get( ) { return val; }
   private:
      int val;
   };

int main( )
{
   int Array[] = { 10, 20, 30, 40 };
   const int N = sizeof( Array ) / sizeof( int );

   int i;
   cout << "The initialized Array contains " << N << " elements: ";
      for (i = 0 ; i < N; i++ )
      {
         cout << " " << Array [ i ];
      }
   cout << endl;

   Integer* ArrayPtr = ( Integer* ) malloc( N * sizeof( int ) );
   Integer* LArrayPtr = uninitialized_copy(
      Array, Array + N, ArrayPtr);  // C4996

   cout << "Address of position after the last element in the array is: " 
        << &Array[0] + N << endl;
   cout << "The iterator returned by uninitialized_copy addresses: " 
        << ( void* )LArrayPtr << endl;
   cout << "The address just beyond the last copied element is: " 
        << ( void* )( ArrayPtr + N ) << endl;

   if ( ( &Array[0] + N ) == ( void* )LArrayPtr )
      cout << "The return value is an iterator "
           << "pointing just beyond the original array." << endl;
   else
      cout << "The return value is an iterator "
           << "not pointing just beyond the original array." << endl;

   if ( ( void* )LArrayPtr == ( void* )( ArrayPtr + N ) )
      cout << "The return value is an iterator "
           << "pointing just beyond the copied array." << endl;
   else
      cout << "The return value is an iterator "
           << "not pointing just beyond the copied array." << endl;

   free ( ArrayPtr );

   cout << "Note that the exact addresses returned will vary\n"
        << "with the memory allocation in individual computers."
        << endl;
}

出力例

The initialized Array contains 4 elements: 10 20 30 40
Address of position after the last element in the array is: 0012FED8
The iterator returned by uninitialized_copy addresses: 00311B88
The address just beyond the last copied element is: 00311B88
The return value is an iterator not pointing just beyond the original array.
The return value is an iterator pointing just beyond the copied array.
Note that the exact addresses returned will vary
with  the memory allocation in individual computers.

必要条件

ヘッダー : <memory>

名前空間: std