【问题标题】:How to store an English dictionary?如何存储英语词典?
【发布时间】:2013-03-21 03:34:12
【问题描述】:

我正在编写一个 C++ 程序,它可以读取英文字典(按顺序),然​​后进行进一步处理。

在第一步中,我决定将所有内容读取到二维数组中。

string dictionary[x][y];

其中x 的大小仅为26,代表A-Z,y 是保存与x 变量相关的单词。

但我无法预测y 的大小,而且它是可变的,所以我不知道如何做到这一点。

其次,我听说了一个叫vector的容器。如何使用vector 进行上述设计?比如用一个二维向量,用第一个维度携带第一个字母,第二个维度携带单词?

【问题讨论】:

    标签: c++ arrays dictionary vector


    【解决方案1】:

    如果你的编译器支持一些 c++11 特性

    #include <algorithm>
    #include <iterator>
    #include <string>
    #include <vector>
    
    int main()
    {       
        std::vector<std::vector<std::string> > dictionary(26);
        //'a' part
        dictionary[0].push_back("alien");
        dictionary[0].push_back("amend");
        dictionary[0].push_back("apple");
    
        //.......
        //'z' part
        dictionary[25].push_back("zero");
        dictionary[25].push_back("zoo");
    
        //sort all of the words after insert
        for(auto &strs : dictionary){
            std::sort(std::begin(strs), std::end(strs));
        }
    
        //find the specific words of 'a'
        auto const it = std::equal_range(std::begin(dictionary[0]), std::end(dictionary[0]), "apple");
        if(it.first != it.second){
            std::cout<<*(it.first)<<std::endl;
        }else{
            std::cout<<"The word do not exist"<<std::endl;
        }           
    
        return 0;
    }
    

    如果没有,那么代码会变得有点乏味

    #include <algorithm>
    #include <string>
    #include <vector>
    
    int main()
    {       
        std::vector<std::vector<std::string> > dictionary(26);
        //'a' part
        dictionary[0].push_back("alien");
        dictionary[0].push_back("amend");
        dictionary[0].push_back("apple");
    
        //.......
        //'z' part
        dictionary[25].push_back("zero");
        dictionary[25].push_back("zoo");            
    
        //you could use std::for_each if you like, I choose for loop because I
        //don't like to write so many trivial functor
        typedef std::vector<std::vector<std::string> >::size_type size_type;
        size_type const size = dictionary.size();
        for(size_type i = 0; i != size; ++i){
           std::sort(dictionary[i].begin(), dictionary[i].end());
        }
    
        //find the specific words of 'a'
        typedef std::vector<std::string>::const_iterator StrIter;
        std::pair<StrIter, StrIter> it = std::equal_range(dictionary[0].begin(), dictionary[0].end(), "apple");
        if(it.first != it.second){
            std::cout<<*(it.first)<<std::endl;
        }else{
            std::cout<<"The word do not exist"<<std::endl;
        }    
    
        return 0;
    }
    

    【讨论】:

      【解决方案2】:

      要直接回答您的问题,您会这样做:

      std::vector<string> dictionary[26];
      

      dictionary[4] 现在是stringsvector(如可变长度数组)

      但是有更好的方法来存储排序字典。如果您从不添加单词,您可以将整个内容放入std::vector&lt;std::string&gt; 并使用std::sort(dictionary.begin(), dictionary.end()) 对其进行一次排序。或者,如果您需要添加/删除单词并始终保持排序列表,您可以使用始终排序的std::set&lt;std::string&gt;(当您插入单词时,它会将其放在正确的位置)

      【讨论】:

        【解决方案3】:

        您可以将multimapcharstring 一起使用。

        例子:

        #include <iostream>
        #include <map>
        #include <fstream>
        #include <string>
        
        using namespace std;
        
        multimap<char,string> dictionary;
        
        void printLetter(char ch)
        {
            for (auto it=dictionary.equal_range(ch).first; it!=dictionary.equal_range(ch).second; ++it)
            {
                cout << it->second << endl;
            }
        }
        
        int main()
        {
            fstream file;
            file.open("file.txt");
            //Read the data from the file
            while(!file.eof())
            {
                string temp;
                file >> temp;
                dictionary.insert(pair<char,string>(temp[0],temp));
            }
        
            file.close();
            //Print all
            for(auto i: dictionary)
            {
                cout << i.first << ":" << i.second << endl;
            }
            //Print words starting with specific letter
            printLetter('A');
        
            return 0;
        }
        

        【讨论】:

          【解决方案4】:

          您应该使用 Trie 数据结构来存储字典。 here is a C implementation of Trie。你可以轻松地为 C++ 工作

          【讨论】:

            【解决方案5】:

            您可以使用向量数组:std::vector&lt;string&gt; dictionary[26]。这背后的想法与您的第一个想法相同(除了使用std::vector::push_back() 方法将单词添加到行中;))

            【讨论】:

              【解决方案6】:

              你可以把字典放在

               std::vector<std::pair< string,std::vector<string> > > 
              

              结构,以便每个向量元素在向量中包含一个字符和单词列表。

              【讨论】:

                猜你喜欢
                • 2013-02-19
                • 1970-01-01
                • 2017-12-11
                • 2016-07-18
                • 2011-07-06
                • 1970-01-01
                • 2011-09-10
                • 1970-01-01
                • 2012-03-03
                相关资源
                最近更新 更多