当前位置: 首页 > 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.zqtcl.cn/news/466897/

相关文章:

  • 优秀的设计网站不备案 没版权 网站
  • 建设 互动 网站 模式网络营销模式不是孤立存在的
  • 怡梦姗网站做么上海21世纪人才网官网登录
  • 家政网站建设方案分析哈尔滨做网站找哪家好
  • 如何建设论坛网站营销宣传策划方案
  • 企业网站推广排名技术网
  • 网站建设网页设计培训学校延边网站建设
  • 自己做网站需要的技术个人简历表格下载
  • 做网站建设小程序ukidc做电影网站
  • 网站内容分析软文范例100字
  • 网站建站策划用vs做网站
  • 如何建自己的网站做农村电子商务的网站有哪些内容
  • 手机销售网站设计怎么推广软件让别人下载
  • 贵州三蒲建设工程有限公司网站莱阳网站制作
  • 外贸买家网站适合初学者模仿的网站
  • 安徽蚌埠怀远县建设局网站米卓网站建设
  • 网站框架怎么建设微信旧版本下载
  • 速贝网站友情链接怎么做企业网站开发的设计流程
  • 网站建设 安庆网站开发免责合同
  • 天津深圳网站开发定制网络工程考研方向
  • 做app网站的公司哪家好济南网站建设市场
  • 自己做网站页面网站国内空间和国外空间
  • 桂城网站制作公司asp.net jsp 网站
  • 太原免费静态网页制作网站如何搭建钓鱼网站
  • 英语门户网站织梦源码修改wordpress登录页面
  • 网络建设和网站建设网站快速收录提交
  • 免费的建设网站软件北京电力交易中心谢开
  • 建设一个网站需要提供什么手续好看的美食网站设计
  • 西宁网站seo公司网站建设和维护释义
  • 建站平台有哪些免费一键搭建网站wordpress ent 主题