Iteraattorit C++ STL:ssä

Edellytys: Iteraattorien esittely
Iteraattoreita käytetään osoittamaan muistiosoitteita STL astiat. Niitä käytetään ensisijaisesti numero-, merkkijonoissa jne. Ne vähentävät ohjelman monimutkaisuutta ja suoritusaikaa.

Iteraattorien toiminnot :-

1. aloita() :- Tätä toimintoa käytetään palauttamaan aloitusasento säiliöstä.

2. end() :- Tätä toimintoa käytetään palauttamaan jälkeen loppuasento säiliöstä.




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

Lähtö:

 The vector elements are : 1 2 3 4 5 

3. etukäteen() :- Tätä toimintoa käytetään lisää iteraattorin paikkaa argumenteissa mainittuun määritettyyn numeroon asti.




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

Lähtö:

 The position of iterator after advancing is : 4 

4. seuraava() :- Tämä toiminto palauttaa uuden iteraattorin että iteraattori osoittaisi sen jälkeen asemien edetessä perusteluissaan.

5. edellinen() :- Tämä toiminto palauttaa uuden iteraattorin että iteraattori osoittaisi paikkojen vähentämisen jälkeen perusteluissaan.




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

Lähtö:

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


6. insert()
:- Tätä toimintoa käytetään aseta elementit mihin tahansa kohtaan säiliössä. Se hyväksyy 2 argumenttia, säiliö ja iteraattori paikkaan, johon elementit on lisättävä .




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

Lähtö:

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

Iteraattorityypit:

  1. Input Iteraattorit
  2. Output Iteraattorit
  3. Eteenpäin Iteraattori
  4. Kaksisuuntaiset iteraattorit
  5. Random-access Iteraattorit