string和char型数组的区别
总的来说,string比char[]更耗时,比如apend()这种函数,而string能完成的操作char[]基本都可以实现。
1.赋值
char赋值:
char ch1[] = 'give me'; char ch2[] = 'a cup'; strcpy(ch1,ch2); cout<<'ch1='<<ch1<<endl;
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
输出结果:ch1=a cup
string赋值:
string str1 = 'give me';
string str2 = 'a cup';
①str1 = str2;
cout<<'str1='<<str1<<endl;
②str1.assign(str2,0,5); // 参数2为起始位置,参数3为字符数
cout<<'str1='<<str1<<endl;
1
2
3
4
5
6
7
1
2
3
4
5
6
7
输出结果:str1=a cup
str1=a cup
2.合并(全部)
char合并(全部):
char ch1[15] = 'give me '; // 注意长度,合并后为13 char ch2[] = 'a cup'; strcat(ch1,ch2); cout<<'ch1='<<ch1<<endl;
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
输出结果:ch1=give me a cup
string合并(全部)
string str1 = 'give me ';
string str2 = 'a cup';
str1 = str1 str2;
cout<<'str1='<<str1<<endl;
1
2
3
4
5
1
2
3
4
5
输出结果:str1=give me a cup
3.合并(部分)
char合并(部分):
char ch1[10] = 'ab'; // 注意合并后的长度 char ch2[] = 'abc'; strncat(ch1,ch2,3); // 参数3为从起始位置起的字符数 cout<<'ch1='<<ch1<<endl;
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
输出结果:ch1=ababc
string合并(部分):
string str1 = 'ab';
string str2 = 'cdefg';
str1.append(str2,2,3); // 参数2为数组下标,参数3为参数2后字符数。即将str2从下标为2位置起3个字符合并到str1
cout<<'str1='<<str1<<endl;
1
2
3
4
5
6
1
2
3
4
5
6
输出结果:str1=abefg
4.测长
char测长:
char ch1[] = 'give me'; int m = strlen(ch1); //不包括'\0’ cout<<'m='<<m<<endl;
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
输出结果:m=7
string测长:
string str1 = 'give me';
①int m = strlen(str1.c_str());
cout<<'m='<<m<<endl;
②int n = str1.size();
cout<<'n='<<n<<endl;
③int k = str1.length();
cout<<'k='<<k<<endl;
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
输出结果:m=7
n=7
k=7
5.替换
char替换:
char ch1[10] = 'ab'; char ch2[] = 'cdefg'; strncpy(ch1,ch2,3); // 拷贝ch2起始位置后3个字符赋给ch1 cout<<'ch1='<<ch1<<endl;
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
输出结果:ch1=cde
string替换:
string str1 = 'ab';
string str2 = 'cdefgh';
str1.replace(0,1,str2,4,2); // 将str2从下标4开始2个字符替换掉str1中从起始位置开始1个字符
// replace函数可重载,有多种形式,也支持Char型字符替换
cout<<'str1='<<str1<<endl;
1
2
3
4
5
6
1
2
3
4
5
6
输出结果:str1=ghb
6.拷贝
char拷贝:
char ch1[10] = 'abc'; char ch2[] = 'de'; memmove(ch1,ch2,2); //将ch2从起始位置起两个字符赋给ch1 cout<<'ch1='<<ch1<<endl;
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
输出结果:ch1=dec
string拷贝:
string str1 = 'abc';
char ch2[10] = 'defg';
str1.copy(ch2,10,1); // copy函数的第一个参数只能为char类型
// 将str1从下标1位置开始的10个字符赋给ch2
cout<<'ch2='<<ch2<<endl;
1
2
3
4
5
6
1
2
3
4
5
6
输出结果:ch2=bcfg
7.插入
string str1 = '1278'; string str2 = '3456'; str1.insert(2,str2,0,4); // 在str1下标为2的位置插入str2起始位置后4个字符 cout<<'str1='<<str1<<endl;
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
输出结果:str1=12345678
8.删除
string str('give me');
str.erase(2,2); // 删除从下标为2位置起两个字符
cout<<'str='<<str<<endl;
str.erase(2); // 删除下标为2位置后全部字符
cout<<'str='<<str<<endl;
str.erase(); // 删除所有字符
cout<<'str='<<str<<endl;
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
输出结果:str=gi me
str=gi
str=
9.查找
string str('Hello worldw'); int m = str.find('w',0); // 从str起始位置开始查找w字符 cout<<'m='<<m<<endl; int n = str.find_first_not_of('w',0); // 查找str起始位置开始不是w的字符 cout<<'n='<<n<<endl; int k = str.find_first_of('w',0); // 从str起始位置开始查找第一个w字符 cout<<'k='<<k<<endl; int l = str.find_last_of('w'); // 查找最后一个w的位置 cout<<'l='<<l<<endl; int p = str.find_last_not_of('w'); // 查找最后一个不是w的字符的位置 cout<<'p='<<p<<endl; int q = str.rfind('w'); // 反向查找 cout<<'q='<<q<<endl;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
输出结果:m=6
n=0
k=6
l=11
p=10
q=11
10.比较
string str1 = '155';
string str2 = '52';
char c[] = '34';
char c2[]= '56';
int z = strncat(c,c1);//用来比较两个char数组是否相等
int i = str1.compare(str2);
cout<<'i='<<i<<endl; // 输出i=-1
int j = str2.compare(c); // string字符串可以与char字符比较
cout<<'j='<<j<<endl; // 输出j=1
int l = str1.compare(0,2,str2); // 比较前两个字符
cout<<'l='<<l<<endl; // 输出l=-1;
int k = str1.compare(1,1,str2,0,1); // str1下标为1位置字符与str2下标为0位置字符比较
cout<<'k='<<k<<endl; // 输出k=0
int m = str1.compare(1,1,c,0,1);
cout<<'m='<<m<<endl;// 输出m=1
int n = str1.compare(1,1,c,1);
cout<<'n='<<n<<endl; // 输出n=1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
11.判空
string str = ''; if(str.empty())
- 1
- 2
- 1
- 2
12.转换(string转字符数组)
string str1 = 'Hello World';
const char* ch1;
ch1 = str1.c_str();
1
2
3
4
1
2
3
4
13.交换
char交换:
char ch1[15] = 'ofru'; char ch2[15] = ''; swab(ch1,ch2,strlen(ch1)); // 将ch1奇偶对调后传入ch2 cout<<'ch2='<<ch2<<endl;
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
输出结果:ch2=four
string交换:
string str1 = 'four';
string str2 = '';
str1.swap(str2);
cout<<'str2='<<str2<<endl;
1
2
3
4
5
1
2
3
4
5
输出结果:str2=four
以上,整理自《范磊C 视频》笔记
赞 (0)