博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
string那些事之replace
阅读量:7057 次
发布时间:2019-06-28

本文共 3598 字,大约阅读时间需要 11 分钟。

/*用法一:用str替换指定字符串从起始位置pos开始 长度为为len的字符串string &replace(size_t pos, size_t len, const string& str)*/#include 
using namespace std;typedef long long ll;const double eps = 1e-7;const int maxn = 5e5 + 5;const double pi = acos(-1.0);int main(){ string s="this is@ a test string!"; s=s.replace(s.find("@"),1,""); cout<
<

/*用法二:用str替换 迭代器 起始位置 和 结束位置的字符串string &replace(const_iterator i1, const_iterator i2, const string& str)*/#include 
using namespace std;typedef long long ll;const double eps = 1e-7;const int maxn = 5e5 + 5;const double pi = acos(-1.0);int main(){ string s="this is@ a@ test string!"; s=s.replace(s.begin(),s.begin()+6,""); cout<
<

/*用法三:用substr的指定子串(给定起始位置和长度)替换从指定位置上的字符串string &replace(size_t pos, size_t len, const string& str, size_t subpos, size_t sublen)*/#include 
using namespace std;typedef long long ll;const double eps = 1e-7;const int maxn = 5e5 + 5;const double pi = acos(-1.0);int main(){ string s="this is@ a@ test string!"; string str="12345"; s=s.replace(0,5,str,str.find("1"),3); //用str的指定子串(从1位置数共3个字符)替换从0到5位置上的s cout<
<

/*用法四:**string转char*时编译器可能会报出警告,不建议这样做**用str替换从指定位置0开始长度为5的字符串string &replace(size_t pos, size_t len, const char*s)*/#include 
using namespace std;typedef long long ll;const double eps = 1e-7;const int maxn = 5e5 + 5;const double pi = acos(-1.0);int main(){ string s="this is@ a@ test string!"; char* str="12345"; s=s.replace(0,5,str); //用str替换从指定位置0开始长度为5的字符串 cout<
<

/*用法五:**string转char*时编译器可能会报出警告,不建议这样做**用str替换从指定迭代器位置的字符串string &replace(const_iterator i1, const_iterator i2, const char* s)*/#include 
using namespace std;typedef long long ll;const double eps = 1e-7;const int maxn = 5e5 + 5;const double pi = acos(-1.0);int main(){ string s="this is@ a@ test string!"; char* str="12345"; s=s.replace(s.begin(),s.begin()+9,str); //用str替换从指定迭代器位置的字符串 cout<
<

/*用法六:**string转char*时编译器可能会报出警告,不建议这样做**用s的前n个字符替换从开始位置pos长度为len的字符串string& replace(size_t pos, size_t len, const char* s, size_t n)*/#include 
using namespace std;typedef long long ll;const double eps = 1e-7;const int maxn = 5e5 + 5;const double pi = acos(-1.0);int main(){ string s="this is@ a@ test string!"; char* str="12345"; s=s.replace(0,9,str,4); //用str的前4个字符替换从0位置开始长度为9的字符串 cout<
<

/*用法七:string转char*时编译器可能会报出警告,不建议这样做   用s的前n个字符替换指定迭代器位置(从i1到i2)的字符串   string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n);   */    int main()    {        string line = "this@ is@ a test string!";        char* str = "12345";        line = line.replace(line.begin(), line.begin()+9, str, 4);  //用str的前4个字符替换指定迭代器位置的字符串        cout << line << endl;           return 0;    }

/*用法八:用重复n次的c字符替换从指定位置pos长度为len的内容string &replace(size_t pos, size_t len, size_t, char c)*/#include 
using namespace std;typedef long long ll;const double eps = 1e-7;const int maxn = 5e5 + 5;const double pi = acos(-1.0);int main(){ string s="this is@ a@ test string!"; char c='z'; s=s.replace(0,9,3,c); //用重复3次的c字符替换从指定位置0长度为9的内容 cout<
<

/*用法九:用重复n次的c字符替换从指定迭代器位置的内容string &replace(const_iterator i1, const_iterator i2,  size_t, char c)*/#include 
using namespace std;typedef long long ll;const double eps = 1e-7;const int maxn = 5e5 + 5;const double pi = acos(-1.0);int main(){ string s="this is@ a@ test string!"; char c='z'; s=s.replace(s.begin(),s.begin()+9,3,c); //用重复3次的c字符替换从指定位置0长度为9的内容 cout<
<

转载于:https://www.cnblogs.com/Roni-i/p/8997283.html

你可能感兴趣的文章
day14-css之内外边距
查看>>
Oracle实现自增方式:序列+触发器
查看>>
[难过]小明住院了
查看>>
开源项目大全 >> ...
查看>>
LINUX信号-sigaction更强壮的信号注册函数
查看>>
你还在用xshell 啊呜特了 xshell替代产品finalshell横空出世
查看>>
[转]SSL 与 数字证书 的基本概念和工作原理
查看>>
[转载] Redis
查看>>
[LeetCode]:237:Delete Node in a Linked List
查看>>
PHP编译安装时常见错误及解决办法,大全
查看>>
获取动态创建的元素触发点击事件
查看>>
策略模式
查看>>
Git Cmd
查看>>
Software Tesing HW2
查看>>
收益率
查看>>
2. 更多标准模块
查看>>
Python中文处理(转)
查看>>
Cocos2d-x之的动作混合
查看>>
linux安装Django 以及 生产环境部署实现高并发
查看>>
jQuery.holdReady()方法用法实例
查看>>