Can we use iterator more than one times in c++?

problem : https://www.codechef.com/LRNDSA03/problems/CVOTE
my soultion : https://www.codechef.com/viewsolution/33224449

I just want to know that - why same iterator can’t be used two times?
In my solution, when I use “iterator itr” again then it gives compile time error (while compile on ubuntu) but when I replace it with different name then it works fine. In my case I used “iterator i”.

and one more question -
Can we access element in map using its key?
e.g -
map<string, int>mp;
mp.insert({“abc”, 1});
How to print 1 using its key “abc”?

I am new in c++.

You can use mp.getValue(“abc”); to get respective value

1 Like

use mp[key]

1 Like

In general it can, assuming you haven’t invalidated it in some way.

Please post full, formatted code that produces the compiler error, plus the command-line you use to compile it and the resulting compiler error - otherwise, we’ll spend all day trying to guess what the issue is :slight_smile:

3 Likes

@ssjgz
Sorry! I had make mistake while declaring iterator, I rechecked my code and found that. Actually, it was spelling mistake. I will take care about it.
Sorry! :no_mouth:

2 Likes

No problem :slight_smile:

1 Like