需要一些有关 C++ 的帮助Trie 数据结构

发布于 2024-09-12 21:58:04 字数 682 浏览 0 评论 0原文

我正在尝试编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁把谁当真 2024-09-19 21:58:04

如果您使用此 trie此示例代码< /a> 表示您的声明中需要更多模板参数来告诉它如何拆分键,以便它可以执行 trie 索引,尤其是前缀搜索:

trie< std::string, int, string_trie_e_access_traits<>, pat_trie_tag, trie_prefix_search_node_update> dict;

另请注意在搜索函数中使用 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:

trie< std::string, int, string_trie_e_access_traits<>, pat_trie_tag, trie_prefix_search_node_update> dict;

Also note the use of prefix_range in the search function in the linked sample code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文