Share via


list::difference_type

list の要素の数を、反復子が指す要素の範囲に基づいて表すために使用できる符号付き整数型。

typedef typename Allocator::difference_type difference_type;

解説

difference_type は、コンテナーの反復子を減算またはインクリメントするときに返される型です。 通常、difference_type は、[_First, _Last) の範囲内で、反復子 _First と _Last の間にある要素の数を表すために使用され、_First が指す要素と、_Last が指す要素の 1 つ前までの範囲の要素を含みます。

difference_type は、入力反復子の要件を満たすすべての反復子 (set などの反転可能なコンテナーによってサポートされる双方向反復子のクラスを含む) に対して使用できますが、反復子間の減算は、vector クラスなどのランダム アクセス コンテナーによって提供される、ランダム アクセス反復子によってのみサポートされます。

使用例

// list_diff_type.cpp
// compile with: /EHsc
#include <iostream>
#include <list>
#include <algorithm>

int main( ) 
{
   using namespace std;

   list <int> c1;
   list <int>::iterator   c1_Iter, c2_Iter;

   c1.push_back( 30 );
   c1.push_back( 20 );
   c1.push_back( 30 );
   c1.push_back( 10 );
   c1.push_back( 30 );
   c1.push_back( 20 );

   c1_Iter = c1.begin( );
   c2_Iter = c1.end( );

    list <int>::difference_type df_typ1, df_typ2, df_typ3;

   df_typ1 = count( c1_Iter, c2_Iter, 10 );
   df_typ2 = count( c1_Iter, c2_Iter, 20 );
   df_typ3 = count( c1_Iter, c2_Iter, 30 );
   cout << "The number '10' is in c1 collection " << df_typ1 << " times.\n";
   cout << "The number '20' is in c1 collection " << df_typ2 << " times.\n";
   cout << "The number '30' is in c1 collection " << df_typ3 << " times.\n";
}
          

必要条件

ヘッダー: <list>

名前空間: std

参照

関連項目

list クラス

標準テンプレート ライブラリ