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

सारंगपुर का युद्ध कब हुआ था ? सारंगपुर का युद्ध किसके मध्य हुआ

कुम्भा की राजनैतिक उपलकियाँ कुंमा की प्रारंभिक विजयें  - महाराणा कुम्भा ने अपने शासनकाल के…

3 weeks ago

रसिक प्रिया किसकी रचना है ? rasik priya ke lekhak kaun hai ?

अध्याय- मेवाड़ का उत्कर्ष 'रसिक प्रिया' - यह कृति कुम्भा द्वारा रचित है तथा जगदेय…

3 weeks ago

मालकाना का युद्ध malkhana ka yudh kab hua tha in hindi

malkhana ka yudh kab hua tha in hindi मालकाना का युद्ध ? मालकाना के युद्ध…

2 months ago

कान्हड़देव तथा अलाउद्दीन खिलजी के संबंधों पर प्रकाश डालिए

राणा रतन सिंह चित्तौड़ ( 1302 ई. - 1303 ) राजस्थान के इतिहास में गुहिलवंशी…

2 months ago

हम्मीर देव चौहान का इतिहास क्या है ? hammir dev chauhan history in hindi explained

hammir dev chauhan history in hindi explained हम्मीर देव चौहान का इतिहास क्या है ?…

2 months ago

तराइन का प्रथम युद्ध कब और किसके बीच हुआ द्वितीय युद्ध Tarain battle in hindi first and second

Tarain battle in hindi first and second तराइन का प्रथम युद्ध कब और किसके बीच…

2 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