当前位置: 首页 > news >正文

网站建设的重要性与价值本地生活网

网站建设的重要性与价值,本地生活网,丁的老头seo博客,asp网站会员管理模板在 C 的 std::string 类中,有几个成员函数可以用于在字符串中执行搜索和子字符串提取操作。以下是这些函数的简要说明: find(): 查找子字符串的第一个出现位置。 size_t find(const string& str, size_t pos 0) const; size_t find(const char* s, …

在 C++ 的 std::string 类中,有几个成员函数可以用于在字符串中执行搜索和子字符串提取操作。以下是这些函数的简要说明:

  1. find(): 查找子字符串的第一个出现位置。

    size_t find(const string& str, size_t pos = 0) const;
    size_t find(const char* s, size_t pos = 0) const;
    

    这个函数返回子字符串 str 或 C 字符串 s 第一次出现的位置(索引)。可以指定搜索的起始位置 pos。如果找不到子字符串,返回 string::npos

  2. rfind(): 反向查找子字符串的最后一个出现位置。

    size_t rfind(const string& str, size_t pos = npos) const;
    size_t rfind(const char* s, size_t pos = npos) const;
    

    这个函数返回子字符串 str 或 C 字符串 s 最后一次出现的位置(索引)。可以指定搜索的起始位置 pos,默认情况下从字符串的末尾开始搜索。如果找不到子字符串,返回 string::npos

  3. find_first_of(): 查找给定字符集合中任意字符第一次出现的位置。

    size_t find_first_of(const string& str, size_t pos = 0) const;
    size_t find_first_of(const char* s, size_t pos = 0) const;
    

    这个函数返回在子字符串 str 或 C 字符串 s 中任意字符的第一次出现的位置(索引)。可以指定搜索的起始位置 pos。如果找不到字符,返回 string::npos

  4. find_last_of(): 反向查找给定字符集合中任意字符最后一次出现的位置。

    size_t find_last_of(const string& str, size_t pos = npos) const;
    size_t find_last_of(const char* s, size_t pos = npos) const;
    

    这个函数返回在子字符串 str 或 C 字符串 s 中任意字符的最后一次出现的位置(索引)。可以指定搜索的起始位置 pos,默认情况下从字符串的末尾开始搜索。如果找不到字符,返回 string::npos

  5. substr(): 提取子字符串。

    string substr(size_t pos = 0, size_t len = npos) const;
    

    这个函数返回从位置 pos 开始,长度为 len 的子字符串副本。如果省略 len,则返回从 pos 开始的剩余部分。

具体示例:

#include <iostream>
#include <string>int main() {std::string str = "Hello, World!";// 使用 find() 查找子字符串的第一个出现位置size_t pos = str.find("World");if (pos != std::string::npos) {std::cout << "'World' found at position " << pos << std::endl;} else {std::cout << "'World' not found" << std::endl;}// 使用 rfind() 反向查找子字符串的最后一个出现位置size_t pos_reverse = str.rfind("o");if (pos_reverse != std::string::npos) {std::cout << "'o' found at position " << pos_reverse << std::endl;} else {std::cout << "'o' not found" << std::endl;}// 使用 find_first_of() 查找给定字符集合中任意字符的第一个出现位置size_t pos_first_of = str.find_first_of("eio");if (pos_first_of != std::string::npos) {std::cout << "Any of 'eio' found at position " << pos_first_of << std::endl;} else {std::cout << "Any of 'eio' not found" << std::endl;}// 使用 find_last_of() 反向查找给定字符集合中任意字符的最后一个出现位置size_t pos_last_of = str.find_last_of("rlod");if (pos_last_of != std::string::npos) {std::cout << "Any of 'rlod' found at position " << pos_last_of << std::endl;} else {std::cout << "Any of 'rlod' not found" << std::endl;}// 使用 substr() 提取子字符串std::string substr = str.substr(7, 5);std::cout << "Substring: " << substr << std::endl;return 0;
}

输出结果:

'World' found at position 7
'o' found at position 8
Any of 'eio' found at position 1
Any of 'rlod' found at position 13
Substring: World

以上示例演示了如何使用这些函数在字符串中查找子字符串并提取子字符串的各种操作。

http://www.yayakq.cn/news/396977/

相关文章:

  • 安微省建设厅网站合肥官方网站优化费用
  • 做网站要会编程么建站工具模板
  • 苏州建设交易中心网站在线 网站建设
  • 邯郸邯山区网站建设小说网站开发l
  • 婚纱摄影网站源码服装html网站模板下载
  • 如何建造网站链接哈尔滨网站制作工具
  • 免费网站制作网站源码苏州seo关键字优化
  • 网站文章列表模板广州番禺专业建网站
  • 怎么做美食的网站深圳福田特价网站建设
  • 加强门户网站建设的方案东莞网络推广
  • wordpress建自己的网站吗美食网页设计模板代码
  • 秦皇岛网站建设seo商标购买网站
  • 怎么做自己的导航网站网页设计与制作模版
  • 南宁武鸣区建设局网站网站开发的经费预算
  • 潮州网站制作口子网站怎么做
  • 广东平台网站建设找哪家线上app怎么做
  • 义乌好品质自适应网站建设官方网站建设方案
  • 建设网站和app人工智能公众号
  • 河北做网站衡水哪家制作网站好
  • 饮料公司网站模板网站开发好学不
  • 潍坊微信网站响应式网站建设推广
  • 加工企业网站这么做做网站找酷万
  • 苏州做儿童场馆门票包票的网站网站刷流量有用吗
  • 怎么在在慧聪网上做网站推广网站开发工程师需要具备的综合素质
  • pytson做网站安全吗计算机应用网站建设与维护是做什么
  • 公司网站建设的建议搜索引擎营销概念
  • 在线免费网站seo站长工具
  • 网站建设费用如何收取中心网站建设
  • 个人可以做电商网站吗前端个人介绍网站模板下载
  • 四川省建设厅官方网站三内人员宽屏绿色新闻资讯网站织梦模板