Необхідна умова: Вступ до ітераторів
Ітератори використовуються для вказівки на адреси пам'яті STL контейнери. Вони в основному використовуються в послідовностях чисел, символів тощо. Вони зменшують складність і час виконання програми.
Операції ітераторів :-
1. початок() :- Ця функція використовується для повернення початкове положення контейнера.
2. кінець() :- Ця функція використовується для повернення після кінцеве положення контейнера.
// 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>>з = { 1, 2, 3, 4, 5 };> > >// Declaring iterator to a vector> >vector<>int>>::ітератор ptr;> > >// Displaying vector elements using begin() and end()> >cout <<>'The vector elements are : '>;> >for> (ptr = ar.begin(); ptr cout << *ptr << ' '; return 0; }> |
>
>
Вихід:
The vector elements are : 1 2 3 4 5>
3. advance() :- Ця функція використовується для збільшити позицію ітератора до вказаного числа, зазначеного в його аргументах.
// C++ code to demonstrate the working of> // advance()> #include> #include // for iterators> #include // for vectors> using> namespace> std;> int> main()> {> >vector<>int>>з = { 1, 2, 3, 4, 5 };> > >// Declaring iterator to a vector> >vector<>int>>::ітератор 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;> > }> |
>
>
Вихід:
The position of iterator after advancing is : 4>
4. наступний() :- Ця функція повертає новий ітератор після якого вказував би ітератор просування позицій згадується у своїх аргументах.
5. попередній() :- Ця функція повертає новий ітератор що вказує ітератор після зменшення позицій згадується у своїх аргументах.
// C++ code to demonstrate the working of> // next() and prev()> #include> #include // for iterators> #include // for vectors> using> namespace> std;> int> main()> {> >vector<>int>>з = { 1, 2, 3, 4, 5 };> > >// Declaring iterators to a vector> >vector<>int>>::ітератор ptr = ar.begin();> >vector<>int>>::ітератор 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
>
Вихід:
The position of new iterator using next() is : 4 The position of new iterator using prev() is : 3>
6. вставити() :- Ця функція використовується для вставте елементи в будь-якому місці в контейнері. Це приймає 2 аргументи, контейнер та ітератор для позиції, куди потрібно вставити елементи .
// C++ code to demonstrate the working of> // inserter()> #include> #include // for iterators> #include // for vectors> using> namespace> std;> int> main()> {> >vector<>int>>з = { 1, 2, 3, 4, 5 };> >vector<>int>>ar1 = {10, 20, 30};> > >// Declaring iterator to a vector> >vector<>int>>::ітератор 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;> }> |
>
>
Вихід:
The new vector after inserting elements is : 1 2 3 10 20 30 4 5>
Типи ітераторів:
- Ітератори введення
- Вихідні ітератори
- Ітератор вперед
- Двонаправлені ітератори
- Ітератори довільного доступу