2011-03-12 53 views
4

有誰知道C++的詞典API,它允許我搜索一個詞並獲取定義?C++詞典API

(我不介意,如果它是一個在線的API,我必須使用JSON或XML解析它)

編輯:對不起,我的意思是一本字典中的單詞的定義。不是C++地圖。抱歉混淆。

+2

你在尋找的東西用文字的預先定義的列表,或者是你打算創建的列表單詞/定義你自己? – 2011-03-12 13:33:09

+0

預先定義的單詞列表。 – 2011-03-12 15:05:20

回答

2

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

23

使用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; 

打印定義

+7

如果你知道你正在插入,你應該使用'insert'函數。當您不知道密鑰是否存在時,應使用方括號進行訪問,更新或插入,否則可能會產生大量開銷。另外,如果密鑰不存在,'mymap.find(「apple」) - > second'可能會非常危險。 – steveo225 2011-03-12 13:44:05

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 
+0

使用'std :: make_pair'。應該添加「#include 」。 – jipje44 2015-05-29 13:35:42

0

我剛剛開始學習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 
+1

我相信OP想要一個已經充滿英文單詞定義的工具,而不是學習如何在C++中使用字典。 – Robin 2014-02-08 00:27:26