首页 > 技术文章 > STL之map

andrew3 2018-04-05 20:23 原文

描述

使用STL中的map,查找字符串出现的次数。

部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码。

 

int main()
{
    int m;
    map<string, int> sm;
    Input(sm);
    cin>>m;
    while(m--)
    {
        string s;
        cin>>s;
        cout<<sm[s]<<endl;
    }
    return 0;
}

输入

输入数据第一行为正整数n,接下来包含n行,每行一个字符串,表示字符串列表。

接下来有m行,每行一个字符串,表示要查找的字符串。

字符串均不含空格。

输出

查找每个字符串在字符串列表中出现的次数。

样例输入

 5
abc
def
abc
def
abc
3
abc
def
hello

样例输出

 3
2
0

#include <iostream>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <functional>
#include <deque>
using namespace std;
void Input(map<string , int > &sm)
{
    int n;
    cin>>n;
    while(n--)
    {
        string s;
        cin>>s; 
        sm[s]++;
    }
}
int main()
{
    int m;
    map<string, int> sm;
    Input(sm);
    cin>>m;
    while(m--)
    {
        string s;
        cin>>s;
        cout<<sm[s]<<endl;
    }
    return 0;
}

 

 

推荐阅读