Prerequisite : Introduction to Iterators
Iterators are used to point at the memory addresses of STL containers. They are primarily used in sequences of numbers, characters etc. They reduce the complexity and execution time of the program.
Operations of iterators :-
1. begin() :- This function is used to return the beginning position of the container.
2. end() :- This function is used to return the after end position of the container.
#include<iostream>
#include<iterator> // for iterators
#include<vector> // for vectors
using namespace std;
int main()
{
vector<int> ar = { 1, 2, 3, 4, 5 };
vector<int>::iterator ptr;
cout << "The vector elements are : ";
for (ptr = ar.begin(); ptr < ar.end(); ptr++)
cout << *ptr << " ";
return 0;
}
|
Output:
The vector elements are : 1 2 3 4 5
3. advance() :- This function is used to increment the iterator position till the specified number mentioned in its arguments.
#include<iostream>
#include<iterator> // for iterators
#include<vector> // for vectors
using namespace std;
int main()
{
vector<int> ar = { 1, 2, 3, 4, 5 };
vector<int>::iterator ptr = ar.begin();
advance(ptr, 3);
cout << "The position of iterator after advancing is : ";
cout << *ptr << " ";
return 0;
}
|
Output:
The position of iterator after advancing is : 4
4. next() :- This function returns the new iterator that the iterator would point after advancing the positions mentioned in its arguments.
5. prev() :- This function returns the new iterator that the iterator would point after decrementing the positions mentioned in its arguments.
#include<iostream>
#include<iterator> // for iterators
#include<vector> // for vectors
using namespace std;
int main()
{
vector<int> ar = { 1, 2, 3, 4, 5 };
vector<int>::iterator ptr = ar.begin();
vector<int>::iterator ftr = ar.end();
auto it = next(ptr, 3);
auto it1 = prev(ftr, 3);
cout << "The position of new iterator using next() is : ";
cout << *it << " ";
cout << endl;
cout << "The position of new iterator using prev() is : ";
cout << *it1 << " ";
cout << endl;
return 0;
}
|
Output:
The position of new iterator using next() is : 4
The position of new iterator using prev() is : 3
6. inserter() :- This function is used to insert the elements at any position in the container. It accepts 2 arguments, the container and iterator to position where the elements have to be inserted.
#include<iostream>
#include<iterator> // for iterators
#include<vector> // for vectors
using namespace std;
int main()
{
vector<int> ar = { 1, 2, 3, 4, 5 };
vector<int> ar1 = {10, 20, 30};
vector<int>::iterator ptr = ar.begin();
advance(ptr, 3);
copy(ar1.begin(), ar1.end(), inserter(ar,ptr));
cout << "The new vector after inserting elements is : ";
for (int &x; : ar)
cout << x << " ";
return 0;
}
|
Output:
The new vector after inserting elements is : 1 2 3 10 20 30 4 5
Types of Iterators :
- Input Iterators
- Output Iterators
- Forward Iterator
- Bidirectional Iterators
- Random-Access Iterators
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
10 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...