我正在尝试用 2 个字符的单词制作字典,但不太成功这是我的代码:
#include <cstdlib>
#include <iostream>
#include <map>
using namespace std;
int main(int argc, char *argv[]){
map<char*,int> m;
//input 5 two-lengthed words
for (int i=0;i<5;i++){
char s[3];
cin>>s;
s[2] = '\0';
m[s]=1; //add a key
}
//checking if a word exists.
cout<<"Word you want to check whether it exists:"<<endl;
char chck[3];
cin>>chck;
chck[2]='\0';
//I heard this is how you check whether a key exists:
bool exists = m.find(chck)==m.end();
cout<<((exists)?"Yes!":"No.")<<endl;
system("pause"); //Yea, system, I know.
return 0;
}
每当我输入单词,然后当我想检查一个单词是否在字典中时,我总是打印“No.”?
我来自Java,所以我习惯了引用,而不是指针,所以这就是我可能错的地方。我想学习如何正确使用地图,请问我应该在这里做什么?
谢谢