C++;字典API 有人知道C++的字典API,允许我搜索一个单词并找回定义吗?< /p>
(我不介意它是否是一个在线API,我必须使用JSON或XML来解析它) 编辑:对不起,我指的是一本字典,就像单词的定义一样。不是C++的映射。很抱歉造成混淆。请使用 然后你可以做:C++;字典API 有人知道C++的字典API,允许我搜索一个单词并找回定义吗?< /p>,c++,api,C++,Api,(我不介意它是否是一个在线API,我必须使用JSON或XML来解析它) 编辑:对不起,我指的是一本字典,就像单词的定义一样。不是C++的映射。很抱歉造成混淆。请使用 然后你可以做: #include <map> map["apple"] = "A tasty fruit"; map["word"] = "A group of characters that makes sense"; #包括 地图[“苹果”]=“美味的水果”; map[“word”]=“一组有意义的字符”; 然
#include <map>
map["apple"] = "A tasty fruit";
map["word"] = "A group of characters that makes sense";
#包括
地图[“苹果”]=“美味的水果”;
map[“word”]=“一组有意义的字符”;
然后
map<char,int>::iterator it;
cout << "apple => " << mymap.find("apple")->second << endl;
cout << "word => " << mymap.find("word")->second << endl;
map::迭代器;
请尝试使用std::map
:
#include <map>
map<string, string> dictionary;
// adding
dictionary.insert(make_pair("foo", "bar"));
// searching
map<string, string>::iterator it = dictionary.find("foo");
if(it != dictionary.end())
cout << "Found! " << it->first << " is " << it->second << "\n";
// prints: Found! Foo is bar
#包括
地图词典;
//添加
词典.插入(配对(“foo”、“bar”);
//搜寻
迭代器it=dictionary.find(“foo”);
if(it!=dictionary.end())
您可以使用aonaware API。(http://services.aonaware.com/DictService/DictService.asmx). 我不知道费用。 < P>我刚开始学习C++。因为我有Python方面的经验,我正在寻找与Python中的dictionary
类似的数据结构。以下是我的发现:
#include <stream>
#include <map>
using namespace std;
int main() {
map<string,string> dict;
dict["foo"] = "bar";
cout<<dict["foo"]<<"\n";
return 0;
}
您是在寻找具有预定义单词列表的内容,还是打算自己创建单词/定义列表?如果您知道要插入,则应使用insert
功能。当您不知道密钥是否存在时,应该使用括号来访问、更新或插入,否则可能会产生大量开销。此外,如果代码不存在,查找(“Apple”)->第二版/代码>会非常危险。我相信OP想要一个已经被英语单词定义的工具,而不是学习如何在C++中使用字典。应添加“#包括”。
bar