iterator patters(繰り返し文字特性)
iterator_traits
語句
struct iterator_traits {
typedef typename Iterator::iterator_category iterator_category;
typedef typename Iterator::value_type value_type;
typedef typename Iterator::difference_type difference_type;
typedef difference_type distance_type;
typedef typename Iterator::pointer pointer;
typedef typename Iterator::reference reference;
};
アルゴリズムの一般化
アルゴリズムの一般化は,どの容器でもこのアルゴリズムを使用できるようにすることである.
このアルゴリズムは、コンテナの反復器を使用してターゲットコンテナを巡回します.通常、開始を指す重複文字と終了を指す重複文字を使用し、終了を指す重複文字はループに含まれませんが、コンテナ内の最後の要素の後を指します.
重要なことはC++規格が指定されており,STLが提供するアルゴリズムが提供するコンテナだけでなく,主アレイでも使用できることである.
特定のタイプのアルゴリズムを用いて一般化すると、
template< class IteratorType >
Sum( IteratorType _first, IteratorType _last )
{
???? sum = *_first++;
while( _first != _last )
sum += *_first++;
return sum;
}
が見られる.コンテナは汎用的な反復器としてポインタ(pointer)を使用するため、パラメータとして開始と終了を指す反復器を受信し、各要素を遍歴する際に和を求める.しかし要素の(type)が分からないので、プロトコルのタイプもわかりません.
template< class IteratorType >
typename IteratorType::value_type Sum( IteratorType _first, IteratorType _last )
{
typename IteratorType::value_type sum = *_first++;
while( _first != _last )
sum += *_first++;
return sum;
}
コンテナの反復器はvalue typeタイプを提供する.リピーターのvalue typeは、リピーターが指す要素のタイプです.ただし、コンテナの反復器はvalue typeを提供し、既存のポインタはオブジェクトではないため、value typeは提供されないため、配列には通用しません.配列内のポインタは、要素のタイプをどのように決定しますか?
テンプレート部分専門化(部分専門化)
template<class _iter="">
struct iterator_traits
{ // get traits from iterator _Iter
typedef typename _Iter::iterator_category iterator_category;
typedef typename _Iter::value_type value_type; // <--
typedef typename _Iter::difference_type difference_type;
typedef difference_type distance_type; // retained
typedef typename _Iter::pointer pointer;
typedef typename _Iter::reference reference;
};
template<class _ty="">
struct iterator_traits<_Ty *>
{ // get traits from pointer
typedef random_access_iterator_tag iterator_category;
typedef _Ty value_type; // <--
typedef ptrdiff_t difference_type;
typedef ptrdiff_t distance_type; // retained
typedef _Ty *pointer;
typedef _Ty& reference;
};
一部の特殊化を使用して、ポインタと重複文字のvalue typeに異なる値を指定します.iterator_traits
iterator attersは反復器特性に翻訳されますが、実際には反復器と配列内のポインタを区別するために作成されます.
ソース
Microsoft
Reference
この問題について(iterator patters(繰り返し文字特性)), 我々は、より多くの情報をここで見つけました https://velog.io/@songtofu/iteratortraits-반복자-특질テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol