Iteratori C++ STL

Priekšnosacījums: Ievads Iteratoros
Iteratori tiek izmantoti, lai norādītu uz atmiņas adresēm STL konteineri. Tos galvenokārt izmanto ciparu, rakstzīmju uc secībās. Tie samazina programmas sarežģītību un izpildes laiku.

Iteratoru darbības :-

1. sākums () :- Šī funkcija tiek izmantota, lai atgrieztu sākuma pozīcija no konteinera.

2. beigas () :- Šī funkcija tiek izmantota, lai atgrieztu pēc gala pozīcija no konteinera.




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

Izvade:

 The vector elements are : 1 2 3 4 5 

3. avanss() :- Šī funkcija tiek izmantota palielināt iteratora pozīciju līdz norādītajam skaitam, kas minēts tā argumentos.




// C++ code to demonstrate the working of> // advance()> #include> #include // for iterators> #include // for vectors> using> namespace> std;> int> main()> {> > vector <> int> >ar = { 1, 2, 3, 4, 5 };> > > // Declaring iterator to a vector> > vector <> int> >::iterators ptr = ar.begin();>> ;> > cout < < *ptr < <> ' '> ;> > > return> 0;> > }>

Izvade:

 The position of iterator after advancing is : 4 

4. nākamais() :- Šī funkcija atgriež jauno iteratoru ka iterators norādītu pēc tam virzot pozīcijas minēts savos argumentos.

5. iepriekšējais() :- Šī funkcija atgriež jauno iteratoru ka iterators norādītu pēc pozīciju samazināšanas minēts savos argumentos.




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

Izvade:

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


6. ievietot()
:- Šī funkcija tiek izmantota ievietojiet elementus jebkurā pozīcijā konteinerā. Tā pieņem 2 argumenti, konteiners un iterators, lai novietotu elementus, kur jāievieto elementi .




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

Izvade:

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

Iteratoru veidi:

  1. Ievades iteratori
  2. Izvades iteratori
  3. Pārsūtīt iteratoru
  4. Divvirzienu iteratori
  5. Brīvpiekļuves iteratori