关于string类型的加法操作

string类型加法为连接字符串生成新的string类型。但是加法操作的左右操作数必须有一个是string类型,不然会产生编译错误

string str2="myword";

string str1=str2+"hellow";

string str3=str1+"bill"+str2;

string str4=""hellow"+"world"+str2;//this is wrong;

对于stl算法也是一样的比如

string str=accumulate(st2.begin(),str2.end(),"hellow");//编译时错误,因为第三个参数是字符串字面值,会导致累加类型都为const char* 类型,违背string类型加法操作

string str=accumulate(str2.begin(),str2.end(),string("hellow"));//编译正确

 

posted on 2015-11-19 14:52  菜鸟基地  阅读(2785)  评论(0)    收藏  举报

导航