JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now

हिंदी माध्यम नोट्स

Categories: c++ language in hindi

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 किया है |

Sbistudy

Recent Posts

सती रासो किसकी रचना है , sati raso ke rachnakar kaun hai in hindi , सती रासो के लेखक कौन है

सती रासो के लेखक कौन है सती रासो किसकी रचना है , sati raso ke…

13 hours ago

मारवाड़ रा परगना री विगत किसकी रचना है , marwar ra pargana ri vigat ke lekhak kaun the

marwar ra pargana ri vigat ke lekhak kaun the मारवाड़ रा परगना री विगत किसकी…

13 hours ago

राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए sources of rajasthan history in hindi

sources of rajasthan history in hindi राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए…

2 days ago

गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है ? gurjaratra pradesh in rajasthan in hindi

gurjaratra pradesh in rajasthan in hindi गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है…

2 days ago

Weston Standard Cell in hindi वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन

वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन Weston Standard Cell in…

3 months ago

polity notes pdf in hindi for upsc prelims and mains exam , SSC , RAS political science hindi medium handwritten

get all types and chapters polity notes pdf in hindi for upsc , SSC ,…

3 months ago
All Rights ReservedView Non-AMP Version
X

Headline

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings
JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now