다음을 통해 공유


raw_storage_iterator 클래스

초기화되지 않은 메모리에 결과를 저장하는 알고리즘을 사용할 수 있도록 제공되는 어댑터 클래스입니다.

구문

template <class OutputIterator, class Type>
    class raw_storage_iterator

매개 변수

OutputIterator
저장되는 개체에 대한 출력 반복기를 지정합니다.

Type
스토리지를 할당할 개체 형식입니다.

설명

클래스는 생성되는 시퀀스에서 형식 Type 의 개체를 생성하는 출력 반복기를 설명합니다. ForwardIterator 클래스<raw_storage_iterator의 개체인 Type>은 개체를 생성할 때 지정하는 클래스ForwardIterator의 정방향 반복기 개체를 통해 스토리지에 액세스합니다. 클래스ForwardIterator의 첫 번째 개체에 대해 &*first은 생성된 시퀀스에서 다음 개체(형식Type)에 대해 구조화되지 않은 스토리지를 지정해야 합니다.

이 어댑터 클래스는 메모리 할당 및 개체 생성을 구분해야 하는 경우에 사용됩니다. raw_storage_iterator를 사용하여 malloc 함수를 통해 할당된 메모리와 같은 초기화되지 않은 스토리지에 개체를 복사할 수 있습니다.

멤버

생성자

이름 설명
raw_storage_iterator 지정된 기본 출력 반복기를 사용하여 원시 스토리지 반복기를 생성합니다.

Typedef

이름 설명
element_type 원시 스토리지 반복기를 저장할 요소를 설명하는 형식을 제공합니다.
iter_type 원시 스토리지 반복기의 기반이 되는 반복기를 설명하는 형식을 제공합니다.

연산자

이름 설명
operator* 출력 반복기 식 * ii = x를 구현하는 데 사용되는 역참조 연산자입니다.
operator= 메모리에 저장하기 위해 원시 스토리지 반복기 식 * i = x를 구현하는 데 사용되는 대입 연산자입니다.
operator++ 원시 스토리지 반복기에 대한 사전 증가 및 사후 증가 연산자입니다.

element_type

원시 스토리지 반복기를 저장할 요소를 설명하는 형식을 제공합니다.

typedef Type element_type;

설명

형식은 raw_storage_iterator 클래스 템플릿 매개 변수 Type의 동의어입니다.

iter_type

원시 스토리지 반복기의 기반이 되는 반복기를 설명하는 형식을 제공합니다.

typedef ForwardIterator iter_type;

설명

이 형식은 템플릿 매개 변수 ForwardIterator의 동의어입니다.

operator*

원시 스토리지 반복기 식 * ii = x를 구현하는 데 사용되는 역참조 연산자입니다.

raw_storage_iterator<ForwardIterator, Type>& operator*();

Return Value

원시 스토리지 반복기에 대한 참조

설명

에 대한 ForwardIterator 요구 사항은 원시 스토리지 반복기가 충족해야 하고, 식 * ii = 만 유효하지 않으며 자체 또는 operator= 자체에 대해 operator 아무 말도 하지 않는다는 것입니다. 이 구현의 멤버 연산자는 operator=(constType&&)가 * ptrval = 과 같은 식에서 실제 저장소를 수행할 수 있도록 반환*this합니다.

예시

// raw_storage_iterator_op_deref.cpp
// compile with: /EHsc
#include <iostream>
#include <iterator>
#include <memory>
#include <list>
using namespace std;

class Int
{
public:
   Int(int i)
   {
      cout << "Constructing " << i << endl;
      x = i;
      bIsConstructed = true;
   };

   Int &operator=(int i)
   {
      if (!bIsConstructed)
         cout << "Not constructed.\n";
      cout << "Copying " << i << endl;
      x = i;
      return *this;
   };

   int x;

private:
   bool bIsConstructed;
};

int main( void)
{
   Int *pInt = ( Int* ) malloc( sizeof( Int ) );
   memset( pInt, 0, sizeof( Int ) ); // Set bIsConstructed to false;
*pInt = 5;
   raw_storage_iterator< Int*, Int > it( pInt );
*it = 5;
}
Not constructed.
Copying 5
Constructing 5

operator=

메모리에 저장하기 위해 원시 스토리지 반복기 식 * i = x를 구현하는 데 사용되는 대입 연산자입니다.

raw_storage_iterator<ForwardIterator, Type>& operator=(
    const Type& val);

매개 변수

val
메모리에 삽입할 형식 Type 의 개체 값입니다.

Return Value

연산자는 val을 메모리에 삽입한 다음 원시 스토리지 반복기에 대한 참조를 반환합니다.

설명

원시 스토리지 반복기가 충족해야 하는 상태에 대한 ForwardIterator 요구 사항은 식 * ii = 만 유효하지 않으며 자체 또는 자체에 대해 operatoroperator= 아무 말도 하지 않아야 합니다. 이러한 멤버 연산자는 .를 반환합니다 *this.

대입 연산자는 배치 새 new ( (void*) & *first ) Type( val )식을 평가하여 저장된 반복기 값을 first사용하여 출력 시퀀스에서 다음 개체를 생성합니다.

예시

// raw_storage_iterator_op_assign.cpp
// compile with: /EHsc
#include <iostream>
#include <iterator>
#include <memory>
#include <list>
using namespace std;

class Int
{
public:
   Int( int i )
   {
      cout << "Constructing " << i << endl;
      x = i;
      bIsConstructed = true;
   };
   Int &operator=( int i )
   {
      if ( !bIsConstructed )
         cout << "Not constructed.\n";
      cout << "Copying " << i << endl; x = i;
      return *this;
   };
   int x;
private:
   bool bIsConstructed;
};

int main( void )
{
   Int *pInt = ( Int* )malloc( sizeof( Int ) );
   memset( pInt, 0, sizeof( Int ) ); // Set bIsConstructed to false;

*pInt = 5;

   raw_storage_iterator<Int*, Int> it( pInt );
*it = 5;
}
Not constructed.
Copying 5
Constructing 5

operator++

원시 스토리지 반복기에 대한 사전 증가 및 사후 증가 연산자입니다.

raw_storage_iterator<ForwardIterator, Type>& operator++();

raw_storage_iterator<ForwardIterator, Type> operator++(int);

Return Value

원시 스토리지 반복기 또는 원시 스토리지 반복기에 대한 참조입니다.

설명

첫 번째 연산자는 결국 연결된 입력 스트림에서 형식 CharType 의 개체를 추출하고 저장하려고 시도합니다. 두 번째 연산자는 개체의 복사본을 만들고 개체를 증가시킨 다음 복사본을 반환합니다.

첫 번째 사전 증가 연산자는 저장된 출력 반복기 개체를 증분한 다음 반환합니다 *this.

두 번째 사후 증가 연산자는 복사본을 *this만들고 저장된 출력 반복기 개체를 증분한 다음 복사본을 반환합니다.

생성자는 출력 반복기 개체로 저장 first 됩니다.

예시

// raw_storage_iterator_op_incr.cpp
// compile with: /EHsc
#include <iostream>
#include <iterator>
#include <memory>
#include <list>
using namespace std;

int main( void )
{
   int *pInt = new int[5];
   std::raw_storage_iterator<int*,int> it( pInt );
   for ( int i = 0; i < 5; i++, it++ ) {
      *it = 2 * i;
   };

   for ( int i = 0; i < 5; i++ ) cout << "array " << i << " = " << pInt[i] << endl;;

   delete[] pInt;
}
array 0 = 0
array 1 = 2
array 2 = 4
array 3 = 6
array 4 = 8

raw_storage_iterator

지정된 기본 출력 반복기를 사용하여 원시 스토리지 반복기를 생성합니다.

explicit raw_storage_iterator(ForwardIterator first);

매개 변수

first
생성 중인 raw_storage_iterator의 기준으로 사용할 정방향 반복기입니다.

예제

// raw_storage_iterator_ctor.cpp
// compile with: /EHsc /W3
#include <iostream>
#include <iterator>
#include <memory>
#include <list>
using namespace std;

class Int
{
public:
   Int(int i)
   {
      cout << "Constructing " << i << endl;
      x = i;
      bIsConstructed = true;
   };
   Int &operator=( int i )
   {
      if (!bIsConstructed)
         cout << "Error! I'm not constructed!\n";
      cout << "Copying " << i << endl;  x = i; return *this;
   };
   int x;
   bool bIsConstructed;
};

int main( void )
{
   std::list<int> l;
   l.push_back( 1 );
   l.push_back( 2 );
   l.push_back( 3 );
   l.push_back( 4 );

   Int *pInt = (Int*)malloc(sizeof(Int)*l.size( ));
   memset (pInt, 0, sizeof(Int)*l.size( ));
   // Hack: make sure bIsConstructed is false

   std::copy( l.begin( ), l.end( ), pInt );  // C4996
   for (unsigned int i = 0; i < l.size( ); i++)
      cout << "array " << i << " = " << pInt[i].x << endl;;

   memset (pInt, 0, sizeof(Int)*l.size( ));
   // hack: make sure bIsConstructed is false

   std::copy( l.begin( ), l.end( ),
      std::raw_storage_iterator<Int*,Int>(pInt));  // C4996
   for (unsigned int i = 0; i < l.size( ); i++ )
      cout << "array " << i << " = " << pInt[i].x << endl;

   free(pInt);
}
Error! I'm not constructed!
Copying 1
Error! I'm not constructed!
Copying 2
Error! I'm not constructed!
Copying 3
Error! I'm not constructed!
Copying 4
array 0 = 1
array 1 = 2
array 2 = 3
array 3 = 4
Constructing 1
Constructing 2
Constructing 3
Constructing 4
array 0 = 1
array 1 = 2
array 2 = 3
array 3 = 4