<cstring> append() 详解及其扩展(int, char):
#include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<cstdio> using namespace std; int main() { cout<<"第三: 字符串的添加与复制 append();\nstring d(a);\ncout<<d<<endl;\n//或者 d=d+a;/nd.append(b);\n"; cout<<"1.在d的末尾添加字符串a\n\n"; string d(a); d.append(b); cout<<d<<endl<<endl; cout<<"2.在d的末尾添加字符串/nb(0位置开始,2个长度)的数据\n\n"; cout<<"d.append(b,0,2);\ncout<<d<<endl;\n"; d.append(b,0,2); cout<<d<<endl<<endl; cout<<"3.添加4个 ~ 字符\n\n"; cout<<"d.append(4,'~');\n"; cout<<"cout<<d<<endl<<endl;\n"; d.append(4,'~') ; cout<<d<<endl<<endl; system("pause"); system("cls"); cout<<"4. int 与 char 型添加 (好高兴自己想到的int ^_^ )\n"; cout<<"char app[100]=\"aaabbb\";"; cout<<"\nstring charr(\"-_-\");\n"; cout<<"charr.append(app);\ncout<<charr<<endl\n"; char app[100]="aaabbb"; string charr("-_- "); charr.append(app); cout<<charr<<endl<<endl; cout<<"charr.append(app,4);\ncout<<charr<<endl<<endl;\n"; charr.append(app,4); cout<<charr<<endl<<endl; cout<<"char型数组全部,char型数组的前4个\n\n****如果要添加中间***\n"; cout<<"string tmp;\nstring tmp;tmp.assign(app);\ncharr.assign(\"\");\ncharr.append(tmp,1,4);\ncout<<charr<<endl<<endl;\n"; string tmp; tmp.assign(app); charr.assign(""); charr.append(tmp,1,4); cout<<charr<<endl<<endl; cout<<"5.int double 等等 通过 sprintf() <cstdio>作为转接\n"; cout<<"int aaa=15314;\ndouble bbb=3.1415;\nchar aa[10];\nsprintf(aa,\"%d\",aaa);\ncharr.append(aa,0,4);\nsprintf(aa,\"%f\",bbb);\ncharr.append(aa,0,4);\ncout<<charr<<endl;\n"; int aaa=15314; double bbb=3.1415; char aa[10]; sprintf(aa,"%d",aaa); charr.append(aa,0,4); sprintf(aa,"%f",bbb); charr.append(aa,0,4); cout<<charr<<endl<<endl; system("pause"); system("cls"); return 0; }