C++map container application example

Maintaining a collection supports the following operations:
"I x", insert a number x;
"Q x", ask whether the number x has appeared in the collection;
now N operations are to be performed, and the corresponding output is output for each query operation result.

The first line of the input format
contains the integer N, which represents the number of operations.
Next N lines, each line contains an operation instruction, the operation instruction is one of "I x" and "Q x".

Output format
For each query instruction "Q x", output a query result. If x has appeared in the set, output "Yes", otherwise output "No".
Each result occupies one line.

Data range
1≤N≤10^5
−10 9≤x≤10 9

Input example:
5
I 1
I 2
I 3
Q 2
Q 5

Output example:
Yes
No

code show as below:

#include <iostream>
#include <map>
using namespace std;
map<int,int> h;
int main()
{
    
    
    int cnt;
    cin>>cnt;
    while(cnt--)
    {
    
    
        string a;
        int b;
        cin>>a>>b;
        if (a[0]=='I') h[b] = b;
        else if (a[0]=='Q')
        {
    
    
            if (h[b]==0) cout<<"No"<<endl;
            else cout<<"Yes"<<endl;
        }
    }
}

Guess you like

Origin blog.csdn.net/m0_51955470/article/details/114148210