Condiție preliminară: Introducere în Iteratori
Iteratoarele sunt folosite pentru a indica adresele de memorie ale STL containere. Ele sunt utilizate în principal în secvențe de numere, caractere etc. Ele reduc complexitatea și timpul de execuție a programului.
Operații ale iteratoarelor :-
1. începe() :- Această funcție este folosită pentru a returna pozitia de inceput a recipientului.
2. sfârşitul () :- Această funcție este folosită pentru a returna după pozitia finala a recipientului.
// 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>>cu = { 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; }> |
>
>
Ieșire:
The vector elements are : 1 2 3 4 5>
3. avans() :- Această funcție este folosită pentru crește poziția iteratorului până la numărul specificat menționat în argumentele sale.
// C++ code to demonstrate the working of> // advance()> #include> #include // for iterators> #include // for vectors> using> namespace> std;> int> main()> {> >vector<>int>>cu = { 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;> > }> |
>
>
Ieșire:
The position of iterator after advancing is : 4>
4. următorul() :- Această funcție returnează noul iterator pe care iteratorul ar indica după avansarea poziţiilor menționate în argumentele sale.
5. anterior() :- Această funcție returnează noul iterator că iteratorul ar indica după decrementarea posturilor menționate în argumentele sale.
// C++ code to demonstrate the working of> // next() and prev()> #include> #include // for iterators> #include // for vectors> using> namespace> std;> int> main()> {> >vector<>int>>cu = { 1, 2, 3, 4, 5 };>>> // Declaring iterators to a vector> >vector<>int>>::iterator ptr = ar.begin();>>> 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;> }> |
>
dacă altceva în shell bash
>
Ieșire:
The position of new iterator using next() is : 4 The position of new iterator using prev() is : 3>
6. insert () :- Această funcție este folosită pentru introduceți elementele în orice poziție în recipient. Acceptă 2 argumente, containerul și iteratorul pentru a poziționa unde trebuie introduse elementele .
// C++ code to demonstrate the working of> // inserter()> #include> #include // for iterators> #include // for vectors> using> namespace> std;> int> main()> {> >vector<>int>>cu = { 1, 2, 3, 4, 5 };>>> 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;> }> |
>
>
Ieșire:
The new vector after inserting elements is : 1 2 3 10 20 30 4 5>
Tipuri de iteratoare:
- Iteratoare de intrare
- Iteratoare de ieșire
- Iterator înainte
- Iteratoare bidirecționale
- Iteratoare cu acces aleatoriu