21xrx.com
2024-09-19 09:04:26 Thursday
登录
文章检索 我的文章 写文章
C++查找带有特定后缀的字符串
2023-06-27 01:00:01 深夜i     --     --
C++ 查找 特定后缀 字符串

C++是一种最流行的编程语言之一,用于开发各种应用程序。如果您想在C++程序中查找带有特定后缀的字符串,下面提供了一些有用的信息和建议。

首先,让我们考虑使用C++的标准库函数std::find来查找特定后缀的字符串。下面是一个示例代码:


#include <iostream>

#include <algorithm>

#include <string>

int main()

{

  std::string str = "hello world.mp4";

  std::string suffix = ".mp4";

  if(str.size() >= suffix.size() && str.substr(str.size() - suffix.size()) == suffix)

  

    std::cout << "The string contains the suffix." << std::endl;

  

  else

  

    std::cout << "The string does not contain the suffix." << std::endl;

  

  return 0;

}

在这个示例中,我们首先定义了一个字符串str和一个后缀suffix。我们使用std::substr函数从字符串的末尾截取与后缀相同长度的子字符串,然后与后缀进行比较。如果它们相同,我们就说明该字符串包含了特定的后缀。

这种方法很简单,但是它只适用于小字符串。如果您需要在大型文本文件或数据流中查找字符串,这种方法可能会过于冗长和低效。因此,我们可以使用更高级的C++函数和数据结构,例如正则表达式或Trie树。

Trie树是一种高效的数据结构,用于存储和查找字符串。它使用树形结构来存储字符串,每个节点都代表一个字符串的前缀或整个字符串。叶节点表示字符串的完整结尾。以下是一个示例代码,用于在Trie树中查找带有特定后缀的字符串:


#include <iostream>

#include <unordered_map>

class TrieNode

{

public:

  bool isWord;

  std::unordered_map<char, TrieNode*> children;

  TrieNode() : isWord(false) {}

  ~TrieNode()

  {

    for(auto& child : children)

    

      delete child.second;

    

  }

};

class Trie

{

public:

  Trie() : root(new TrieNode()) {}

  void insert(const std::string& word)

  {

    TrieNode* node = root;

    for(char c : word)

    {

      if(!node->children.count(c))

      {

        node->children[c] = new TrieNode();

      }

      node = node->children[c];

    }

    node->isWord = true;

  }

  bool search(const std::string& word)

  {

    TrieNode* node = root;

    for(char c : word)

    {

      if(!node->children.count(c))

      

        return false;

      

      node = node->children[c];

    }

    return node->isWord;

  }

  bool searchSuffix(const std::string& suffix)

  {

    TrieNode* node = root;

    for(char c : suffix)

    {

      if(!node->children.count(c))

      

        return false;

      

      node = node->children[c];

    }

    return searchSuffix(node);

  }

  bool searchSuffix(TrieNode* node)

  {

    if(node->isWord)

    

      return true;

    

    for(auto& child : node->children)

    {

      if(searchSuffix(child.second))

      

        return true;

      

    }

    return false;

  }

private:

  TrieNode* root;

};

int main()

{

  Trie trie;

  trie.insert("hello world.mp4");

  trie.insert("C++ program.exe");

  std::cout << "Searching for mp4 suffix: " << (trie.searchSuffix(".mp4") ? "found" : "not found") << std::endl;

  std::cout << "Searching for exe suffix: " << (trie.searchSuffix(".exe") ? "found" : "not found") << std::endl;

  return 0;

}

在这个示例中,我们创建了一个Trie类,并实现了insert、search和searchSuffix函数。insert函数用于向Trie中插入字符串,search函数用于在Trie中查找完整的字符串,searchSuffix函数用于查找带有特定后缀的字符串。

我们首先插入了两个字符串,然后分别查找mp4和exe后缀的字符串。在searchSuffix函数中,我们从后缀的第一个字符开始,按字母顺序遍历Trie,直到找到一个叶节点,这表明在Trie中找到了带有特定后缀的字符串。

尽管这种方法比std::find更复杂,但它在处理大量数据时更快且更有效。如果您需要在自己的项目中查找带有特定后缀的字符串,这些代码示例可以作为起点帮助您完成这一任务。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复
    相似文章