返回列表 發帖
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     string s="Hello World!";
  6.     cout<<s<<endl;
  7.     cout<<"字元'o'的索引位置(由前往後查找):"<<s.find('o')<<endl;
  8.     cout<<"字元'o'的索引位置(由後往前查找):"<<s.rfind('o')<<endl;
  9.     cout<<"字串\"llo\"的索引位置(由前往後查找):"<<s.find("llo")<<endl;
  10.     cout<<"字元'l'的索引位置(由前往後自第4個位置開始查找):"<<s.find('l',4)<<endl;
  11.     int res=s.find('a');
  12.     cout<<"字元'a'的索引位置(由前奘後查找):"<<res<<endl;
  13. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<string>
  3. #include<algorithm>
  4. using namespace std;
  5. int main()
  6. {
  7.     string s1="honolulu";
  8.     cout<<s1.replace(5,1,"a")<<endl;

  9.     string s2="honolulu";
  10.     cout<<s2.replace(s2.find("u"),1,"a")<<endl;

  11.     string s3="honolulu";
  12.     replace(s3.begin(),s3.end(),'u','a');
  13.     cout<<s3<<endl;
  14. }
複製代碼

TOP

返回列表