Prerequisite : Introduction to Iterators
Iterators are used to point at the memory addresses of STL containers. They are primarily used in sequence of numbers, characters etc. They reduce the complexity and execution time of 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.
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.
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.
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.
The new vector after inserting elements is : 1 2 3 10 20 30 4 5
Types of Iterators :
This article is contributed by Manjeet Singh .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Random-access Iterators in C++
- Input Iterators in C++
- Forward Iterators in C++
- Output Iterators in C++
- Bidirectional Iterators in C++
- Introduction to Iterators in C++
- Difference between Iterators and Pointers in C/C++ with Examples
- How to iterate through a Vector without using Iterators in C++
- Different types of range-based for loop iterators in C++
- Const vs Regular iterators in C++ with examples
- std::sort() in C++ STL
- Kruskal's Minimum Spanning Tree using STL in C++
- Dijkstra’s shortest path algorithm using set in STL
- Dijkstra's Shortest Path Algorithm using priority_queue of STL
- Prim's algorithm using priority_queue in STL
- unordered_map in C++ STL
- fill() and fill_n() functions in C++ STL
- std::transform() in C++ STL (Perform an operation on all elements)
- Count number of unique Triangles using STL | Set 1 (Using set)
- Permutations of a given string using STL
Improved By : IkamaruEnauki