raw_storage_iterator クラス
アルゴリズムの結果を初期化されていないメモリに格納するために用意されたアダプター クラスです。
構文
template <class OutputIterator, class Type>
class raw_storage_iterator
パラメーター
OutputIterator
格納されるオブジェクトの出力反復子を指定します。
Type
ストレージが割り当てられるオブジェクトの型。
解説
このクラスは、生成するシーケンス内に Type
型のオブジェクトを構築する出力反復子を記述します。 raw_storage_iterator
<ForwardIterator、Type> クラスのオブジェクトは、オブジェクトを構築する際に指定した ForwardIterator
クラスの前方反復子オブジェクトを介してストレージにアクセスします。 クラス ForwardIterator
の最初のオブジェクトの場合、式 &*first は、生成されたシーケンス内の次のオブジェクト (型 Type
) の非構造化ストレージを指定する必要があります。
このアダプター クラスは、メモリ割り当てとオブジェクト構築を分離する必要がある場合に使用されます。 raw_storage_iterator
は、malloc
関数を使用して割り当てられたメモリなど、初期化されていないストレージにオブジェクトをコピーするために使用できます。
メンバー
コンストラクター
名前 | 説明 |
---|---|
raw_storage_iterator | 指定した基になる出力反復子を使用して、生のストレージの反復子を構築します。 |
Typedefs
名前 | 説明 |
---|---|
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*();
戻り値
生のストレージ反復子への参照
解説
a ForwardIterator
の要件は、生のストレージ反復子が満たす必要があり、式 * ii = t のみが有効で、自身に関してはoperator
operator=
何も言わないということです。 この実装のメンバー演算子は、戻り *this
値を返します。これにより 、operator=(constType> は* ptr = val
などの式で実際のストアを実行できます)。
例
// 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
型のオブジェクトの値。
戻り値
この演算子は、メモリに val
を挿入し、生のストレージ反復子への参照を返します。
解説
生のストレージ反復子が満たす必要があり、式 * ii = のみが有効で、またはそれ自体に関operator
して何も言わない状態のoperator=
要件。ForwardIterator
これらのメンバー演算子は *this
を返します。
代入演算子は、配置の new 式 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);
戻り値
生のストレージ反復子、または生のストレージ反復子への参照。
解説
最終的に最初の演算子は、関連付けられている入力ストリームから CharType
型のオブジェクトの抽出と格納を試行します。 2 つ目の演算子は、オブジェクトのコピーを作成して、オブジェクトをインクリメントしてから、そのコピーを返します。
最初のプリインクリメント演算子は、格納されている出力反復子オブジェクトをインクリメントし、次に返します *this
。
2 番目の *this
postincrement 演算子はコピーを作成し、格納されている出力反復子オブジェクトをインクリメントして、コピーを返します。
コンストラクターは 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
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示