다음을 통해 공유


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;

코드에서 예외를 throw 하지 않으면.이런 경우 생성 된 모든 개체가 소멸 되 고 예외가 다시 throw 됩니다.

uninitialized_copy 관련 된 두 폼을 있습니다.

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