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

深圳集团网站建设报价双公示 网站专栏建设

深圳集团网站建设报价,双公示 网站专栏建设,优秀校园网站,忻州建站公司【C】使用 list 与 string 实现基础字符串操作 文章目录 一、字符串的基础操作1.1 - startsWith1.2 - endsWith1.3 - trim1.4 - indexOf1.5 - replaceAll 二、list 基础操作2.1 - 遍历2.1.1 - 使用迭代器访问2.1.2 - 使用基于范围的 for 循环遍历2.1.3 - 使用标准算法库遍历 2.… 【C】使用 list 与 string 实现基础字符串操作 文章目录 一、字符串的基础操作1.1 - startsWith1.2 - endsWith1.3 - trim1.4 - indexOf1.5 - replaceAll 二、list 基础操作2.1 - 遍历2.1.1 - 使用迭代器访问2.1.2 - 使用基于范围的 for 循环遍历2.1.3 - 使用标准算法库遍历 2.2 - 访问元素2.3 - 删除元素 三、list\string\3.1 - 移除所有空字符串元素3.2 - 遍历字符串并应用 trim3.3 - 移除连续的空白行 一、字符串的基础操作 1.1 - startsWith bool startsWith(const std::string fullString, const std::string starting) {if (fullString.length() starting.length()) {return (0 fullString.compare(0, starting.length(), starting));} else {return false;} }1.2 - endsWith bool endsWith(const std::string fullString, const std::string ending) {if (fullString.length() ending.length()){return (0 fullString.compare(fullString.length() - ending.length(), ending.length(), ending));}else{return false;} }1.3 - trim 用于移除字符串前后两端的空白符 // Function to trim whitespace from the beginning and end of a string std::string trim(const std::string str) {size_t first str.find_first_not_of( \t\n\r\f\v);// No non-whitespace charactersif (first std::string::npos){ // 如果从头开始非空白符找不到说明所有的字符都是空白符因此全部去掉return ; }size_t last str.find_last_not_of( \t\n\r\f\v);// 即便 last 为 string::npos substr 也会做处理。return str.substr(first, (last - first 1)); }或者 #include algorithm #include cctype// 去除字符串左侧空白 static inline void ltrim(std::string s) {s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {return !std::isspace(ch);})); }// 去除字符串右侧空白 static inline void rtrim(std::string s) {s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {return !std::isspace(ch);}).base(), s.end()); }// 去除字符串两侧空白 static inline void trim(std::string s) {ltrim(s);rtrim(s); }1.4 - indexOf 用于获取第一个子串的位置索引如果找不到则返回 -1。 // Function to find the index of the first occurrence of a substring int indexOf(const std::string str, const std::string substr) {size_t pos str.find(substr);return (pos ! std::string::npos) ? static_castint(pos) : -1; }1.5 - replaceAll // 替换字符串中所有匹配的子字符串 void replaceAll(std::string source, const std::string from, const std::string to) {// 如果字符串为空则返回。if (from.empty()) { return; }size_t startPos 0;while ((startPos source.find(from, startPos)) ! std::string::npos) {source.replace(startPos, from.length(), to);startPos to.length(); // 在替换后移动过去新增的部分} }二、list 基础操作 2.1 - 遍历 2.1.1 - 使用迭代器访问 #include iostream #include list std::listint myList {1, 2, 3, 4, 5};// 使用迭代器遍历 std::list for (auto it myList.begin(); it ! myList.end(); it) {std::cout *it ; } std::cout std::endl;2.1.2 - 使用基于范围的 for 循环遍历 #include iostream #include list // 使用范围基 for 循环遍历 std::list for (int elem : myList) {std::cout elem ; } std::cout std::endl;2.1.3 - 使用标准算法库遍历 #include iostream #include list #include algorithm // for std::for_each std::listint myList {1, 2, 3, 4, 5};// 使用 std::for_each 遍历 std::list std::for_each(myList.begin(), myList.end(), [](int elem) {std::cout elem ; }); std::cout std::endl;2.2 - 访问元素 访问第 N 个元素 #include iostream #include liststd::listint myList {10, 20, 30, 40, 50}; int N 3; // 以 0 为起始索引访问第 4 个元素 auto it myList.begin(); std::advance(it, N); // 使用 std::advance 前进到第 N 个元素if (it ! myList.end()) {std::cout The element at index N is *it std::endl; } else {std::cout Index out of range. std::endl; }2.3 - 删除元素 删除前 N 个元素 std::listint myList {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};int N 3; // 指定要删除的元素数量 if (N myList.size()) {// 获取开始到第 N 个元素的迭代器auto it myList.begin();std::advance(it, N); // 移动迭代器到第 N 个位置// 从开始到第 N 个元素进行删除myList.erase(myList.begin(), it); }// 打印剩余的元素 for (int elem : myList) {std::cout elem ; } std::cout std::endl;三、liststring 3.1 - 移除所有空字符串元素 #include iostream #include list #include string// 创建并初始化一个 std::liststd::string std::liststd::string strings {Hello, , World, , C17, }; // 输出原始列表 std::cout Original list: std::endl; for (const auto str : strings) {std::cout str std::endl; } // 移除所有空字符串 strings.remove_if([](const std::string s) { return s.empty(); }); // 输出修改后的列表 std::cout \nList after removing empty strings: std::endl; for (const auto str : strings) {std::cout str std::endl; }3.2 - 遍历字符串并应用 trim std::liststd::string myStrings { hello , world! , example };// 遍历列表并应用 trim 函数 for (std::string str : myStrings) {trim(str); } // 打印修剪后的字符串列表 for (const auto str : myStrings) {std::cout str std::endl; }3.3 - 移除连续的空白行 将多个连续的空白行替换为一个空白行 #include iostream #include list #include string #include iteratorvoid compressEmptyLines(std::liststd::string lines) {bool lastWasEmpty false;for (auto it lines.begin(); it ! lines.end(); ) {// 检查当前行是否为空白或只包含空格bool isEmpty it-find_first_not_of( \t\n\v\f\r) std::string::npos;if (isEmpty) {if (lastWasEmpty) {// 如果当前行是空的并且上一行也是空的删除当前行it lines.erase(it);} else {// 如果当前行是空的但上一行不是保留这行并标记lastWasEmpty true;it;}} else {// 如果当前行不是空的继续前进lastWasEmpty false;it;}} }int main() {std::liststd::string lines {Hello, , ,World,,,!, ,End};compressEmptyLines(lines);// 输出处理后的列表for (const auto line : lines) {std::cout line std::endl;}return 0; }
http://www.pierceye.com/news/466897/

相关文章:

  • asp网站生成静态在线网站软件免费下载
  • wordpress古腾堡编辑器广州建网站开发seo型企业网站
  • 易云巢做营销型网站wordpress下载视频播放器
  • google网站哪里找人做网站
  • 外贸网站建设如何做呢百度热搜风云榜
  • ASP网站建设实训报告总结山西建设公司网站
  • 网站建设使用的什么软件房屋设计风格
  • 厦门网站建设公司排名杭州小型网站建设服务
  • 西安建设主管部门官方网站中文搭建式软件开发工具
  • 南通网站建设论文网站首页导航栏怎么做
  • 建设网站的运行费包括什么北京市建设工程信息网安徽兴创
  • 我想在泉州做网站上上海网站设计
  • 百度seo网站优化一 网站开发体会
  • 成都58手机微信网站建设名录tiktok跨境电商好做吗
  • 电子科技 深圳 网站建设怎么做打码网站
  • 南宁小程序开发网站建设公司公司网站建设怎么选择
  • erp网站代做项目网络进度图
  • 网站建设胶州家园外贸网站怎么注册
  • 我想找阿里巴巴做网站推广建一个公司网站花多少钱
  • 最新购物网站建设框架wordpress 登录后台乱码
  • 音频网站开发做外贸网站需要营业执照
  • 企业网站搭建项目概述范文wordpress更改链接地址
  • 免费网站在线观看人数在哪直播建设工业网站首页
  • 权威的南昌网站设计wordpress游客评论游客
  • 乡镇府建设网站什么是外链
  • 营销型网站设计流程电子商务网站建设软件
  • 做个人网站的步骤wordpress 添加新页面
  • 公司建网站流程网站布局选择
  • 云南效果好的网站优化微信如何做有趣的短视频网站
  • wordpress个人网站后台登陆dedecms仿站