目录replace算法:用法一:用str替换指定字符串从起始位置pos开始长度为len的字符用法二: 用str替换 迭代器起始位置 和 结束位置 的字符用法三: 用substr的指定
replace函数包含于头文件#include<string>中。
泛型算法replace把队列中与给定值相等的所有值替换为另一个值,整个队列都被扫描,即此算法的各个版本都在
线性时间内执行———其复杂度为O(n)。
即replace的执行要遍历由区间[frist,last)限定的整个队列,以把old_value替换成new_value。
下面说下replace()的九种用法:(编译软件dev5.4.0)
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;
}
结果如下:
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;
}
结果如下:
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;
}
结果如下:
用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;
}
结果如下:
用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;
}
结果如下:
用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;
}
结果如下:
用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;
}
结果如下:
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;
}
结果如下:
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/158639.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0