永发信息网

replace函数C++应该怎么用呢?

答案:3  悬赏:60  手机版
解决时间 2021-01-12 15:40
replace函数C++应该怎么用呢?
最佳答案
basic_string::max_size

返回string 能放的最大元素个数。(不同于capacity)

size _ type max _ size( ) const;

basic_string ::size_type cap, max;

cap = s.capacity ( );

max = s.max_size ( ); // max=4294967294.

basic_string::rfind

寻找给定的string。返回找到的第一个string 下标值;如果没找到则返回npos。

与find 不同的是:rfind 默认从npos 开始找。其他相同。

basic_string::replace

将原string 中的元素或子串替换。返回替换后的string。

(1)用string 或C-string 代替操作string 中从 _Pos1 开始的 _Num1 个字符

basic _ string& replace( size _ type _Pos1 ,size _ type _Num1 , const value _ type* _Ptr );

basic _ string& replace(size _ type _Pos1 ,size _ type _Num1 ,const basic _ string _Str );

Code

(2)用string 中从 _Pos2 开始的 _Num2 个字符,代替操作string 中从 _Pos1 开始的 _Num1 个字符

用C-string 中的 _Num2 个字符,代替操作string 中从 _Pos1 开始的 _Num1 个字符

basic _ string& replace( size _ type _Pos1 , size _ type _Num1 , const basic _ string& _Str ,

size _ type _Pos2 , size _ type );

basic _ string& replace( size _ type _Pos1 , size _ type _Num1 ,

const value _ type* _Ptr , size _ type _Num2 );

Code

(3)用 _Count 个character _Ch , 代替操作string 中从 _Pos1 开始的 _Num1 个字符

basic _ string& replace( size _ type _Pos1 , size _ type _Num1 ,

size _ type _Count , value _ type _Ch );

string result;

string s ( "AAAAAAAA" );

char ch = 'C';

result = s.replace ( 1 , 3 , 4 , ch ); // s= "ACCCCAAAA"

(4)用string 或C-string ,代替操作string 中从 First0 到 Last0 的字符

basic _ string&replace(iterator First0 ,iterator Last0 , const basic _ string& _Str );

basic _ string&replace(iterator First0 ,iterator _Last0 , const value _ type* _Ptr );

Code

(5)用string 中从 _Pos2 开始的 _Num2 个字符,代替操作string 中从 First0 到 Last0 的字符

用C-string 中的 _Num2 个字符,代替操作string 中从 First0 到 Last0 的字符

basic _ string& replace( iterator _First0 , iterator _Last0 ,

const value _ type* _Ptr , size _ type _Num2 );

template basic _ string& replace(

iterator _First0 , iterator _Last0 ,

InputIterator _First , InputIterator _Last );

IterF3 = s.begin ( ) + 1; IterL3 = s.begin ( ) + 3;

IterF4 = s.begin ( ); IterL4 = s.begin ( ) + 2;

a = s.replace ( IterF3 , IterL3 , IterF4 , IterL4 );

b = s.replace ( IterF1 , IterL1 , cs5p , 4 );

(6)用 _Count 个character _Ch , 代替操作string 中从 First0 到 Last0 的字符

basic _ string& replace( iterator _First0 , iterator _Last0 ,

size _ type _Count , value _ type _Ch );

a = s.replace ( IterF2 , IterL2 , 4 , ch );

basic_string::swap

交换两个string。

void swap( basic _ string& _Str );

s1.swap ( s2 );

basic_string::substr

返回从 _Off ( 下标)开始的 _Count 个字符组成的string

basic _ string substr( size _ type _Off = 0, size _ type _Count = npos ) const;

string s("I love you!") ,sub;

sub=s.substr( ); // sub= "I love you!"

sub=s.substr(1); // sub= " love you!"

sub=s.substr(3,4); // sub= "ove"
全部回答
试试这个:
str.replace(str.begin(), str.end(), str.rbegin(), str.rend());
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
黄河牌电视机为何破产
问一下交行有4055128开头的卡号码?
求助:maple里怎样做曲线拟合
悠悠球光子精灵s光子精灵超级光子精灵有什么
一张蓝图绘到底是什么时候提出来的
你对男朋友有什么要求?
上海圆什实业有限公司怎么样?
家拌凉菜的做法,正宗东北家拌凉菜怎么做好
小型人造飞机怎么造
顺德十级工伤可以赔多少钱?
男朋友宁愿玩游戏也不理我。为什么
国考申论和省考申论有哪些区别
cos圈的潜规则是什么
如何激活苹果7p imessage 和facetime?
万华园丽景华都小区属哪个学区房
推荐资讯
电动车锂电池48v12ah一般多钱 保用两年吗可以
劲浪网络地址在什么地方,我要处理点事!
吃什么东西补充渃铵酸梅生成
为什么qq空间老显示未登录?
VVT-i代表这什么
能发下迅雷下载]哈利波特3BD1024高清国语配音
两家共用一墙,一家盖房时,靠共用墙独立再立
概括明清时期棉纺织业发展的特点和原因
350公顷。是多少平方千米
对于爽约的人,你们都是如何对待的
4欧50w喇叭跟8欧50w喇叭哪个更好?
pb 怎样调用Cxf webservice
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?