C++字典API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5282779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:49:07  来源:igfitidea点击:

C++ dictionary API

c++api

提问by Tom Leese

Does anyone know of a dictionary API for C++ that allows me to search for a word and get back the definition?

有谁知道 C++ 的字典 API 允许我搜索一个词并取回定义?

(I don't mind if it is an online API and I have to use JSON or XML to parse it)

(我不介意它是一个在线 API,我必须使用 JSON 或 XML 来解析它)

Edit:Sorry, I meant a dictionary as in definitions for words. Not a C++ Map. Sorry for confusion.

编辑:对不起,我的意思是字典中的单词定义。不是 C++ 地图。抱歉造成混乱。

采纳答案by gbvb

You can use aonaware APIs. (http://services.aonaware.com/DictService/DictService.asmx). I dont know the cost though.

您可以使用 aonaware API。(http://services.aonaware.com/DictService/DictService.asmx)。虽然我不知道费用。

回答by The GiG

Use std::map<string,string>then you can do:

使用std::map<string,string>然后你可以这样做:

#include <map> 
map["apple"] = "A tasty fruit";
map["word"] = "A group of characters that makes sense";

and then

进而

map<char,int>::iterator it;
cout << "apple => " << mymap.find("apple")->second << endl;
cout << "word => " << mymap.find("word")->second << endl;

to print the definitions

打印定义

回答by steveo225

Try using the std::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

回答by bingwang619

I have just started to learn C++. Since I have experience in Python, and I'm looking for similar data structure as dictionaryin Python. Here is what I found:

我刚刚开始学习 C++。因为我有 Python 方面的经验,所以我正在寻找与dictionaryPython类似的数据结构。这是我发现的:

#include <stream>
#include <map>

using namespace std;

int main() { 

    map<string,string> dict;
    dict["foo"] = "bar";
    cout<<dict["foo"]<<"\n";

    return 0; 
}

compile and run, you will got:

编译运行,你会得到:

bar