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

Question Tag Definition in english with examples upsc ssc ias state pcs exames important topic

Question Tag Definition • A question tag is a small question at the end of a…

2 weeks ago

Translation in english grammer in hindi examples Step of Translation (अनुवाद के चरण)

Translation 1. Step of Translation (अनुवाद के चरण) • मूल वाक्य का पता करना और उसकी…

2 weeks ago

Report Writing examples in english grammer How to Write Reports explain Exercise

Report Writing • How to Write Reports • Just as no definite rules can be laid down…

2 weeks ago

Letter writing ,types and their examples in english grammer upsc state pcs class 12 10th

Letter writing • Introduction • Letter writing is an intricate task as it demands meticulous attention, still…

2 weeks ago

विश्व के महाद्वीप की भौगोलिक विशेषताएँ continents of the world and their countries in hindi features

continents of the world and their countries in hindi features विश्व के महाद्वीप की भौगोलिक…

2 weeks ago

भारत के वन्य जीव राष्ट्रीय उद्यान list in hin hindi IAS UPSC

भारत के वन्य जीव भारत में जलवायु की दृष्टि से काफी विविधता पाई जाती है,…

2 weeks 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