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

相关文章

RecastNavigation之Poly相关类

Poly分成正常的Poly 和 OffMeshPoly。 正常的Poly 又分成 原始的Poly 和 Detail化的Poly,本文介绍这两种。 Poly的边分成三种类型: 1. 正常边:有tile内部的poly与之相邻 2.border边:没有poly与之相邻 3.Portal边:与之相邻的是外部tile的poly   由firstLink索引 得到第一个连接的Poly  通

十四、观察者模式与访问者模式详解

21.观察者模式 21.1.课程目标 1、 掌握观察者模式和访问者模式的应用场景。 2、 掌握观察者模式在具体业务场景中的应用。 3、 了解访问者模式的双分派。 4、 观察者模式和访问者模式的优、缺点。 21.2.内容定位 1、 有 Swing开发经验的人群更容易理解观察者模式。 2、 访问者模式被称为最复杂的设计模式。 21.3.观察者模式 观 察 者 模 式 ( Obser

【操作系统】信号Signal超详解|捕捉函数

🔥博客主页: 我要成为C++领域大神🎥系列专栏:【C++核心编程】 【计算机网络】 【Linux编程】 【操作系统】 ❤️感谢大家点赞👍收藏⭐评论✍️ 本博客致力于知识分享,与更多的人进行学习交流 ​ 如何触发信号 信号是Linux下的经典技术,一般操作系统利用信号杀死违规进程,典型进程干预手段,信号除了杀死进程外也可以挂起进程 kill -l 查看系统支持的信号

java中查看函数运行时间和cpu运行时间

android开发调查性能问题中有一个现象,函数的运行时间远低于cpu执行时间,因为函数运行期间线程可能包含等待操作。native层可以查看实际的cpu执行时间和函数执行时间。在java中如何实现? 借助AI得到了答案 import java.lang.management.ManagementFactory;import java.lang.management.Threa

Jitter Injection详解

一、定义与作用 Jitter Injection,即抖动注入,是一种在通信系统中人为地添加抖动的技术。该技术通过在发送端对数据包进行延迟和抖动调整,以实现对整个通信系统的时延和抖动的控制。其主要作用包括: 改善传输质量:通过调整数据包的时延和抖动,可以有效地降低误码率,提高数据传输的可靠性。均衡网络负载:通过对不同的数据流进行不同程度的抖动注入,可以实现网络资源的合理分配,提高整体传输效率。增

SQL Server中,always on服务器的相关操作

在SQL Server中,建立了always on服务,可用于数据库的同步备份,当数据库出现问题后,always on服务会自动切换主从服务器。 例如192.168.1.10为主服务器,12为从服务器,当主服务器出现问题后,always on自动将主服务器切换为12,保证数据库正常访问。 对于always on服务器有如下操作: 1、切换主从服务器:假如需要手动切换主从服务器时(如果两个服务

SQL Server中,isnull()函数以及null的用法

SQL Serve中的isnull()函数:          isnull(value1,value2)         1、value1与value2的数据类型必须一致。         2、如果value1的值不为null,结果返回value1。         3、如果value1为null,结果返回vaule2的值。vaule2是你设定的值。        如

Steam邮件推送内容有哪些?配置教程详解!

Steam邮件推送功能是否安全?如何个性化邮件推送内容? Steam作为全球最大的数字游戏分发平台之一,不仅提供了海量的游戏资源,还通过邮件推送为用户提供最新的游戏信息、促销活动和个性化推荐。AokSend将详细介绍Steam邮件推送的主要内容。 Steam邮件推送:促销优惠 每当平台举办大型促销活动,如夏季促销、冬季促销、黑色星期五等,用户都会收到邮件通知。这些邮件详细列出了打折游戏、

探索Elastic Search:强大的开源搜索引擎,详解及使用

🎬 鸽芷咕:个人主页  🔥 个人专栏: 《C++干货基地》《粉丝福利》 ⛺️生活的理想,就是为了理想的生活! 引入 全文搜索属于最常见的需求,开源的 Elasticsearch (以下简称 Elastic)是目前全文搜索引擎的首选,相信大家多多少少的都听说过它。它可以快速地储存、搜索和分析海量数据。就连维基百科、Stack Overflow、

tf.split()函数解析

API原型(TensorFlow 1.8.0): tf.split(     value,     num_or_size_splits,     axis=0,     num=None,     name='split' ) 这个函数是用来切割张量的。输入切割的张量和参数,返回切割的结果。  value传入的就是需要切割的张量。  这个函数有两种切割的方式: 以三个维度的张量为例,比如说一