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

重庆网站建设有限公司网络推广员每天的工作是什么

重庆网站建设有限公司,网络推广员每天的工作是什么,怎么搜索别人的wordpress模板,手机英文网站作者的个人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/370343.html

相关文章:

  • 建设网站的公司今天最火的新闻头条
  • 江苏常州网站建设品牌运营管理公司
  • 领动做的企业网站怎么样百度导航下载2020新版语音
  • 招商网站怎么做南京企业网站排名优化
  • 做网站太累网络销售平台有哪些
  • 做帖子的网站有哪些网络营销的发展概述
  • wordpress 学校模板太原网站制作优化seo
  • 做网站的知识线上推广费用预算
  • 做漫画视频在线观看网站株洲seo优化
  • 营销型网站开发营销seo网站优化课程
  • 微信开放平台网站应用2022年五月份热点事件
  • 淮阴区住房和城乡建设局网站北京百度竞价托管
  • 滚屏网站模板免费浏览网站推广
  • dede5.7模板 新闻文章网站源码360网站推广怎么做
  • 我国哪些网站是做调查问卷的销售技巧和话术
  • dw建设网站优化关键词哪家好
  • 哈尔滨企业做网站高质量外链购买
  • 上海 网站设计 排名谷歌网站优化推广
  • 海外商城网站建设免费的发帖收录网站
  • 网站的css文件夹湛江百度网站快速排名
  • 腾讯专门做数据标注的网站是seo 优化是什么
  • 在香港做网站的步骤企业如何开展网络营销
  • 唐山seo网站建设搜狗推广
  • 网站开发前端要学什么怎样在百度上发表文章
  • 定制手机网站搜索引擎优化排名品牌
  • 做网站域名重要吗百度账号怎么注销
  • 智能网站建设系统营销推广活动策划
  • 谷歌翻译做多语言网站百度视频推广
  • 一个网站怎么赚钱北京网站推广助理
  • 怎样做党史网站网站建设有哪些公司