logo

C++ STL のイテレータ

前提条件: イテレータの概要
イテレータは、次のメモリ アドレスを指すために使用されます。 STL コンテナ。これらは主に数値や文字などのシーケンスで使用され、プログラムの複雑さと実行時間を軽減します。

イテレータの操作 :-



1.begin() :- この関数は、 開始位置 コンテナの。

2.end() :- この関数は、 終了位置 コンテナの。








// C++ code to demonstrate the working of> // iterator, begin() and end()> #include> #include // for iterators> #include // for vectors> using> namespace> std;> int> main()> {> >vector<>int>>= { 1, 2, 3, 4, 5 };>> > >// Declaring iterator to a vector> >vector<>int>>::イテレータ ptr;>> > >// Displaying vector elements using begin() and end()> >cout <<>'The vector elements are : '>;> >for> (ptr = ar.begin(); ptr cout << *ptr << ' '; return 0; }>

>

>

出力:

 The vector elements are : 1 2 3 4 5>

3.アドバンス() :- この関数は次の目的で使用されます。 イテレータの位置をインクリメントします 引数で指定された数まで。




// C++ code to demonstrate the working of> // advance()> #include> #include // for iterators> #include // for vectors> using> namespace> std;> int> main()> {> >vector<>int>>= { 1, 2, 3, 4, 5 };>> > >// Declaring iterator to a vector> >vector<>int>>::イテレータ ptr = ar.begin();>> > >// Using advance() to increment iterator position> >// points to 4> >advance(ptr, 3);> > >// Displaying iterator position> >cout <<>'The position of iterator after advancing is : '>;> >cout << *ptr <<>' '>;> > >return> 0;> > }>

>

>

出力:

 The position of iterator after advancing is : 4>

4.次へ() :- この機能 新しいイテレータを返します イテレータがその後を指すことになる ポジションを進める その議論の中で言及されています。

5. 前へ() :- この機能 新しいイテレータを返します イテレータが指すこと ポジションを減らした後 その議論の中で言及されています。




// C++ code to demonstrate the working of> // next() and prev()> #include> #include // for iterators> #include // for vectors> using> namespace> std;> int> main()> {> >vector<>int>>= { 1, 2, 3, 4, 5 };>> > >// Declaring iterators to a vector> >vector<>int>>::イテレータ ptr = ar.begin();>> >vector<>int>>::イテレータ ftr = ar.end();>> > > >// Using next() to return new iterator> >// points to 4> >auto> it = next(ptr, 3);> > >// Using prev() to return new iterator> >// points to 3> >auto> it1 = prev(ftr, 3);> > >// Displaying iterator position> >cout <<>'The position of new iterator using next() is : '>;> >cout << *it <<>' '>;> >cout << endl;> > >// Displaying iterator position> >cout <<>'The position of new iterator using prev() is : '>;> >cout << *it1 <<>' '>;> >cout << endl;> > >return> 0;> }>

>

100万いくつ 0
>

出力:

 The position of new iterator using next() is : 4 The position of new iterator using prev() is : 3>


6. インサータ()
:- この関数は次の目的で使用されます。 要素を任意の位置に挿入します コンテナの中。受け付けます 2 つの引数、要素を挿入する必要がある場所を指定するコンテナーとイテレーター




// C++ code to demonstrate the working of> // inserter()> #include> #include // for iterators> #include // for vectors> using> namespace> std;> int> main()> {> >vector<>int>>= { 1, 2, 3, 4, 5 };>> >vector<>int>>ar1 = {10, 20, 30};>> > >// Declaring iterator to a vector> >vector<>int>>::イテレータ ptr = ar.begin();>> > >// Using advance to set position> >advance(ptr, 3);> > >// copying 1 vector elements in other using inserter()> >// inserts ar1 after 3rd position in ar> >copy(ar1.begin(), ar1.end(), inserter(ar,ptr));> > >// Displaying new vector elements> >cout <<>'The new vector after inserting elements is : '>;> >for> (>int> &x : ar)> >cout << x <<>' '>;> > >return> 0;> }>

>

>

出力:

 The new vector after inserting elements is : 1 2 3 10 20 30 4 5>

イテレータの種類:

  1. 入力反復子
  2. 出力反復子
  3. 前方反復子
  4. 双方向反復子
  5. ランダムアクセス反復子