如何存放一本英语字典

How to store an English dictionary?

本文关键字:一本 英语 字典 何存放      更新时间:2023-10-16

我正在写一个c++程序,它在英语字典(asc)中读取。订单),然后进行进一步处理。

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

string dictionary[x][y];

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

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

其次,我听说有一个叫做vector的容器。我如何使用vector做上述设计?例如,使用2D矢量,并使用第一个维度来携带第一个字母,第二个维度来携带单词?

如果你的编译器支持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;
}

直接回答你的问题:

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

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

但是有更好的方法来存储排序字典。如果你从不添加单词,你可以把整个东西放入std::vector<std::string>中,然后使用std::sort(dictionary.begin(), dictionary.end())进行排序。或者如果你需要添加/删除单词并保持一个排序列表,你可以使用std::set<std::string>,它总是排序的(当你插入一个单词时,它会把它放在适当的位置)

您可以将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;
}

您应该使用Trie数据结构来存储字典。这是一个Trie的C实现。你可以很容易地算出c++

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

你可以把字典放在

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