C++ string知识总结

前言

关于学习、了解string相关方法的定义及功能,官方参考手册是最好的地方,权威、客观、简明扼要,没有歧义。这是官方文档的地址:C++官方文档

Construct

string string1;	//construct an empty string n
string string2=string1;		
string string3=string(string 2,8,3);	//for (int i = 8,l=0; (i+l)<string2.size()&&l < 3; l++)string3 += string2[i + l];
string string3=("hello and byebye");
string string4=("hello and byebye",5);		string 4=hello.
string string5=(555,'*');	//555 *.
string string6=(string5.begin(),string5.end()--554);		//string6=*.

Iterator

略const与reverse

string5.size()==(string.end()-string.begin());

Capacity

size与length方法的区别:没有区别

empty();	//判空
size();		//容器大小
length();	//
resize();	//重设大小
clear();	//清空容器
reserve();	//设置容积大小

Element access

front();	//略
back();	 	//略
at();		//略

Modify

append方法:

string2.append(string1);
string2.append(string1,2,3);	//for (int i = 2,l=0; (i+l)<string1.size()&&l < 3; l++)string2 += string1[i + l];
string2.append("example");
string2.append("example",5);	
string2.append(int n,char value);//append n value.
string2.append(string::iterator begin,string::iterator end);//append begin~end.

insert方法:

string2.insert(string2.begin(),'c');
string2.insert(string2.begin(),2,'c');
string2.insert(0,'c')
string2.insert(0,"capture");
string2.insert(0,"capture",2);
string2.insert(0,string3,2,5);
string2.insert(string2.begin(),string::iterator it,string::iteratro that);
string2.insert(0,5,',');
//未实现的重载形式:
string2.insert(string2.begin(),string3);
string2.insert(string2.begin(),"error");

replace方法:

std::string base="this is a test string.";
std::string str2="n example";
std::string str3="sample phrase";
std::string str4="useful.";
std::string str=base;           // "this is a test string."
str.replace(9,5,str2);          // "this is an example string." (1)
str.replace(19,6,str3,7,6);     // "this is an example phrase." (2)
str.replace(8,10,"just a");     // "this is just a phrase."     (3)
str.replace(8,6,"a shorty",7);  // "this is a short phrase."    (4)
str.replace(22,1,3,'!');        // "this is a short phrase!!!"  (5)
str.replace(str.begin(),str.end()-3,str3);                    // "sample phrase!!!"      (1)
str.replace(str.begin(),str.begin()+6,"replace");             // "replace phrase!!!"     (3)
str.replace(str.begin()+8,str.begin()+14,"is coolness",7);    // "replace is cool!!!"    (4)
str.replace(str.begin()+12,str.end()-4,4,'o');                // "replace is cooool!!!"  (5)
str.replace(str.begin()+11,str.end(),str4.begin(),str4.end());// "replace is useful."    (6)

erase方法

string2.erase(3,5);	//erase(string2[3]string2[4]string2[5]).
string2.erase(string::iterator begin,string::iterator end);	//erase begin toend.
string2.erase(string.begin());	//==string2.erase(0);

其他

push_back();
pop_back();
swap();
assign();

weiwan

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值