boost::string_algo详解6——replace相关函数

2024-06-15 03:48

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

replace的主要函数(以及其包括的copy函数)包括:
replace_range, replace_first, replace_last, replace_nth, replace_head, replace_tail, replace_regex, replace_all, replace_all_regex

[cpp]  view plain copy print ?
  1. void test_string_replace()  
  2. {  
  3.     using namespace boost;  
  4.     std::string str = "Hello Dolly, Hello World!";  
  5.   
  6.     std::cout << "#### replace_range ####" << std::endl;  
  7.     {  
  8.         std::string str1 = str;  
  9.         replace_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 << "#### replace_first and replace_first_copy ####" << std::endl;  
  15.     {  
  16.         std::string str2 = str;  
  17.         replace_first(str2, "Hello""GoodBye");  
  18.         std::cout << str2 << std::endl;  
  19.     }  
  20.     {  
  21.         std::string str2 = str;  
  22.         std::string strc1 = replace_first_copy(str2, "Hello""GoodBye");  
  23.         std::cout << strc1 << std::endl;  
  24.   
  25.         std::string strc2 = "result = ";  
  26.         replace_first_copy(back_inserter(strc2), str, "Hello""GoodBye");  
  27.         std::cout << strc2 << std::endl;  
  28.     }  
  29.     std::cout << std::endl;  
  30.   
  31.     std::cout << "#### replace_last and replace_last_copy ####" << std::endl;  
  32.     {  
  33.         std::string str3 = str;  
  34.         replace_last(str3, "Hello""GoodBye");  
  35.         std::cout << str3 << std::endl;  
  36.     }  
  37.     {  
  38.         std::string str3 = str;  
  39.         std::string strc1 = replace_last_copy(str3, "Hello""GoodBye");  
  40.         std::cout << strc1 << std::endl;  
  41.   
  42.         std::string strc2 = "result = ";  
  43.         replace_last_copy(back_inserter(strc2), str, "Hello""GoodBye");  
  44.         std::cout << strc2 << std::endl;  
  45.     }  
  46.     std::cout << std::endl;  
  47.   
  48.     // 注意nth的索引从0开始.  
  49.     std::cout << "#### replace_nth and replace_nth_copy ####" << std::endl;  
  50.     {  
  51.         std::string str4 = str;  
  52.         replace_nth(str4, "Hello", 1, "GoodBye");  
  53.         std::cout << str4 << std::endl;  
  54.     }  
  55.     {  
  56.         std::string str4 = str;  
  57.         std::string strc1 = replace_nth_copy(str4, "Hello", 1, "GoodBye");  
  58.         std::cout << strc1 << std::endl;  
  59.   
  60.         std::string strc2 = "result = ";  
  61.         replace_nth_copy(back_inserter(strc2), str, "Hello", 1, "GoodBye");  
  62.         std::cout << strc2 << std::endl;  
  63.     }  
  64.     std::cout << std::endl;  
  65.   
  66.     std::cout << "#### replace_all and replace_all_copy ####" << std::endl;  
  67.     {  
  68.         std::string str5 = str;  
  69.         replace_all(str5, "Hello""GoodBye");  
  70.         std::cout << str5 << std::endl;  
  71.     }  
  72.     {  
  73.         std::string str5 = str;  
  74.         std::string strc1 = replace_all_copy(str5, "Hello""GoodBye");  
  75.         std::cout << strc1 << std::endl;  
  76.   
  77.         std::string strc2 = "result = ";  
  78.         replace_all_copy(back_inserter(strc2), str, "Hello""GoodBye");  
  79.         std::cout << strc2 << std::endl;  
  80.     }  
  81.     std::cout << std::endl;  
  82.   
  83.     std::cout << "#### replace_head and replace_head_copy ####" << std::endl;  
  84.     {  
  85.         std::string str6 = str;  
  86.         replace_head(str6, 5, "GoodBye");  
  87.         std::cout << str6 << std::endl;  
  88.     }  
  89.     {  
  90.         std::string str6 = str;  
  91.         std::string strc1 = replace_head_copy(str6, 5, "GoodBye");  
  92.         std::cout << strc1 << std::endl;  
  93.   
  94.         std::string strc2 = "result = ";  
  95.         replace_head_copy(back_inserter(strc2), str, 5, "GoodBye");  
  96.         std::cout << strc2 << std::endl;  
  97.     }  
  98.     std::cout << std::endl;  
  99.   
  100.     std::cout << "#### replace_tail and replace_tail_copy ####" << std::endl;  
  101.     {  
  102.         std::string str6 = str;  
  103.         replace_tail(str6, 6, "GoodBye");  
  104.         std::cout << str6 << std::endl;  
  105.     }  
  106.     {  
  107.         std::string str6 = str;  
  108.         std::string strc1 = replace_tail_copy(str6, 6, "GoodBye");  
  109.         std::cout << strc1 << std::endl;  
  110.   
  111.         std::string strc2 = "result = ";  
  112.         replace_tail_copy(back_inserter(strc2), str, 6, "GoodBye");  
  113.         std::cout << strc2 << std::endl;  
  114.     }  
  115.     std::cout << std::endl;  
  116.   
  117.     std::cout << "#### replace_regex, replace_regex_copy ####" << std::endl;  
  118.     {  
  119.         std::string str6 = str;  
  120.         regex reg("H.*?o");  
  121.   
  122. //      replace_regex(str6, reg, "GoodBye");    // 直接这么写会报错. 要用如下的方法.  
  123.         std::string strReplace = "GoodBye";  
  124.         replace_regex(str6, reg, strReplace);  
  125.   
  126.         std::cout << str6 << std::endl;  
  127.     }  
  128.   
  129.     {  
  130.         std::string str6 = str;  
  131.         regex reg("H.*?o");  
  132.         std::string strReplace = "GoodBye";  
  133.         std::string strc1 = replace_regex_copy(str6, reg, strReplace);  
  134.         std::cout << strc1 << std::endl;  
  135.   
  136.         std::string strc2 = "result = ";  
  137.         replace_regex_copy(back_inserter(strc2), str, reg, strReplace);  
  138.         std::cout << strc2 << std::endl;  
  139.     }  
  140.     std::cout << std::endl;  
  141.   
  142.     std::cout << "#### replace_all_regex, replace_all_regex_copy ####" << std::endl;  
  143.     {  
  144.         std::string str6 = str;  
  145.         regex reg("H.*?o");  
  146.         std::string strReplace = "GoodBye";  
  147.         replace_all_regex(str6, reg, strReplace);  
  148.         std::cout << str6 << std::endl;  
  149.     }  
  150.     {  
  151.         std::string str6 = str;  
  152.         regex reg("H.*?o");  
  153.         std::string strReplace = "GoodBye";  
  154.         std::string strc1 = replace_all_regex_copy(str6, reg, strReplace);  
  155.         std::cout << strc1 << std::endl;  
  156.   
  157.         std::string strc2 = "result = ";  
  158.         replace_all_regex_copy(back_inserter(strc2), str, reg, strReplace);  
  159.         std::cout << strc2 << std::endl;  
  160.     }  
  161.     std::cout << std::endl;  
  162. }  

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



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

相关文章

Debezium 与 Apache Kafka 的集成方式步骤详解

《Debezium与ApacheKafka的集成方式步骤详解》本文详细介绍了如何将Debezium与ApacheKafka集成,包括集成概述、步骤、注意事项等,通过KafkaConnect,D... 目录一、集成概述二、集成步骤1. 准备 Kafka 环境2. 配置 Kafka Connect3. 安装 D

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

Spring Cloud LoadBalancer 负载均衡详解

《SpringCloudLoadBalancer负载均衡详解》本文介绍了如何在SpringCloud中使用SpringCloudLoadBalancer实现客户端负载均衡,并详细讲解了轮询策略和... 目录1. 在 idea 上运行多个服务2. 问题引入3. 负载均衡4. Spring Cloud Load

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为

SQL 中多表查询的常见连接方式详解

《SQL中多表查询的常见连接方式详解》本文介绍SQL中多表查询的常见连接方式,包括内连接(INNERJOIN)、左连接(LEFTJOIN)、右连接(RIGHTJOIN)、全外连接(FULLOUTER... 目录一、连接类型图表(ASCII 形式)二、前置代码(创建示例表)三、连接方式代码示例1. 内连接(I

Go路由注册方法详解

《Go路由注册方法详解》Go语言中,http.NewServeMux()和http.HandleFunc()是两种不同的路由注册方式,前者创建独立的ServeMux实例,适合模块化和分层路由,灵活性高... 目录Go路由注册方法1. 路由注册的方式2. 路由器的独立性3. 灵活性4. 启动服务器的方式5.

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

Java中八大包装类举例详解(通俗易懂)

《Java中八大包装类举例详解(通俗易懂)》:本文主要介绍Java中的包装类,包括它们的作用、特点、用途以及如何进行装箱和拆箱,包装类还提供了许多实用方法,如转换、获取基本类型值、比较和类型检测,... 目录一、包装类(Wrapper Class)1、简要介绍2、包装类特点3、包装类用途二、装箱和拆箱1、装