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

无锡黄页网衡阳seo快速排名

无锡黄页网,衡阳seo快速排名,公众平台如何做网站,深圳企业做网站作者的个人gitee▶️ 作者的算法讲解主页 每日一言:“山高自有行路客,水深亦有渡船人。🌸🌸” 概述:std::string 的用法 取自:cplusplus.com/reference/string/string/ String类可以使用的成员函数非常多&…

作者的个人gitee▶️
作者的算法讲解主页

每日一言:“山高自有行路客,水深亦有渡船人。🌸🌸”

概述:std::string 的用法

取自:cplusplus.com/reference/string/string/

在这里插入图片描述

String类可以使用的成员函数非常多,这里仅列举一些比较常用的成员函数。

🔴1. 构造函数

std::string 提供了多种构造方式:

  • 默认构造:创建一个空字符串。

    std::string s1;
    
  • 从 C 风格字符串构造:将 C 风格字符串转换为 std::string

    const char* cstr = "Hello, World!";
    std::string s2(cstr);
    
  • 从另一个字符串构造:可以指定子字符串的范围。

    std::string s3(s2, 7, 5); // 从 s2 的第 7 个字符开始,取 5 个字符,结果为 "World"
    
  • 重复字符构造:指定字符和重复次数。

    std::string s4(5, 'a'); // 结果为 "aaaaa"
    

🔴2. 字符串拼接

std::string 支持使用 ++= 操作符进行拼接。

std::string s1 = "Hello";
std::string s2 = "World";
std::string s3 = s1 + ", " + s2 + "!";
s1 += " C++"; // s1 现在为 "Hello C++"

🔴3. 字符串比较

std::string 支持使用比较操作符(==!=<><=>=)进行比较。

std::string s1 = "apple";
std::string s2 = "banana";
std::string s3 = "apple";if (s1 == s3) {std::cout << "s1 和 s3 相等" << std::endl;
}
if (s1 < s2) {std::cout << "s1 小于 s2" << std::endl;
}

🔴4. 访问字符

可以通过下标操作符 [] 或成员函数 at() 访问字符串中的字符。

std::string s = "Hello";
char c1 = s[0]; // c1 为 'H'
char c2 = s.at(1); // c2 为 'e'// 修改字符
s[0] = 'h'; // s 现在为 "hello"

🔴5. 字符串长度和大小

  • size():返回字符串的长度(字符数)。
  • length():与 size() 功能相同。
  • empty():判断字符串是否为空。
std::string s = "Hello";
std::cout << "长度: " << s.size() << std::endl; // 输出 5
std::cout << "是否为空: " << s.empty() << std::endl; // 输出 0 (false)

🔴6. 子字符串操作

  • substr():提取子字符串。
std::string s = "Hello, World!";
std::string sub = s.substr(7, 5); // 从第 7 个字符开始,取 5 个字符,结果为 "World"

🔴7. 查找和替换

  • find():查找子字符串的位置。
  • replace():替换子字符串。
std::string s = "Hello, World!";
size_t pos = s.find("World"); // 返回 7
if (pos != std::string::npos) {s.replace(pos, 5, "C++"); // 将 "World" 替换为 "C++"std::cout << s << std::endl; // 输出 "Hello, C++!"
}

🔴8. 字符串转换

  • c_str():将 std::string 转换为 C 风格字符串。
  • std::to_string():将数字转换为字符串。
std::string s = "Hello";
const char* cstr = s.c_str(); // C 风格字符串int num = 42;
std::string num_str = std::to_string(num); // 将数字转换为字符串

🔴9. 字符串流

可以使用 std::stringstream 来方便地进行字符串的格式化和解析。

#include <sstream>std::stringstream ss;
ss << "Hello" << ", " << 42 << "!";
std::string result = ss.str(); // result 为 "Hello, 42!"// 解析字符串
std::string word;
int num;
std::stringstream("Hello 42") >> word >> num; // word 为 "Hello",num 为 42

🔴10. 常用的成员函数

  • clear():清空字符串。
  • resize():调整字符串大小。
  • append():追加字符串。
  • erase():删除字符。
std::string s = "Hello";
s.clear(); // 清空字符串,s 现在为空s.resize(10, 'x'); // 调整大小,不足部分用 'x' 填充,s 现在为 "Helloxxxxx"s.append(", World!"); // 追加字符串,s 现在为 "Helloxxxxx, World!"s.erase(5, 5); // 删除从第 5 个字符开始的 5 个字符,s 现在为 "Hello, World!"

如有错误,恳请指正。

http://www.dtcms.com/wzjs/209000.html

相关文章:

  • 装修房子什么风格最耐看windows 优化大师
  • 怎么用织梦做购物网站如何优化网页
  • 晚上做设计挣钱的网站seo关键词怎么填
  • 做视频网站空间要多大中国大数据平台官网
  • 淮安做网站的有多少百度自动搜索关键词软件
  • 做单挣钱的网站百度指数专业版价格
  • 企业网站建设ppt站长收录
  • 山西省城乡和建设厅网站制造业中小微企业
  • 临沂疫情最新数据消息正规seo排名外包
  • 网站建设要准备什么长春网站关键词排名
  • 宁波营销型网站建设上海高玩seo
  • 织梦网络公司网站微信广告怎么投放
  • 网站开发维护的好处电商运营公司简介
  • 做网站怎么赚钱的快速建站
  • 免费中文网站模板百度的营销方式有哪些
  • 物流专线网站建设免费crm系统手机版
  • 响应式网站开发工具seo外贸公司推广
  • seo网站结构线下课程seo
  • 制作企业网站需要什么费用百度推广费用多少钱
  • 广州门户网站建设方案汕头seo不错
  • 高端网站设计企业网站建设腾讯企点qq
  • 二道网站建设seo关键词分类
  • 做书封面的模板下载网站今日头条官网首页
  • 织梦cms手机网站网络推广方式主要有
  • B2B网站建设哪家好衡阳seo
  • 如何做色流量网站html网页完整代码作业
  • 福清可以做宣传的网站搜索引擎排名谷歌
  • wordpress radiate电子商务seo
  • 服装设计有哪些网站怎么自己做一个小程序
  • 有哪些网站可以做海报最新新闻热点事件2024