Iterators in c++ language in hindi

what is c++ iterator implementation examples source code , Iterators in c++ language in hindi :- इससे पहले के article मे advance c++ के topics को discuss किया था अब इस article मे c++ Iterators को discuss करेगे |
c++ Iterators
c++ Iterators केवल एक pointer है जिसका use किसी container elements को access करने के लिए किया जाता है |
Iterators का use किसी एक element से दुसरे element मे traverse करने के लिए किया जाता है | इस प्रोसेस को Iterating कहते है जिसे केवल एक container मे traversing की लिए किया जाता है |
iterator का मुख्य advantage सभी container type मे interface बनाने के लिए किया जाता है |
इसके अलावा , iterator का use किसी algorithm को बनाने के लिए किया जाट है जो की किसी container के type से free होता है |
iterator generic approach को प्रोविडे किया जाता है जिसका use किसी container के element को navigate करने के लिए किया जाता है |
इसका syntax निन्म है :-
:: iterator;
:: const_iterator;
iterator मे निन्म operation perform हो सकता है :
Operator (*) : इस operation का use किसी element के current position को  return करने के लिए किया जाता है जो की iterator से point होता है |
#include
#include
void main()
{
vector v = { 1, 2, 3 };
for (i = v.begin(); i != v.end(); ++i)
{
cout
}
getch();
}
इस code को execute किया जाता है तब vector data type की value को display किया जाता है |
Operator (++) : इस operation मे , iterator को एक से increament किया जाता है | अतः iterator किसी data type के next data को point करता है |
Operator (==) and Operator (!=) : इस operator का use ये check करने के लिए किया जाता है की दो iterator एक ही position को point करते है या नहीं |
Operator (=) : इस operation का use किसी iterator मे value को assign करने के लिए किया जाता है |
Difference b/w Iterators & Pointers
Iterators एक smart pointer होता है जिसका use किसी complex data structures को Iterate करने के लिए किया जाता है | container का खुद का Iterators होता है | अतः Iterators का अलग अलग container के लिए एक ही interface ह्योता है | अभी container के लिए member function होते है |
begin(): इस function का use container के first element को point out करने के लिए किया जाता है |
end(): इस function का use container के last element को point out करने के लिए किया जाता है |
उदाहरन  :
#include
#include
using namespace std;
int main()
{
// Declaring a vector
vector b = { 1, 2, 3 };
// Declaring an iterator
vector::iterator i;
int j;
cout
// Accessing the elements without using iterators
for (j = 0; j
{
cout
}
cout
// Accessing the elements using iterators
for (i = b.begin(); i != b.end(); ++i)
{
cout
}
// Adding one more element to vector
b.push_back(4);
cout
// Accessing the elements without using iterators
for (j = 0; j
{
cout
}
cout
// Accessing the elements using iterators
for (i = b.begin(); i != b.end(); ++i)
{
cout
}
return 0;
}
इस उदाहरन मे , किसी vector data type को define किया जाता है | और इस vector data type के element को एलेग एलेग mode मे access किया गया है |
पहले code मे elements को without using iterators के access किया गया है |

Also read : Signals in c++ language in hindi


दुसरे block मे , elements को using iterators के access किया गया है |
तीसरे block मे vector मे एक और element को add किया गया है |
चोथे  code मे elements को without using iterators के access किया गया है |
पांचवा block मे , elements को using iterators के access किया गया है |उदाहरन 2
#include
#include
using namespace std;
int main()
{
// Declaring a vector
vector b = { 1, 2, 3 };
// Declaring an iterator
vector::iterator i;
int j;
// Inserting element using iterators
for (i = b.begin(); i != b.end(); ++i) {
if (i == b.begin()) {
i = b.insert(i, 7);
// inserting 7 at the beginning of b
}
}
// b contains 7 1 2 3
// Deleting a element using iterators
for (i = b.begin(); i != b.end(); ++i) {
if (i == b.begin() + 1) {
i = b.erase(i);
// i now points to the element after the
// deleted element
}
}
// b contains 5 2 3
// Accessing the elements using iterators
for (i = b.begin(); i != b.end(); ++i) {
cout
}
getch();
}
इस उदाहरन मे ,
सबसे पहले vector को declare किया गया है |
उसके बाद vector data type के beginning मे assignment operation से 7 को insert कराया |

हम आशा करते है कि यह नोट्स आपकी स्टडी में उपयोगी साबित हुए होंगे | अगर आप लोगो को इससे रिलेटेड कोई भी किसी भी प्रकार का डॉउट हो तो कमेंट बॉक्स में कमेंट करके पूंछ सकते है |
आप इन्हे अपने Classmates & Friends के साथ शेयर कर सकते है |

Leave a Comment

Your email address will not be published. Required fields are marked *