117.info
人生若只如初见

c++ string类的查找功能如何使用

C++中的std::string类提供了几种查找功能,包括find(), rfind(), find_first_of(), find_last_of()等。下面是这些函数的简单介绍和示例:

  1. size_t find(const std::string& str, size_t pos = 0) const noexcept;

    在当前字符串中从位置pos开始查找子字符串str第一次出现的位置。如果找不到则返回std::string::npos

    #include
    #include
    
    int main() {
        std::string s("hello world");
        std::string sub("world");
        size_t pos = s.find(sub);
        if (pos != std::string::npos) {
            std::cout << "Found at position: "<< pos<< std::endl;
        } else {
            std::cout << "Not found"<< std::endl;
        }
        return 0;
    }
    
  2. size_t rfind(const std::string& str, size_t pos = npos) const noexcept;

    在当前字符串中从位置pos开始查找子字符串str最后一次出现的位置。如果找不到则返回std::string::npos

    #include
    #include
    
    int main() {
        std::string s("hello world world");
        std::string sub("world");
        size_t pos = s.rfind(sub);
        if (pos != std::string::npos) {
            std::cout << "Found at position: "<< pos<< std::endl;
        } else {
            std::cout << "Not found"<< std::endl;
        }
        return 0;
    }
    
  3. size_t find_first_of(const std::string& str, size_t pos = 0) const noexcept;

    在当前字符串中从位置pos开始查找str中任意字符第一次出现的位置。如果找不到则返回std::string::npos

    #include
    #include
    
    int main() {
        std::string s("hello world");
        std::string chars("wro");
        size_t pos = s.find_first_of(chars);
        if (pos != std::string::npos) {
            std::cout << "Found at position: "<< pos<< std::endl;
        } else {
            std::cout << "Not found"<< std::endl;
        }
        return 0;
    }
    
  4. size_t find_last_of(const std::string& str, size_t pos = npos) const noexcept;

    在当前字符串中从位置pos开始查找str中任意字符最后一次出现的位置。如果找不到则返回std::string::npos

    #include
    #include
    
    int main() {
        std::string s("hello world");
        std::string chars("wro");
        size_t pos = s.find_last_of(chars);
        if (pos != std::string::npos) {
            std::cout << "Found at position: "<< pos<< std::endl;
        } else {
            std::cout << "Not found"<< std::endl;
        }
        return 0;
    }
    

这些函数可以根据需要进行组合使用,以实现更复杂的查找功能。注意,当使用std::string::npos作为参数时,表示从字符串的末尾开始查找。

未经允许不得转载 » 本文链接:https://www.117.info/ask/fe407AzsBBQ9VBA.html

推荐文章

  • C# XML的操作方法有哪些

    C#中可以使用以下方法来操作XML: XmlDocument类:该类提供了一系列的方法用于加载、创建、修改和保存XML文档。常见方法有Load、Save、SelectSingleNode、Select...

  • c++中system("pause")的作用和含义

    在C++中,system(“pause”)的作用是暂停程序的执行,并等待用户按下任意键后继续执行。它将调用操作系统的命令行窗口来显示一个提示信息,并等待用户按下任意键...

  • C#中索引超出了数组界限如何解决

    索引超出了数组界限是指访问了数组中不存在的元素,这种情况会导致程序抛出 IndexOutOfRangeException 异常。为了解决这个问题,可以采取以下几种方法: 检查索引...

  • c++ fstream中seekg()和seekp()的用法

    在C++的fstream库中,seekg()和seekp()是用来移动文件指针的函数。它们分别用于对输入流和输出流进行定位。
    seekg()函数用于对输入流进行定位,即读取文件时...

  • 如何比较c++ string类的对象

    在 C++ 中,可以使用标准库中的 std::string 类来表示字符串 相等运算符 ==:用于检查两个字符串是否相等。如果两个字符串包含相同的字符序列,则它们相等。 #in...

  • string类在c++中的内存管理

    在C++中,std::string 是一个基于字符数组的容器,它提供了方便的接口来处理字符串。std::string 的内存管理通常涉及到动态内存分配和释放。
    以下是 std::s...

  • c++ string类如何进行字符串拼接

    在 C++ 中,std::string 类提供了几种方法来进行字符串拼接。以下是一些常见的方法: 使用 + 运算符: #include
    #include int main() { std::string str1 =...

  • c++ string类的构造函数有哪些

    C++中的std::string类包含了多种构造函数,可以方便地初始化字符串。以下是一些常见的std::string构造函数: 默认构造函数 std::string(); 创建一个空字符串,即...