C++词典API
有谁知道C++的词典API,它允许我搜索一个词并获取定义?C++词典API
(我不介意,如果它是一个在线的API,我必须使用JSON或XML解析它)
编辑:对不起,我的意思是一本字典中的单词的定义。不是C++地图。抱歉混淆。
您可以使用aonaware API。 (http://services.aonaware.com/DictService/DictService.asmx)。虽然我不知道费用。
使用std::map<string,string>
那么你可以做:
#include <map>
map["apple"] = "A tasty fruit";
map["word"] = "A group of characters that makes sense";
然后
map<char,int>::iterator it;
cout << "apple => " << mymap.find("apple")->second << endl;
cout << "word => " << mymap.find("word")->second << endl;
打印定义
如果你知道你正在插入,你应该使用'insert'函数。当您不知道密钥是否存在时,应使用方括号进行访问,更新或插入,否则可能会产生大量开销。另外,如果密钥不存在,'mymap.find(“apple”) - > second'可能会非常危险。 – steveo225 2011-03-12 13:44:05
尝试使用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
使用'std :: make_pair'。应该添加“#include
我刚刚开始学习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;
}
编译并运行,你将得到:
bar
我相信OP想要一个已经充满英文单词定义的工具,而不是学习如何在C++中使用字典。 – Robin 2014-02-08 00:27:26
你在寻找的东西用文字的预先定义的列表,或者是你打算创建的列表单词/定义你自己? – 2011-03-12 13:33:09
预先定义的单词列表。 – 2011-03-12 15:05:20