Last active
October 25, 2019 15:12
-
-
Save AdelDaniel/0e431371d2759c8fcb37426c6b2323f5 to your computer and use it in GitHub Desktop.
STL/C++/introduction iterators C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <bits/stdc++.h> | |
| using namespace std ; | |
| // source | |
| // https://www.geeksforgeeks.org/introduction-iterators-c/ | |
| //iterators defination | |
| //1//An iterator is an object (like a pointer) | |
| //2//that points to an element | |
| //3//inside the container | |
| // benefits of iterators | |
| //1//move through the contents of the container. | |
| //2//imagine that like a pointer pointing to some location and we can access content at that particular location using them. | |
| vip //3// Iterators play a critical role in (connecting algorithm with containers ) | |
| //along with the manipulation of data stored inside the containers. | |
| //4// The most obvious form of iterator is a pointer. | |
| //note | |
| ///// all iterators do not have similar functionality as that of pointers | |
| //// Now each one of these iterators are not supported by all the containers in STL | |
| ////different containers support different iterators, | |
| ////like vectors support Random-access iterators, while lists support bidirectional iterators. | |
| /*----------------------------------------------------------------------------------------------------- | |
| Depending upon the functionality of iterators they can be classified into five categories, | |
| ->the outer one being the most powerful one | |
| -----------------------------------------------------------------------------------------------------*/ | |
| /*----------------------------------------------------------------------------------------------------- | |
| Types of iterators: Based upon the functionality of the iterators, | |
| they can be classified into 5 major categories: | |
| 1- Input Iterators: They are the weakest of all the iterators | |
| and have very limited functionality. | |
| They can only be used in a single-pass algorithms, | |
| // i.e., those algorithms which process the container sequentially | |
| //such that no element is accessed more than once. | |
| --->//https://www.geeksforgeeks.org/input-iterators-in-cpp/ | |
| 2- Output Iterators: Just like input iterators, | |
| they are also very limited in their functionality | |
| and can only be used in single-pass algorithm, | |
| but not for accessing elements, but for being assigned elements. | |
| --->// | |
| 3-Forward Iterator: They are higher in hierarachy than input and output iterators, | |
| and contain all the features present in these two iterators. | |
| But, as the name suggests, they also can only move in forward direction and that too one step at a time. | |
| 4-Bidirectional Iterators: They have all the features of forward iterators | |
| along with the fact that they overcome the drawback of forward iterators, | |
| ->as they can move in both the directions, // that is why their name is bidirectional. | |
| 5-Random-Access Iterators: They are the most powerful iterators. | |
| They are not limited to moving sequentially, as their name suggests, | |
| they can randomly access any element inside the container. | |
| -->They are the ones whose functionality is same as pointers. | |
| --------------------------------------------------------------------------------- | |
| The following diagram shows the difference in their functionality with respect to various operations that they can perform. | |
| -----------------------------------------------------------------------------------------------------*/ | |
| int main(){ | |
| ///// Benefits of Iterators///// https://www.geeksforgeeks.org/introduction-iterators-c/ | |
| //1- Convenience in programming | |
| //ex | |
| /*step1*/ vector<int> v = { 1, 2, 3 }; // Declaring a vector | |
| cout << "Without iterators = "; | |
| for (int j = 0; j < 3; ++j) cout << v[j] << " "; | |
| cout << "\nWith iterators = "; | |
| for (vector<int>::iterator i = v.begin(); i != v.end(); ++i) cout << *i << " "; | |
| v.push_back(4); // Adding one more element to vector | |
| cout << "\nWithout iterators = "; | |
| for (int j = 0; j < 4; ++j) cout << v[j] << " "; | |
| cout << "\nWith iterators = "; | |
| for (vector<int>::iterator i = v.begin(); i != v.end(); ++i) cout << *i << " "; | |
| /* | |
| Without iterators = 1 2 3 | |
| With iterators = 1 2 3 | |
| Without iterators = 1 2 3 4 | |
| With iterators = 1 2 3 4 | |
| */ | |
| //2- Code reusability: | |
| //3- Dynamic processing of container: | |
| //terators provide us the ability to dynamically add or remove elements from the container | |
| //as and when we want | |
| //with erase | |
| //ex | |
| for (auto i = v.begin(); i != v.end(); ++i) { | |
| if (i == v.begin() + 1) { | |
| i = v.erase(i); | |
| // i now points to the element after the | |
| // deleted element | |
| } | |
| } | |
| //4-// 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. | |
| //3. advance() :- This function is used to increment the iterator position | |
| //till the specified number mentioned in its arguments. | |
| // C++ code to demonstrate the working of | |
| // advance() | |
| vector<int> ar = { 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 << " "; | |
| /*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. | |
| // next() and prev() | |
| vector<int> ar2 = { 1, 2, 3, 4, 5 }; | |
| // Using next() to return new iterator | |
| // points to 4 | |
| vector<int>::iterator ptr = ar2.begin(); | |
| auto itnxt = next(ptr, 3); | |
| // Displaying iterator position | |
| cout << "The position of new iterator using next() is : "; | |
| cout << *itnxt << " "; //The position of new iterator using next() is : 4 | |
| cout << endl; | |
| // Using prev() to return new iterator | |
| // points to 3 | |
| vector<int>::iterator ftr = ar2.end(); | |
| auto itprv = prev(ftr, 3); | |
| // Displaying iterator position | |
| cout << "The position of new iterator using prev() is : "; | |
| cout << *itprv << " "; // Output: The position of new iterator using prev() is : 3 | |
| cout << endl; | |
| //6. inserter() :- This function is used to insert the elements at any position in the container. | |
| //It accepts 2 arguments, 1- the container | |
| //2- and iterator to position where the elements have to be inserted. | |
| vector<int> ar = { 1, 2, 3, 4, 5 }; | |
| vector<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; | |
| } | |
| /*Output: | |
| The new vector after inserting elements is : 1 2 3 10 20 30 4 5 | |
| */ | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment