如何储存英语词典? 我写了一个C++程序,它在英文字典(ASC命令)中读,而不是进一步处理。
在第一步中,我决定将所有内容读取到2D数组中如何储存英语词典? 我写了一个C++程序,它在英文字典(ASC命令)中读,而不是进一步处理。,c++,arrays,dictionary,vector,C++,Arrays,Dictionary,Vector,在第一步中,我决定将所有内容读取到2D数组中 string dictionary[x][y]; 其中,x的大小仅为26,表示A-Z,y用于保存与x变量相关的单词 但是我无法预测y的大小,而且它是可变的,所以我不知道如何做到这一点 其次,我听说有一个叫做vector的容器。如何使用vector进行上述设计?例如,使用2D向量,并使用第一个维度携带第一个字母,第二个维度携带单词?您可以使用带有字符和字符串的多重映射 例如: #include <iostream> #include &l
string dictionary[x][y];
其中,x
的大小仅为26,表示A-Z,y
用于保存与x
变量相关的单词
但是我无法预测y的大小,而且它是可变的,所以我不知道如何做到这一点
其次,我听说有一个叫做
vector
的容器。如何使用vector
进行上述设计?例如,使用2D向量,并使用第一个维度携带第一个字母,第二个维度携带单词?您可以使用带有字符和字符串的多重映射
例如:
#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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
多地图词典;
无效打印字母(字符ch)
{
for(auto it=dictionary.equal_range(ch).first;it!=dictionary.equal_range(ch).second;++it)
{
cout second>temp;
字典插入(成对(临时[0],临时));
}
file.close();
//全部打印
for(自动i:字典)
{
cout你可以使用向量数组:std::vector dictionary[26]
。其背后的思想与你的第一个相同(除了使用std::vector::push_back()
method;)向行中添加单词之外)直接回答你的问题你可以这样做:
std::vector<string> dictionary[26];
std::向量字典[26];
dictionary[4]
现在是一个字符串的向量(类似于可变长度数组)
但是有更好的方法来存储已排序的词典。如果您从不添加单词,您可以将整个内容放入std::vector
中,然后使用std::sort(dictionary.begin(),dictionary.end())对其进行一次排序
。或者,如果您需要添加/删除单词并始终保留排序列表,您可以使用始终排序的std::set
(当您插入单词时,它会将其放在正确的位置)您可以保留字典
std::vector<std::pair< string,std::vector<string> > >
std::vector>
结构,使每个向量元素在向量中包含字符和单词列表。如果编译器支持某些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;
}
#包括
#包括
#包括
#包括
int main()
{
std::向量字典(26);
//“a”部分
字典[0]。推回(“外来”);
字典[0]。推回(“修改”);
字典[0]。推回(“苹果”);
//.......
//“z”部分
字典[25]。推回(“零”);
字典[25]。推回(“动物园”);
//对插入后的所有单词进行排序
for(自动和strs:字典){
std::sort(std::begin(strs),std::end(strs));
}
//查找“a”的特定单词
auto const it=std::equal_range(std::begin(dictionary[0]),std::end(dictionary[0]),“apple”);
如果(it.first!=it.second){
std::cout您应该使用Trie数据结构来存储字典。
.你可以很容易地为C练习++
#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;
}