需要一些有关 C++ 的帮助Trie 数据结构
我正在尝试编写一个 C++ 函数来匹配字典中是否存在字符串。它可以是部分字符串或完整字符串。所以我将每一行读入特里树中,
trie< std::string, int > dict;
dict.insert(make_pair(line,i++));
// when i search for a string it always returns invalid.
if(dict.find("AA")!=dict.end())
cout<<valid<<endl;
else
cout<<invalid<<endl;
有人可以帮我解决这个问题吗?我添加了用于读取字典中单词的代码。
if(myfile.is_open())
{
int i=0;
string line;
cout<<dict.size()<<endl;
while(!myfile.eof())
{
getline(myfile,line);
dict.insert(make_pair(line,i++));
}
}
I am trying to write a C++ function that matches whether a string is present in a dictionary . It can be a partial string or a complete string. SO I read each and every line into a trie
trie< std::string, int > dict;
dict.insert(make_pair(line,i++));
// when i search for a string it always returns invalid.
if(dict.find("AA")!=dict.end())
cout<<valid<<endl;
else
cout<<invalid<<endl;
Can some one please help me with this. I added code for reading words in dictionary.
if(myfile.is_open())
{
int i=0;
string line;
cout<<dict.size()<<endl;
while(!myfile.eof())
{
getline(myfile,line);
dict.insert(make_pair(line,i++));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用此 trie,此示例代码< /a> 表示您的声明中需要更多模板参数来告诉它如何拆分键,以便它可以执行 trie 索引,尤其是前缀搜索:
另请注意在搜索函数中使用
prefix_range
在链接的示例代码中。If you're using this trie, this sample code indicates you need more template parameters in your declaration to tell it how to split up the keys so it can do the trie indexing and especially prefix searches:
Also note the use of
prefix_range
in the search function in the linked sample code.