天天看點

UVA10282 map容器使用

UVA10282 map容器使用

好像沒什麼好說的。。map容器會怎麼用就水過了。。

代碼如下:

#include <map>
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

map<string,string>mp;
const int maxn = 30;
char s[maxn];

void init(){
    int len = strlen(s);
    string s1  = "";
    string s2 = "";
    int i = 0;

    for (; i<len; i++) {
         if (s[i]==' ') break;
         s1 += s[i];
    }
    i++;
    for (; i<len; i++) s2 += s[i];
    mp[s2] = s1;
}

int main(){
    mp.clear();
    while (gets(s)) {
        if (s[0]=='\0') break;
        init();
    }
    while (gets(s)){
        if (mp.find(s)!=mp.end()) cout << mp[s] << endl;
        else cout << "eh" << endl;
    }
    return 0;
}
           
下一篇: STL - map