C++String模板类学习笔记

in[1]:
#include<iostream>
#include<string>
using namespace std;
int main(){
  //...
}
in[2]:
string s1;
s1 = "abcd";
string s2(s1);
cout<<s2<<endl;
  • abcd
in[3]:
string s3(s1,1);
cout<<s3<<endl;
string s4(s1,1,2);
cout<<s4<<endl;
  • bcd
  • bc
in[4]:
string s5(4,'c');
cout<<s5<<endl;
string s6('d','j');
cout<<s6<<endl;
  • cccc
  • jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
in[5]:
char ch[] = "jackeylove";
string s7(ch);
cout<<s7<<endl;
string s8(ch,6);
cout<<s8<<endl;
  • jackeylove
  • jackey
in[6]:
string s9 = s8;
cout<<s9<<endl;
string s10;
s10.assign(s7);
cout<<s10<<endl;
s10.swap(s9);
cout<<s10<<endl;
  • jackey
  • jackeylove
  • jackey
in[7]:
cout<<s10.empty()<<endl;
cout<<s10.length()<<endl;
cout<<s10.size()<<endl;
cout<<s10.max_size()<<endl;
cout<<s10.capacity()<<endl;
  • 0
  • 6
  • 6
  • 4611686018427387903
  • 15
in[8]:
cout<<s10[4]<<' '<<s10.at(4)<<endl;
cout<<s10.compare("jackeylove")<<endl;
cout<<s10.compare("xyz")<<endl;
s10.clear();
cout<<s10<<endl;
  • e e
  • -4
  • -923671
in[9]:
string s11("369nb!");
cout<<s11<<endl;
s11+="ok,ok...";
cout<<s11<<endl;
s11.append("1 ge sai ji");
cout<<s11<<endl;
s11.push_back('.');
cout<<s11<<endl;
  • 369nb!
  • 369nb!ok,ok…
  • 369nb!ok,ok…1 ge sai ji
  • 369nb!ok,ok…1 ge sai ji.
in[10]:
cout<<string::npos<<endl;
string s12;
s12.assign("hello");
cout<<s12<<" size:"<<s12.size()<<endl;
s12.assign("hello",1);
cout<<s12<<" size:"<<s12.size()<<endl;
s12.assign("hello",1,3);
cout<<s12<<" size:"<<s12.size()<<endl;
s12.assign("hello",1,string::npos);
cout<<s12<<" size:"<<s12.size()<<endl;
s12.assign("hello",6);
cout<<s12<<" size:"<<s12.size()<<endl;
s12.assign(5,'x');
cout<<s12<<" size:"<<s12.size()<<endl;
  • 18446744073709551615
  • hello size:5
  • h size:1
  • ell size:3
  • ello size:4
  • hello size:6
  • xxxxx size:5
in[11]:
string s13("xiushennb,fule!");
cout<<s13.substr()<<endl;
cout<<s13.substr(10)<<endl;
cout<<s13.substr(10,4)<<endl;
  • xiushennb,fule!
  • fule!
  • fule
in[12]:
string s14("woshinidie,hhhnidieshiwo,shagou");
cout<<s14.size()<<endl;
cout<<s14.find("nidie")<<endl;
cout<<s14.rfind("nidie")<<endl;
cout<<s14.find("nima")<<endl;
cout<<s14.find_first_of("abcde")<<endl;
cout<<s14.find_last_of("abcde")<<endl;
cout<<s14.find_first_not_of("abcde")<<endl;
cout<<s14.find_last_not_of("abcde")<<endl;
  • 31
  • 5
  • 14
  • 18446744073709551615
  • 7
  • 27
  • 0
  • 30
in[13]:
string s15("hh,,,,,hh");
string::iterator it=s15.begin()+3;
s15.insert(it,'a');
cout<<s15<<endl;
s15.insert(it,2,'b');
cout<<s15<<endl;
  • hh,a,hh
  • hh,bba,hh
in[14]:
string s16 = "yyds";
string s17("uzi,!");
s17.insert(4,s16,0,4);
//(p0,const c_str);(p0,const c_str,n);
//(p0,const str);(p0,const str,pos,n);
//(p0,n,char);
cout<<s17<<endl;
  • uzi,yyds!
in[15]:
string s18("the shy!tian shen xia fan!");
cout<<s18<<endl;
string::iterator it_s18 = s18.begin();
s18.erase(it_s18);
cout<<s18<<endl;
it_s18++;
s18.erase(it_s18,it_s18+5);
cout<<s18<<endl;
s18.erase(5,4);
cout<<s18<<endl;
  • the shy!tian shen xia fan!
  • he shy!tian shen xia fan!
  • h!tian shen xia fan!
  • h!tiaen xia fan!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值