JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now

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

Categories: c++ language in hindi

Iterator examples : Use iterator to change all elements in a list , loop through all elements in a list

Example 1 : इस उदाहरन मे , किसी लिस्ट के element को मूव करने के लिए किया है |
इसके लिए ++ operation को use किया गया है इसमें forward traversing कराया जाता है |
Explain
1.सबसे पहले l file और algorithm file को  header को include किया जाता है |
2.उसके बाद main() function मे , array type के variable a को declare किया जाता है | इसमें array मे 5 element को assign किया जाता है |
3.इसके बाद लिस्ट को define किया जाता है जिसमे begin और end मे कमश array का पहला element और last element से initial किया जाता है  |
3.इसके बाद search operation को find किया जाता है जिसमे ‘e’ को find किया जाता है |
4.इसके बाद ‘e’ की location को next मे assign करते है |
5.इसके बाद next को increament करते है | और इस location के element को print करा लेते है |Code :
#include <iostream>
#include <cassert>
#include <l>
#include <algorithm> // for find
using namespace std;
int main()
{
char a[5] = {“a”, “r”, “e”, “q”, “t”};
l<char> lfirst(&a[0], &a[5]);
// Search for the first occurrence of the letter e:
l<char>::iterator where = find(lfirst.begin(), lfirst.end(), “e”);
l<char>::iterator next = where;
++next;
cout << *next << endl;
return 0;
}Example : Use iterator to change all elements in a list.
Explain
इस उदाहरन मे लिस्ट को implement किया जाता है लेकिन इस लिस्ट का कोई type नहीं है | इसके लिए निन्म प्रकार के प्रोग्राम को use किया जाता है :-
1.सबसे पहले l file को  header को include किया जाता है |
2.उसके बाद main() function मे , l type के variable l को declare किया जाता है |
3.इसके बाद loop चलाया जाता है जिसमे l के element को l मे assign किया जाता है | और size को find किया जाता है |
4.इसके बाद iterator ir को declare किया जाता है जिसे l के  begin element से initial किया जाता है |
5.इसका बाद while loop चलाया जाता है जिसमे इस लिस्ट के element को display किया जाता है |
6.इसका बाद while loop फिर से  चलाया जाता है जिसमे इस लिस्ट के element modify किया जाता है |
7.इसके बाद लिस्ट के element को फिर से print किया जाता है |

#include <iostream>
#include <l>
using namespace std;
int main()
{
l<int> l; // create an empty l
int i;
for(i=0; i<10; i++) l.push_back(i);
cout << “Size = ” << l.size() << endl;
cout << “Contents: “;
l<int>::iterator p = l.begin();
while(p != l.end()) {
cout << *p << ” “;
p++;
}
cout << “\n\n”;
// change contents of l
p = l.begin();
while(p != l.end()) {
*p = *p + 100;
p++;
}
cout << “Contents modified: “;
p = l.begin();
while(p != l.end()) {
cout << *p << ” “;
p++;
}
return 0;
}
इसका आउटपुट होगा :
Size = 10
Contents: 0 1 2 3 4 5 6 7 8 9
Contents modified: 100 101 102 103 104 105 106 107 108 109

Example 3 : Use iterator to loop through all elements in a list
Explain :
#include <iostream>
#include <list>
using namespace std;
int main()
{
l<int> l; // create an empty l
int i;
for(i=0; i<10; i++) l.push_back(i);
cout << “Size = ” << l.size() << endl;
cout << “Contents: “;
l<int>::iterator p = l.begin();
while(p != l.end()) {
cout << *p << ” “;
p++;
}
cout << “\n\n”;
return 0;
}
इसका आउटपुट होगा :
Size = 10
Contents: 0 1 2 3 4 5 6 7 8 9

Example 4 : इस उदहारण में iterator के type को interchange किया जाता है |
इस उदाहरन मे iterator को reverse iterator मे interchange किया गया है | इसके लिए base value को लिस्ट के position मे change किया जाता है |
इसके बाद reverse iterator को iterator मे फिर से change किया जाता है |

Code :
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
l<int> list_first ;
// insert elements from 1 to 9
for (int i=1; i<=9; ++i) {
list_first .push_back(i);
}
// find positionition of element with value 5
l<int>::iterator position;
position = find (list_first .begin(), list_first .end(),    // range
5);                          // value
// print value of the element
cout << “position:   ” << *position << endl;

// convert iterator to reverse iterator
l<int>::reverse_iterator rpositionition(position);
// print value of the element to which the reverse iterator refers
cout << “rpositionition:  ” << *rpositionition << endl;

// convert reverse iterator back to normal iterator
l<int>::iterator rpositionition;
rpositionition = rpositionition.base();
// print value of the element to which the normal iterator refers
cout << “rpositionition: ” << *rpositionition << endl;
}

इसका आउटपुट होगा :
position:   5
rpositionition:  4
rrpositionition: 5

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