如何存放一本英语字典
How to store an English dictionary?
我正在写一个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]
现在是strings
的vector
(类似于可变长度数组)
但是有更好的方法来存储排序字典。如果你从不添加单词,你可以把整个东西放入std::vector<std::string>
中,然后使用std::sort(dictionary.begin(), dictionary.end())
进行排序。或者如果你需要添加/删除单词并保持一个排序列表,你可以使用std::set<std::string>
,它总是排序的(当你插入一个单词时,它会把它放在适当的位置)
您可以将multimap
与char
和string
一起使用。
的例子:
#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> > >
相关文章:
- 从C++本机插件更新Vector3数组
- 使用strcpy将char数组的元素复制到另一个数组
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 有一个打印语句的函数是一种糟糕的编程实践吗
- GCC本机矩阵运算库
- armadillo c :如何使效率分配到一本
- 正在寻找一本关于windows C++GUI编程的书
- 如果不使用C++11,安东尼威廉的"C++ Concurrency in action"是一本合适的书吗?
- 对于多少种类型的 x86 处理器,Ngen 和 .net 本机一次创建本机代码
- 有没有一种方法可以让本机npm包依赖于特定版本的v8C++API
- 是否有一种简单的方法可以使用本机C++代码(VS2010)和Bamboo执行CI
- 有人能推荐一本关于可移植C/C++代码开发的书吗
- 一本优秀的C++书,适合中级程序员成为专业程序员
- 读一本c++ 11的书需要有c++98的知识吗?
- 请推荐一本有价值的关于多线程和boost的书.线程或c++ 11
- 如何存放一本英语字典
- C++:如何制作一本简单的词典
- 一本高效的字符串词典
- 请推荐一本反设计模式的电子书
- 什么最适合编写一次本机库,并通过语言绑定跨多种语言使用它