广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C++中replace()函数怎么使用
  • 925
分享到

C++中replace()函数怎么使用

2023-06-21 21:06:33 925人浏览 安东尼
摘要

这篇文章主要介绍“c++中replace()函数怎么使用”,在日常操作中,相信很多人在C++中replace()函数怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++中replace()函数怎么使用

这篇文章主要介绍“c++中replace()函数怎么使用”,在日常操作中,相信很多人在C++中replace()函数怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++中replace()函数怎么使用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

replace算法

replace函数包含于头文件#include<string>中。

泛型算法replace把队列中与给定值相等的所有值替换为另一个值,整个队列都被扫描,即此算法的各个版本都在

线性时间内执行———其复杂度为O(n)。

即replace的执行要遍历由区间[frist,last)限定的整个队列,以把old_value替换成new_value。

下面说下replace()的九种用法:(编译软件dev5.4.0)

用法一:用str替换指定字符串从起始位置pos开始长度为len的字符

string& replace (size_t pos, size_t len, const string& str); 

代码如下:

#include<iOStream>#include<string>using namespace std;int main(){string str = "he is@ a@ Good boy";str=str.replace(str.find("a"),2,"#");  //从第一个a位置开始的两个字符替换成#cout<<str<<endl; return 0;}

结果如下:

C++中replace()函数怎么使用

用法二: 用str替换 迭代器起始位置 和 结束位置 的字符

string& replace (const_iterator i1, const_iterator i2, const string& str);

#include<iostream>#include<string>using namespace std;int main(){string str = "he is@ a@ good boy"; str=str.replace(str.begin(),str.begin()+5,"#"); //用#替换从begin位置开始的5个字符 cout<<str<<endl; return 0; }

结果如下:

C++中replace()函数怎么使用

用法三: 用substr的指定子串(给定起始位置和长度)替换从指定位置上的字符串 

string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);

#include<iostream>#include<string>using namespace std;int main(){string str = "he is@ a@ good boy"; str=str.replace(str.begin(),str.begin()+5,"#"); //用#替换从begin位置开始的5个字符 cout<<str<<endl; return 0; }

结果如下:

C++中replace()函数怎么使用

用法四:string转char*时编译器可能会报出警告,不建议这样做

用str替换从指定位置0开始长度为5的字符串               

string& replace(size_t pos, size_t len, const char* s);

代码如下:

#include<iostream>#include<string>using namespace std;int main(){string str = "he is@ a@ good boy";char * str1 = "12345";str=str.replace(0,5,str1);   //用str替换从指定位置开始长度为5的字符串cout<<str<<endl;return 0; }

结果如下:

C++中replace()函数怎么使用

用法五:string转char*时编译器可能会报出警告,不建议这样做

用str替换从指定迭代器位置的字符串               

string& replace (const_iterator i1, const_iterator i2, const char* s); 

代码如下:

#include<iostream>#include<string>using namespace std;int main(){string str = "he is@ a@ good boy";char * str1 = "12345";str=str.replace(str.begin(),str.begin()+6,str1);   //用str替换从指定迭代器位置的字符串cout<<str<<endl;return 0; }

结果如下:

C++中replace()函数怎么使用

用法六:string转char*时编译器可能会报出警告,不建议这样做

用s的前n个字符替换从开始位置pos长度为len的字符串                 

string& replace(size_t pos, size_t len, const char* s, size_t n);  

代码如下:

#include<iostream>#include<string>using namespace std;int main(){string str = "he is@ a@ good boy";char * str1 = "12345";str=str.replace(0,6,str1,4);   //用str1的前4个字符串替换从位置0~6的字符串cout<<str<<endl;return 0; }

结果如下:

C++中replace()函数怎么使用

用法七:string转char*时编译器可能会报出警告,不建议这样做

用s的前n个字符替换指定迭代器位置(从i1到i2)的字符串

 string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n); 

代码如下:

#include<iostream>#include<string>using namespace std;int main(){string str = "he is@ a@ good boy";char * str1 = "12345";str = str.replace(str.begin(),str.begin()+6,str1,4);   //用str1的前4个字符串替换从位置0~6的字符串cout<<str<<endl;return 0; }

结果如下:

C++中replace()函数怎么使用

用法八: 用重复n次的c字符替换从指定位置pos长度为len的内容

string& replace (size_t pos, size_t len, size_t n, char c); 

代码如下:

#include<iostream>#include<string>using namespace std;int main(){string str = "he is@ a@ good boy";char  str1 = '#';str = str.replace(0,6,3,str1);   //用重复3次的str1字符替换的替换从位置0~6的字符串cout<<str<<endl;return 0; }

结果如下:

C++中replace()函数怎么使用

用法九: 用重复n次的c字符替换从指定迭代器位置(从i1开始到结束)的内容 

string& replace (const_iterator i1, const_iterator i2, size_t n, char c); 

代码如下:

#include<iostream>#include<string>using namespace std;int main(){string str = "he is@ a@ good boy";char  str1 = '#';str = str.replace(str.begin(),str.begin()+6,3,str1);   //用重复3次的str1字符替换的替换从指定迭代器位置的内容 cout<<str<<endl;return 0; }

结果如下:

C++中replace()函数怎么使用

到此,关于“C++中replace()函数怎么使用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!

--结束END--

本文标题: C++中replace()函数怎么使用

本文链接: https://www.lsjlt.com/news/300724.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C++中replace()函数怎么使用
    这篇文章主要介绍“C++中replace()函数怎么使用”,在日常操作中,相信很多人在C++中replace()函数怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++中replace()函数怎么使用...
    99+
    2023-06-21
  • mybatis replace函数怎么使用
    MyBatis中的replace函数用于在字符串中替换指定的字符或字符串。下面是使用replace函数的示例:1. 在SQ...
    99+
    2023-09-14
    mybatis replace
  • sqlserver replace函数怎么使用
    在 SQL Server 中,REPLACE 函数用于替换字符串中的一部分内容。它的语法如下:```REPLACE(string, ...
    99+
    2023-09-22
    sqlserver
  • javascript中的replace函数怎么使用
    在JavaScript中,可以使用`replace()`函数来替换字符串中的内容。`replace()`函数接受两个参数:要替换的内...
    99+
    2023-08-18
    javascript replace
  • Oracle中的replace函数怎么使用
    在Oracle中,REPLACE函数用于将字符串中的指定子字符串替换为另一个子字符串。它的语法如下:REPLACE(orig...
    99+
    2023-08-17
    Oracle replace
  • C++中replace()函数的作用是什么
    今天就跟大家聊聊有关C++中replace()函数的作用是什么,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。C++编程语言中的string应用方式多样化,每一种应用方式都能帮助我们提...
    99+
    2023-06-17
  • replace函数在mysql怎么使用
    replace函数作用:mysql中replace函数的作用是对数据表中字段的字符进行替换。replace函数语法:update `table_name` SET `field_name` = replace (`field_name`,&...
    99+
    2022-10-22
  • string的replace函数怎么使用
    string的replace函数可以通过以下方式使用:```pythonstring.replace(old, new, count...
    99+
    2023-09-22
    string
  • C++中replace() 函数的基本用法
    目录replace算法:用法一:用str替换指定字符串从起始位置pos开始长度为len的字符用法二: 用str替换 迭代器起始位置 和 结束位置 的字符用法三: 用substr的指定...
    99+
    2022-11-12
  • java replace函数怎么用
    在Java中,replace()函数用于替换字符串中的指定字符或字符序列。它有两种重载形式: 替换字符:String replac...
    99+
    2023-10-27
    java
  • translate与replace函数怎么在Oracle中使用
    这篇文章给大家介绍translate与replace函数怎么在Oracle中使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。translate函数语法:translate(...
    99+
    2022-10-18
  • MySQL中replace函数如何使用
    这篇文章给大家介绍MySQL中replace函数如何使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。  MySQLreplace函数的用法有什么  比如你要将表tb1里面的...
    99+
    2022-10-18
  • MySQL中如何使用REPLACE()函数
    这篇文章将为大家详细讲解有关MySQL中如何使用REPLACE()函数,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。REPLACE()REPLACE(str,fr...
    99+
    2022-10-19
  • python中Replace函数如何使用
    Python中的replace函数用于将字符串中的指定子字符串替换为另一个字符串。基本语法如下:```pythonstring.re...
    99+
    2023-09-07
    python Replace
  • Sql Server中REPLACE函数的使用
    在SQL Server中,REPLACE函数用于替换字符串中的指定字符或子字符串。REPLACE函数的语法如下:REPLAC...
    99+
    2023-08-15
    Sql Server
  • oracle中replace函数如何使用
    在Oracle中,`REPLACE`函数的语法如下:```sqlREPLACE(string, search_string,...
    99+
    2023-10-07
    oracle
  • Sql Server中REPLACE函数如何使用
    SQL Server中的REPLACE函数用于在一个字符串中替换指定的字符或字符串。REPLACE函数的语法如下:REPLACE (...
    99+
    2023-08-15
    Sql Server REPLACE
  • Sql Server中如何使用REPLACE函数
    今天就跟大家聊聊有关Sql Server中如何使用REPLACE函数,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。REPLACE用第三个表达式替换第...
    99+
    2022-10-18
  • replace与replace into怎么在Mysql数据库中使用
    本篇文章为大家展示了replace与replace into怎么在Mysql数据库中使用,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。Mysql repl...
    99+
    2022-10-18
  • mysql中replace函数有什么作用
    这篇文章主要讲解了“mysql中replace函数有什么作用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“mysql中replace函数有什么作用”吧!说明可以替换字符串中的内容,...
    99+
    2023-06-20
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作