Requisit previ: Introducció als iteradors
Els iteradors s'utilitzen per apuntar a les adreces de memòria de STL contenidors. S'utilitzen principalment en seqüències de nombres, caràcters, etc. Redueixen la complexitat i el temps d'execució del programa.
Operacions d'iteradors :-
1. començar() :- Aquesta funció s'utilitza per retornar el posició inicial del contenidor.
2. final () :- Aquesta funció s'utilitza per retornar el després posició final del contenidor.
// 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>>amb = { 1, 2, 3, 4, 5 };>>> // Declaring iterator to a vector> >vector<>int>>::iterador ptr;>>> // Displaying vector elements using begin() and end()> >cout <<>'The vector elements are : '>;> >for> (ptr = ar.begin(); ptr cout << *ptr << ' '; return 0; }> |
>
>
Sortida:
The vector elements are : 1 2 3 4 5>
3. avançar() :- Aquesta funció s'utilitza per augmentar la posició de l'iterador fins al nombre especificat esmentat en els seus arguments.
// C++ code to demonstrate the working of> // advance()> #include> #include // for iterators> #include // for vectors> using> namespace> std;> int> main()> {> >vector<>int>>amb = { 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;> > }> |
>
>
Sortida:
The position of iterator after advancing is : 4>
4. següent() :- Aquesta funció retorna el nou iterador que l'iterador apuntaria després avançant les posicions esmentat en els seus arguments.
5. anterior() :- Aquesta funció retorna el nou iterador que apuntaria l'iterador després de disminuir les posicions esmentat en els seus arguments.
// C++ code to demonstrate the working of> // next() and prev()> #include> #include // for iterators> #include // for vectors> using> namespace> std;> int> main()> {> >vector<>int>>amb = { 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;> }> |
>
polimorfisme
>
Sortida:
The position of new iterator using next() is : 4 The position of new iterator using prev() is : 3>
6. inserir() :- Aquesta funció s'utilitza per inseriu els elements en qualsevol posició al contenidor. Accepta 2 arguments, el contenidor i l'iterador per posicionar-se on s'han d'inserir els elements .
// C++ code to demonstrate the working of> // inserter()> #include> #include // for iterators> #include // for vectors> using> namespace> std;> int> main()> {> >vector<>int>>amb = { 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;> }> |
>
>
Sortida:
The new vector after inserting elements is : 1 2 3 10 20 30 4 5>
Tipus d'iteradors:
- Iteradors d'entrada
- Iteradors de sortida
- Iterador cap endavant
- Iteradors bidireccionals
- Iteradors d'accés aleatori