boost::string_algo详解5——erase相关函数

2024-06-15 03:48

本文主要是介绍boost::string_algo详解5——erase相关函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

erase的主要函数(以及其包括的copy函数)包括: 
erase_range, erase_first, erase_last, erase_nth, erase_head, erase_tail, erase_regex, erase_all, erase_all_regex
[cpp] view plain copy print ?
  1. void test_string_erase()  
  2. {  
  3.     using namespace boost;  
  4.     std::string str = "Hello Dolly, Hello World!";  
  5.   
  6.     std::cout << "#### erase_range ####" << std::endl;  
  7.     {  
  8.         std::string str1 = str;  
  9.         erase_range(str1, make_iterator_range(str1.begin() + 2, str1.end() - 2));  
  10.         std::cout << str1 << std::endl;  
  11.     }  
  12.     std::cout << std::endl;  
  13.   
  14.     std::cout << "#### erase_first and erase_first_copy ####" << std::endl;  
  15.     {  
  16.         std::string str2 = str;  
  17.         erase_first(str2, "Hello");  
  18.         std::cout << str2 << std::endl;  
  19.     }  
  20.     {  
  21.         std::string str2 = str;  
  22.         std::string strc1 = erase_first_copy(str2, "Hello");  
  23.         std::cout << strc1 << std::endl;  
  24.   
  25.         std::string strc2 = "result = ";  
  26.         erase_first_copy(back_inserter(strc2), str, "Hello");  
  27.         std::cout << strc2 << std::endl;  
  28.     }  
  29.     std::cout << std::endl;  
  30.   
  31.     std::cout << "#### erase_last and erase_last_copy ####" << std::endl;  
  32.     {  
  33.         std::string str3 = str;  
  34.         erase_last(str3, "Hello");  
  35.         std::cout << str3 << std::endl;  
  36.     }  
  37.     {  
  38.         std::string str3 = str;  
  39.         std::string strc1 = erase_last_copy(str3, "Hello");  
  40.         std::cout << strc1 << std::endl;  
  41.   
  42.         std::string strc2 = "result = ";  
  43.         erase_last_copy(back_inserter(strc2), str, "Hello");  
  44.         std::cout << strc2 << std::endl;  
  45.     }  
  46.     std::cout << std::endl;  
  47.   
  48.     // 注意nth的索引从0开始.  
  49.     std::cout << "#### erase_nth and erase_nth_copy ####" << std::endl;  
  50.     {  
  51.         std::string str4 = str;  
  52.         erase_nth(str4, "Hello", 1);  
  53.         std::cout << str4 << std::endl;  
  54.     }  
  55.     {  
  56.         std::string str4 = str;  
  57.         std::string strc1 = erase_nth_copy(str4, "Hello", 1);  
  58.         std::cout << strc1 << std::endl;  
  59.   
  60.         std::string strc2 = "result = ";  
  61.         erase_nth_copy(back_inserter(strc2), str, "Hello", 1);  
  62.         std::cout << strc2 << std::endl;  
  63.     }  
  64.     std::cout << std::endl;  
  65.   
  66.     std::cout << "#### erase_all and erase_all_copy ####" << std::endl;  
  67.     {  
  68.         std::string str5 = str;  
  69.         erase_all(str5, "Hello");  
  70.         std::cout << str5 << std::endl;  
  71.     }  
  72.     {  
  73.         std::string str5 = str;  
  74.         std::string strc1 = erase_all_copy(str5, "Hello");  
  75.         std::cout << strc1 << std::endl;  
  76.   
  77.         std::string strc2 = "result = ";  
  78.         erase_all_copy(back_inserter(strc2), str, "Hello");  
  79.         std::cout << strc2 << std::endl;  
  80.     }  
  81.     std::cout << std::endl;  
  82.   
  83.     std::cout << "#### erase_head and erase_head_copy ####" << std::endl;  
  84.     {  
  85.         std::string str6 = str;  
  86.         erase_head(str6, 3);  
  87.         std::cout << str6 << std::endl;  
  88.     }  
  89.     {  
  90.         std::string str6 = str;  
  91.         std::string strc1 = erase_head_copy(str6, 3);  
  92.         std::cout << strc1 << std::endl;  
  93.   
  94.         std::string strc2 = "result = ";  
  95.         erase_head_copy(back_inserter(strc2), str, 3);  
  96.         std::cout << strc2 << std::endl;  
  97.     }  
  98.     std::cout << std::endl;  
  99.   
  100.     std::cout << "#### erase_tail and erase_tail_copy ####" << std::endl;  
  101.     {  
  102.         std::string str6 = str;  
  103.         erase_tail(str6, 3);  
  104.         std::cout << str6 << std::endl;  
  105.     }  
  106.     {  
  107.         std::string str6 = str;  
  108.         std::string strc1 = erase_tail_copy(str6, 3);  
  109.         std::cout << strc1 << std::endl;  
  110.   
  111.         std::string strc2 = "result = ";  
  112.         erase_tail_copy(back_inserter(strc2), str, 3);  
  113.         std::cout << strc2 << std::endl;  
  114.     }  
  115.     std::cout << std::endl;  
  116.   
  117.     std::cout << "#### erase_regex, erase_regex_copy ####" << std::endl;  
  118.     {  
  119.         std::string str6 = str;  
  120.         regex reg("H.*?o");  
  121.   
  122.         erase_regex(str6, reg);  
  123.         std::cout << str6 << std::endl;  
  124.     }  
  125.   
  126.     {  
  127.         std::string str6 = str;  
  128.         regex reg("H.*?o");  
  129.         std::string strc1 = erase_regex_copy(str6, reg);  
  130.         std::cout << strc1 << std::endl;  
  131.   
  132.         std::string strc2 = "result = ";  
  133.         erase_regex_copy(back_inserter(strc2), str, reg);  
  134.         std::cout << strc2 << std::endl;  
  135.     }  
  136.     std::cout << std::endl;  
  137.   
  138.     std::cout << "#### erase_all_regex, erase_all_regex_copy ####" << std::endl;  
  139.     {  
  140.         std::string str6 = str;  
  141.         regex reg("H.*?o");  
  142.   
  143.         erase_all_regex(str6, reg);  
  144.         std::cout << str6 << std::endl;  
  145.     }  
  146.     {  
  147.         std::string str6 = str;  
  148.         regex reg("H.*?o");  
  149.         std::string strc1 = erase_all_regex_copy(str6, reg);  
  150.         std::cout << strc1 << std::endl;  
  151.   
  152.         std::string strc2 = "result = ";  
  153.         erase_all_regex_copy(back_inserter(strc2), str, reg);  
  154.         std::cout << strc2 << std::endl;  
  155.     }  
  156.     std::cout << std::endl;  
  157. }  

这篇关于boost::string_algo详解5——erase相关函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1062348

相关文章

MySQL 中的 JSON 查询案例详解

《MySQL中的JSON查询案例详解》:本文主要介绍MySQL的JSON查询的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 的 jsON 路径格式基本结构路径组件详解特殊语法元素实际示例简单路径复杂路径简写操作符注意MySQL 的 J

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Python ZIP文件操作技巧详解

《PythonZIP文件操作技巧详解》在数据处理和系统开发中,ZIP文件操作是开发者必须掌握的核心技能,Python标准库提供的zipfile模块以简洁的API和跨平台特性,成为处理ZIP文件的首选... 目录一、ZIP文件操作基础三板斧1.1 创建压缩包1.2 解压操作1.3 文件遍历与信息获取二、进阶技

一文详解Java异常处理你都了解哪些知识

《一文详解Java异常处理你都了解哪些知识》:本文主要介绍Java异常处理的相关资料,包括异常的分类、捕获和处理异常的语法、常见的异常类型以及自定义异常的实现,文中通过代码介绍的非常详细,需要的朋... 目录前言一、什么是异常二、异常的分类2.1 受检异常2.2 非受检异常三、异常处理的语法3.1 try-

Java中的@SneakyThrows注解用法详解

《Java中的@SneakyThrows注解用法详解》:本文主要介绍Java中的@SneakyThrows注解用法的相关资料,Lombok的@SneakyThrows注解简化了Java方法中的异常... 目录前言一、@SneakyThrows 简介1.1 什么是 Lombok?二、@SneakyThrows

Java中字符串转时间与时间转字符串的操作详解

《Java中字符串转时间与时间转字符串的操作详解》Java的java.time包提供了强大的日期和时间处理功能,通过DateTimeFormatter可以轻松地在日期时间对象和字符串之间进行转换,下面... 目录一、字符串转时间(一)使用预定义格式(二)自定义格式二、时间转字符串(一)使用预定义格式(二)自

Redis Pipeline(管道) 详解

《RedisPipeline(管道)详解》Pipeline管道是Redis提供的一种批量执行命令的机制,通过将多个命令一次性发送到服务器并统一接收响应,减少网络往返次数(RTT),显著提升执行效率... 目录Redis Pipeline 详解1. Pipeline 的核心概念2. 工作原理与性能提升3. 核

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

Nginx location匹配模式与规则详解

《Nginxlocation匹配模式与规则详解》:本文主要介绍Nginxlocation匹配模式与规则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、环境二、匹配模式1. 精准模式2. 前缀模式(不继续匹配正则)3. 前缀模式(继续匹配正则)4. 正则模式(大

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl