標題:
replace() 函式
[打印本頁]
作者:
tonyh
時間:
2022-12-15 19:39
標題:
replace() 函式
本帖最後由 tonyh 於 2022-12-15 20:31 編輯
試以 replace() 函式,將字串中的目標對象以特定字串取代。
<string> 標頭檔與 <algorithm> 標頭檔皆有提供 replace() 函式,但其用法與效果略有不同。
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str1="honolulu";
//cout<<str1.replace(5,1,"a")<<endl; //honolalu
str1[5]='a';
cout<<str1<<endl;
string str2="honolulu";
//cout<<str2.replace(str2.find("u"),1,"a")<<endl; //honolalu
int index=str2.find('u');
str2[index]='a';
cout<<str2<<endl;
string str3="honolulu";
//replace(str3.begin(),str3.end(),'u','a');
for(int i=0; i<str3.size(); i++)
if(str3[i]=='u')
str3[i]='a';
cout<<str3<<endl; //honolala
return 0;
}
複製代碼
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str1="honolulu";
cout<<str1.replace(5,1,"a")<<endl; //honolalu
string str2="honolulu";
cout<<str2.replace(str2.find("u"),1,"a")<<endl; //honolalu
string str3="honolulu";
replace(str3.begin(),str3.end(),'u','a');
cout<<str3<<endl; //honolala
system("pause");
return 0;
}
複製代碼
作者:
黃宥華
時間:
2022-12-15 20:36
#include<bits/stdc++.h>
using namespace std;
int main()
{
cin.tie(0);
cin.sync_with_stdio(0);
string str1="honolulu";
str1[5]='a';
cout<<str1<<endl;
string str2="honolulu";
int index=str2.find('u');
str2[index]='a';
cout<<str2<<endl;
string str3="hahahaha";
replace(str3.begin(),str3.end(),'a','o');
cout<<str3;
return 0;
}
複製代碼
作者:
黃宇綸
時間:
2022-12-15 20:44
#include<bits/stdc++.h>
using namespace std;
#define ll long long
//#define int ll
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,(n)+1)
#define RREP(i,n) for(int i=(n)-1;i>=0;i--)
#define f first
#define s second
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)(x.size())
#define SQ(x) (x)*(x)
#define pii pair<int,int>
#define Graph vector<vector<int>>
#define IOS() cin.sync_with_stdio(0),cin.tie(0),cout.tie(0)
const ll inf=(1ll<<63)-1;
const int maxn=1e5+5;
const ll mod=1e9+7;
string s="0123456789";
int n;
stringstream ss;
int an=0;
signed main()
{
IOS();
cout<<s.replace(0,5,"abc")<<"\n";//abc56789 (p,l,s):[p ~ p+l-1]=s
s="0123456789";
replace(ALL(s),'1','a'); cout<<s<<"\n";//0a123456789 (l,r,c1,c2):[l~r] (c1->c2)
return 0;
}
複製代碼
作者:
黃宇瑄
時間:
2022-12-15 20:49
#include<bits/stdc++.h>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str1="honolulu";
cout<<str1.replace(5,1,"a")<<endl;
string str2="honolulu";
cout<<str2.replace(str2.find("u"),1,"a")<<endl;
string str3="honolulu";
replace(str3.begin(),str3.end(),'u','a');
cout<<str3<<endl;
return 0;
}
複製代碼
作者:
李沛昂
時間:
2022-12-15 20:52
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str1="honolulu";
str1[5]='a';
cout<<str1<<endl;
string str2="honolulu";
int index=str2.find('u');
str2[index]='a';
cout<<str2<<endl;
string str3="honolulu";
for(int i=0; i<str3.size(); i++)
if(str3[i]=='u')
str3[i]='a';
cout<<str3<<endl;
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2