<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; }
<cstring> assign() 、 copy() 详解:
#include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<cstdio> using namespace std; int main() { cout<<"第二: 字符串的赋值 assign();"<<endl; cout<<"1.感觉就像是append不过是抹除-覆盖\n"; cout<<"string e;\nchar f[10]=\"123456\"\ne.assign(f);\ne+=' ';\ncout<<e<<endl<<endl;\n"; string e; char f[10]="123456"; e.assign(f); e+=' '; cout<<e<<endl<<endl; cout<<"2.string区间 赋值都类似吧\n"; cout<<"e.assign(f,3,3);\ne+=' ';\ncout<<e<<endl<<endl;\ne.assign(f,3);\ncout<<e<<endl;\n"; e.assign(f,3,3); e+=' '; cout<<e<<endl; e.assign(f,3); cout<<e<<endl<<endl; cout<<"3.某字符串char型 全部\n"; cout<<"char ssr[10]=\"asdqwezxc\";\ne.assign(ssr);\ncout<<ssr<<endl;\n"; char ssr[10]="asdqwezxc"; e.assign(ssr); cout<<ssr<<endl<<endl; cout<<"4.某字符串char型 前num个\n"; cout<<"e.assign(ssr,4);\ncout<<e<<endl;\n"; e.assign(ssr,4); cout<<e<<endl<<endl; cout<<"5.某字符赋值\n"; cout<<"赋值3个6\n"; e.assign(3,'6'); cout<<e<<endl<<endl; cout<<"copy() 将d中的2位置开始的12个字符覆盖到char型数组ss上\n 必须为-> char型 <-否则报错"; cout<<" char ss[10]=\"123\";\n string dd;\nd.copy(ss,12,2);\ncout<<ss<<endl;\n"; char ss[15]="123"; string dd("abcdefghijklmn"); dd.copy(ss,12,2); cout<<ss<<endl<<endl; system("pause"); system("cls"); return 0; }
<cstring> 创建详解
#include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<cstdio> using namespace std; int main() { //1. 字符串的创建 cout<<"第一:字符串的创建!\n\n"; string a(4,'a'); cout<<"1.以 a 为原字符 4单位大小\n\n"; cout<<"string a(4,'a');\ncout<<a<<endl;\n"; cout<<a<<endl<<endl; cout<<"2.任意大小的字符串\n\n"; cout<<"string b(\"bbbbbb\");\ncout<<b<<endl;\n"; string b("bbbbbb"); cout<<b<<endl<<endl; cout<<"3.把某一字符串的某一部分\n(0位置开始4个长度)给c\n\n"; cout<<"string c(a,0,4) ;\ncout<<c<<endl;\n"; string c(a,0,4) ; cout<<c<<endl<<endl; cout<<"4. 10长度原长度不足补*;\n\n"; cout<<"c.resize(10,'*');\ncout<<c<<endl;\n"; c.resize(10,'*'); cout<<c<<endl<<endl; system("pause"); system("cls"); return 0; }
Vs2017 使用 strcpy 的时候会报错,提示 strcpy 是不安全的,需要用 strcpy_s 代替。
#include "pch.h" #include <iostream> #include <cstring> using namespace std; int main() { char str1[11] = "hello"; char str2[12] = "world"; char str3[11]; int len; //复制str1到str2 strcpy_s(str3, str1); cout << "strcpy(str3, str1):" << str3 << endl; return 0; }
关于字符数组为什么可以以数组名来用cout输出数组内容,而普通数组不行。
先上范例:
int a[10] = {1,2,3,6,7}; char b[6] = {'h','a','p','p','y','\0'}; char c[] = "happy"; cout<<a<<endl; cout<<b<<endl; cout<<c<<endl;
输出结果为:
0x22fe6c happy happy
从以上范例可以看出,普通数组中以数组名用cout来输出,只会得到一串地址;用字符数组则会输出数组中的内容。
那为什么会这样呢?
答案:因为 char 型数组中的每一个元素都是一字节,所以每一个字符之间的地址都是 +1 的是连续的,所以当 cout 输出时读到字符数组中的 \0 便停止输出; 而 int 数组每个元素占 4 个字节所以数个数组中每个元素地址的间隔是 4,但其实它也是连续的,出现乱码是因没找到结束符。
感谢您的支持,我会继续努力的!
支付宝扫一扫,即可进行扫码打赏哦
1909C++ 字符串
<cstring> append() 详解及其扩展(int, char):
1908C++ 字符串
<cstring> assign() 、 copy() 详解:
1907C++ 字符串
<cstring> 创建详解
1906C++ 字符串
Vs2017 使用 strcpy 的时候会报错,提示 strcpy 是不安全的,需要用 strcpy_s 代替。
1905C++ 字符串
关于字符数组为什么可以以数组名来用cout输出数组内容,而普通数组不行。
先上范例:
输出结果为:
从以上范例可以看出,普通数组中以数组名用cout来输出,只会得到一串地址;用字符数组则会输出数组中的内容。
那为什么会这样呢?
答案:因为 char 型数组中的每一个元素都是一字节,所以每一个字符之间的地址都是 +1 的是连续的,所以当 cout 输出时读到字符数组中的 \0 便停止输出; 而 int 数组每个元素占 4 个字节所以数个数组中每个元素地址的间隔是 4,但其实它也是连续的,出现乱码是因没找到结束符。