c++ string操作


#include <iostream>
#include <string>

using namespace std;


int main()
{
  string str1("hello");
  string str2(" study c++");

  string::iterator str_iter = str1.begin();
  str1.insert(str_iter,'a');
  cout << str1 << endl;

  str1.insert(str_iter,3,'b');
  cout << str1<< endl;

  string::iterator str1_iter1 = str1.begin();
  string::iterator str2_iter1 = str2.begin();
  string::iterator str2_iter2 = str2.end();
  
  str1.insert(str1_iter1,str2_iter1,str2_iter2);
  cout << str1 << endl;

  str1 = "hello";
  str1.assign(str2);
  cout << str1 << endl;

  str1.assign(8,'K');
  cout << str1 << endl;

  str1 = "abcdef";
  cout << str1 << endl;
  string::iterator str1_iter2 = str1.begin();
  str1_iter2++;
  str1.erase(str1_iter2);
  cout << str1<< endl;
  
  string::iterator str1_iter3 = str1.end();
  str1_iter3--;
  str1_iter2++;
  str1_iter2++;

  str1.erase(str1_iter2,str1_iter3);
  cout << str1 << endl;

  str1.insert(0, 3, 'K');
  cout << str1 << endl;

  str1 = "hello";
  str1.insert(5, str2);
  cout << str1 << endl;

  system("pause");
  return 0;
}

=================================================

ahello
bbbahello
study c++bbbahello
study c++
KKKKKKKK
abcdef
acdef
acdf
KKKacdf
hello study c++
請按任意鍵繼續. . .

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM