C++词典API

Tom*_*ese 4 c++ api

有没有人知道C++的字典API,它允许我搜索一个单词并取回定义?

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

编辑:对不起,我的意思是字词定义中的字典.不是C++地图.抱歉混淆.

The*_*GiG 23

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

#include <map> 
map["apple"] = "A tasty fruit";
map["word"] = "A group of characters that makes sense";
Run Code Online (Sandbox Code Playgroud)

然后

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

打印定义

  • 如果您知道要插入,则应使用`insert`函数.当您不知道密钥是否存在时,括号应该用于访问,更新或插入,否则您可能会产生很大的开销.另外,如果密钥不存在,`mymap.find("apple") - > second`会非常危险. (8认同)

ste*_*225 10

尝试使用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
Run Code Online (Sandbox Code Playgroud)