mardi 23 mars 2021

i am not able to figure it out how the .end function is working in this cpp program

It is a map problem In which I am taking a string and an int input form the user and then simply printing out values corresponding to the keys and if the key entered by the user has no corresponding value or the key doesn't exist then I am printing out Not found Now the thing I am not able to understand is how if (name.find(s1)!=name.end() is working in the code given below the whole code is written by me except the if block which I have mentioned so help me out to understand the working of the if statement

#include<bits/stdc++.h>

using namespace std;

int main()
{
int n,x,query;
string s,s1;
map<string,int> name;
map<string,int> ::iterator itr;
cin >> n;
for (int i = 0; i < n; i++)
{
    cin >> s >> x;
    name[s]=x;
}
cin>>query;
for (int i = 0; i < query; i++)
{
    cin>>s1;
    if (name.find(s1)!=name.end())
    {
       cout<<s1<<"="<<name[s1]<<endl; 
    }
    else
    {
        cout<<"Not found"<<endl;
    }
    
}
return 0;
}

Aucun commentaire:

Enregistrer un commentaire