logo

Iterátory v C++ STL

Předpoklad : Úvod do iterátorů
Iterátory se používají k ukazování na adresy paměti STL kontejnery. Používají se především v sekvencích čísel, znaků atd. Snižují složitost a dobu provádění programu.

Operace iterátorů :-



1. začít() :- Tato funkce se používá k vrácení počáteční pozice kontejneru.

2. konec() :- Tato funkce se používá k vrácení po koncová poloha kontejneru.








// 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>>kde = { 1, 2, 3, 4, 5};> > >// Declaring iterator to a vector> >vector<>int>>::iterator ptr;> > >// Displaying vector elements using begin() and end()> >cout <<>'The vector elements are : '>;> >for> (ptr = ar.begin(); ptr cout << *ptr << ' '; return 0; }>

>

>

Výstup:

 The vector elements are : 1 2 3 4 5>

3. záloha() :- Tato funkce se používá zvýšit pozici iterátoru do zadaného počtu uvedeného v jeho argumentech.




// C++ code to demonstrate the working of> // advance()> #include> #include // for iterators> #include // for vectors> using> namespace> std;> int> main()> {> >vector<>int>>kde = { 1, 2, 3, 4, 5};> > >// Declaring iterator to a vector> >vector<>int>>::iterator 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;> > }>

>

>

Výstup:

 The position of iterator after advancing is : 4>

4. další() :- Tato funkce vrátí nový iterátor za kterým by iterátor ukazoval posouvání pozic zmíněný ve svých argumentech.

5. předchozí() :- Tato funkce vrátí nový iterátor že by iterátor ukázal po dekrementaci pozic zmíněný ve svých argumentech.




// C++ code to demonstrate the working of> // next() and prev()> #include> #include // for iterators> #include // for vectors> using> namespace> std;> int> main()> {> >vector<>int>>kde = { 1, 2, 3, 4, 5};> > >// Declaring iterators to a vector> >vector<>int>>::iterator ptr = ar.begin();> >vector<>int>>::iterator 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;> }>

>

java 8
>

Výstup:

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


6. vložit()
:- Tato funkce se používá vložte prvky do libovolné polohy v kontejneru. Přijímá to 2 argumenty, kontejner a iterátor na pozici, kam mají být prvky vloženy .




// C++ code to demonstrate the working of> // inserter()> #include> #include // for iterators> #include // for vectors> using> namespace> std;> int> main()> {> >vector<>int>>kde = { 1, 2, 3, 4, 5};> >vector<>int>>ar1 = {10, 20, 30};> > >// Declaring iterator to a vector> >vector<>int>>::iterator 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;> }>

>

>

Výstup:

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

Typy iterátorů:

  1. Vstupní iterátory
  2. Výstupní iterátory
  3. Dopředný iterátor
  4. Obousměrné iterátory
  5. Iterátory s náhodným přístupem