Iterators in c++ language in hindi , what is c++ iterator implementation examples source code

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 निन्म है :-
<ContainerType> :: iterator;
<ContainerType> :: const_iterator;
iterator मे निन्म operation perform हो सकता है :
Operator (*) : इस operation का use किसी element के current position को  return करने के लिए किया जाता है जो की iterator से point होता है |
#include<iostream>
#include<conio>
void main()
{
vector<int> v = { 1, 2, 3 };
for (i = v.begin(); i != v.end(); ++i)
{
cout << *i << ” “;
}
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 <iostream>
#include <vector>
using namespace std;
int main()
{
// Declaring a vector
vector<int> b = { 1, 2, 3 };
// Declaring an iterator
vector<int>::iterator i;
int j;
cout << “Without iterators = “;
// Accessing the elements without using iterators
for (j = 0; j < 3; ++j)
{
cout << b[j] << ” “;
}
cout << “\nWith iterators = “;
// Accessing the elements using iterators
for (i = b.begin(); i != b.end(); ++i)
{
cout << *i << ” “;
}
// Adding one more element to vector
b.push_back(4);
cout << “\nWithout iterators = “;
// Accessing the elements without using iterators
for (j = 0; j < 4; ++j)
{
cout << b[j] << ” “;
}
cout << “\nWith iterators = “;
// Accessing the elements using iterators
for (i = b.begin(); i != b.end(); ++i)
{
cout << *i << ” “;
}
return 0;
}
इस उदाहरन मे , किसी vector data type को define किया जाता है | और इस vector data type के element को एलेग एलेग mode मे access किया गया है |
पहले code मे elements को without using iterators के access किया गया है |
दुसरे block मे , elements को using iterators के access किया गया है |
तीसरे block मे vector मे एक और element को add किया गया है |
चोथे  code मे elements को without using iterators के access किया गया है |
पांचवा block मे , elements को using iterators के access किया गया है |उदाहरन 2
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// Declaring a vector
vector<int> b = { 1, 2, 3 };
// Declaring an iterator
vector<int>::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 << *i << ” “;
}
getch();
}
इस उदाहरन मे ,
सबसे पहले vector को declare किया गया है |
उसके बाद vector data type के beginning मे assignment operation से 7 को insert कराया |