首页 > 编程语言 > 详细

c++ string操作

时间:2019-06-09 21:38:18      阅读:136      评论:0      收藏:0      [点我收藏+]

#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++
请按任意键继续. . .

 

技术分享图片

c++ string操作

原文:https://www.cnblogs.com/herd/p/10994597.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!