List iterator for certain type list , forward और reverse order मे traverse | iterator example

इससे पहले के article मे , iterator का basic और type को discuss किया है अब इस article मे iterator के कुछ उदाहरनो को discuss करेगे जिस्सका मुख्य use किसी लिस्ट को implement करने के लिए किया जाता है |
Example
इस उदाहरन मे किसी iterator से list को generate किया जाता है |
Explanation
इस उदाहरन मे लिस्ट को implement किया जाता है लेकिन इस लिस्ट का कोई type नहीं है | इसके लिए निन्म प्रकार के प्रोग्राम को use किया जाता है :-
1.सबसे पहले list file को  header को include किया जाता है |
2.उसके बाद main() function मे , list type के variable list को declare किया जाता है |
3.इसके बाद loop चलाया जाता है जिसमे list के element को list मे assign किया जाता है | और size को find किया जाता है |
4.इसके बाद iterator ir को declare किया जाता है जिसे list लके begin element से initial किया जाता है |
5.इसका बाद while loop चलाया जाता है जिसमे इस लिस्ट के element को display किया जाता है |
6.इसके बाद लिस्ट के सभी element को 50 से multiply किया जाता है |
7.इसके बाद लिस्ट के element को फिर से print किया जाता है |Code :
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> l;
int i;
for(i=0; i<10; i++) l.push_back(i);
cout << “Size = ” << l.size() << endl;
list<int>::iterator ir = l.begin();
while(ir != l.end()) {
cout << *ir << endl;
ir++;
}
ir = l.begin();
while(ir != l.end()) {
*ir = *ir + 100;
ir++;
}
ir = l.begin();
while(ir != l.end()) {
cout << *ir << ” “;
ir++;
}
return 0;
}
इस उदहारण निन्म output होगा :
12 12 32 12 13 32
1200 1200 3200 1200 1300 3200

Example 2 : List iterator for certain type list
इस उदाहर्ण किसी विशेष type के लिस्ट को generate करने के लिए किया जाता है |
Explanation
इस उदाहरन मे लिस्ट को implement किया जाता है लेकिन इस लिस्ट का type integer है | इसके लिए निन्म प्रकार के प्रोग्राम को use किया जाता है :-
1.सबसे पहले list file को  header को include किया जाता है | इसके बाद एक constructor को define किया जाता है जिसमे list type के integer variable, integer_list को declare किया जाता है |
2.उसके बाद main() function मे , integer_list type के variable list1 को declare किया जाता है |
3.इसके बाद list1 के font position पर  2 को  assign किया जाता है |
4.इसके बाद list1 के font position पर  1 को  assign किया जाता है |
5.इसके बाद list1 के back position पर  3 को  assign किया जाता है |
6.इसके बाद लिस्ट को forward और backward traverse किया गया है :-
7.forward traverse के लिए loop को चलाया जाता है जिसमे iterator i को list1.begin() से initial करते है और इसके loop तब तक चलता है |

Code :
#include <iostream>
#include <list>
using namespace std;
typedef list<int> integer_list ;
int main(void)
{
integer_list lis1;
integer_list::iterator i;
list1.push_front (2);
list1.push_front (1);
list1.push_back (3);
for (i = list1.begin(); i != list1.end(); ++i)
cout << *i << ” “;
cout << endl;
for (i = list1.end(); i != list1.begin(); –i)
cout << *i << ” “;
cout << endl;
}

Example 3 : इस उदाहरन मे , किसी लिस्ट को forward और reverse order मे traverse करने के लिए concept को discuss किया गया है |

Explanation
1.सबसे पहले list file को  header को include किया जाता है |
2.उसके बाद main() function मे , list type के variable list को declare किया जाता है | इसमें लिस्ट मे 9 element को assign किया जाता है |
3.इसके बाद लिस्ट को forward और backward traverse किया गया है |
3.इसके बाद loop चलाया जाता है जिसमे list के element को forward डायरेक्शन मे traverse किया जाता है |
4.इसके बाद loop चलाया जाता है जिसमे list के element को backward डायरेक्शन मे traverse किया जाता है |
इसके लिए begin() और end () function को use किया जाता है | इसके अलावा rbegin() और rend() को भी use किया जाता है |

Code
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
void print (int element )
{
cout << element << ” “;
}
int main()
{
list<int> list;
// insert elements from 1 to 9
for (int i=1; i<=9; ++i) {
list.push_back(i);
}
// print all elements in normal order
for_each (list.begin(), list.end(),      // range
print);                        // operation
cout << endl;
// print all elements in reverse order
for_each (list.resversebegin(), list.resverseend(),    // range
print);                        // operations
cout << endl;
}

इसका आउटपुट निन्म होगा :-
1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1

इस article मे , iterator के उदाहरनो को discuss किया है |