I was trying to solve a problem, but I encountered a weird error. I don't know why my code is giving me wrong output. Can someone help me what is wrong in my code ? Thanks in advance.
Code
If I am giving this input :
1 2
abc
cdf
edf
then my code is giving me this output : len1 = 2 , len2 = 2 ( while len1 must be 1 )
Note : If I am removing that common string checking part — these two lines ( line1 and line2 ) from 2nd for loop, then it is giving me correct output as len1 = 1 , len2 = 2
Please help me.
Seems like
mp1[s]!=0
is creating new keys in the map.Do you want to say something about that note which i have specified here ?
Yeah as scopeInfinity said
mp1[s]!=0
adds another entry in the mp1 if s is not present already. Hence your len1 is 2 (For the given case).If you want to check if a string s is present map s or not then use
mp1.find(s)!=mp1.end()
Because map::find returns an iterator to the element if present otherwise its equal to mp1.end()Errichto , awoo , Ashishgup can you please help me with this issue ? Thanks in advance.
Did you have to ping us after getting help from others already? Downvote.