题意:意思很好懂,给出一个字典,然后输入后面的单词,要求输出前面的单词,如果没有就输出‘eh'

题解:用一个map,key和映射都为string类型,读入后赋值,然后查找就行。


#include <stdio.h>
#include <string>
#include <iostream>
#include <map>
using namespace std;

string str;
map<string, string> m;

int n;

int main() {
	int i;
	m.clear();
	while (getline(cin, str)) {
		if (str == "")
			break;
		else {
			string temp1 = "", temp2 = "";	
			for (i = 0; i < str.size(); i++)
				if (str[i] != ' ')
					temp1 += str[i];	
				else
					break;
			for (int j = i + 1; j < str.size(); j++)
				temp2 += str[j];
			m[temp2] = temp1;
		}
	}
	while (getline(cin, str)) {
		if (m[str] != "")
			cout << m[str] << endl;
		else
			cout << "eh" << endl;
	}
	return 0;
}