The difference between making iterator using :: and using :

When making a map like map<int,list>mymap.Now to traverse the map i make an iterator suint auto it=mympap.begin().There is one other way which is used in the videos auto it:mymap.How does this works?

Hello @S19LPPP0039,

The functionality of both is same.
It’s just the way they have be declared.
Let’s understand both the statements one by one:

map<int, int>::iterator itr;
iterator is a datamember of the STL(templated class) map
As we know, if we have to access a public datamember/member function of a class outside we use scope resolution operator(::).

for (itr =mymap .begin(); itr != mymap.end(); ++itr) {
cout << ‘\t’ << itr->first
<< ‘\t’ << itr->second << ‘\n’;
}
It is a pointer that points to each element of the map. Thus, we then iterate it using above logic.

for(auto itr:mymap){
cout << ‘\t’ << itr->first
<< ‘\t’ << itr->second << ‘\n’;
}
Now, suppose you don’t remeber the syntax then you can use the above statement for declaration and iteration.
The auto keyword specifies that the type of the variable that is being declared will be automatically deducted from its initializer. In case of functions, if their return type is auto then that will be evaluated by return type expression at runtime.
In simple words, to automatically detecting the datatype at runtime.

You can use the above syntax for arrays also.
In it would automatically defined it’s type as int at runtime on basis of the type of array.

Hope, this would help.
Give a like, if you are satisfied.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.