目录
1. 字符与整数的联系——ASCII码每个常用字符都对应一个-128 ~ 127的数字,二者之间可以相互转化。注意:目前负数没有与之对应的字符。
3. 标准库类型string可变长的字符序列,比字符数组更加好用。需要引入头文件:
(3) string的empty和size操作(注意size是无符号整数,因此 s.size() <= -1一定成立):
3.3 处理string对象中的字符可以将string对象当成字符数组来处理:
1、[ ]+下标 因为string类对[ ]运算符进行了重载,所以我们可以直接使用[ ]+下标访问对象中的元素。并且该重载使用的是引用返回,所以我们可以通过[ ]+下标修改对应位置的元素。
2、使用at访问对象中的元素 因为at函数也是使用的引用返回,所以我们也可以通过at函数修改对应位置的元素。
1. 字符与整数的联系——ASCII码
每个常用字符都对应一个-128 ~ 127的数字,二者之间可以相互转化。注意:目前负数没有与之对应的字符。
#include <iostream>
using namespace std;
int main()
{
char c = 'a';
cout << (int)c << endl;
int a = 66;
cout << (char)a << endl;
return 0;
}
常用ASCII值:'A'- 'Z'是65 ~ 90,'a' - 'z'是97 - 122,0 - 9是 48 - 57。
字符可以参与运算,运算时会将其当做整数:
#include <iostream>
using namespace std;
int main()
{
int a = 'B' - 'A';
int b = 'A' * 'B';
char c = 'A' + 2;
cout << a << endl;
cout << b << endl;
cout << c << endl;
return 0;
}
2.字符数组
字符串就是字符数组加上结束符'\0'。
可以使用字符串来初始化字符数组,但此时要注意,每个字符串结尾会暗含一个'\0'字符,因此字符数组的长度至少要比字符串的长度多 1 !
#include <iostream>
using namespace std;
int main()
{
char str[100];
cin >> str; // 输入字符串时,遇到空格或者回车就会停止
cout << str << endl; // 输出字符串时,遇到空格或者回车不会停止,遇到'\0'时停止
printf("%s\n", str);
return 0;
}
读入一行字符串,包括空格:
#include <iostream>
using namespace std;
int main()
{
char str[100];
fgets(str, 100, stdin); // gets函数在新版C++中被移除了,因为不安全。
// 可以用fgets代替,但注意fgets不会删除行末的回车字符
cout << str << endl;
return 0;
}
2.2 字符数组的常用操作
下面几个函数需要引入头文件:
#include <string.h>
(1) strlen(str),求字符串的长度
(2) strcmp(a, b),比较两个字符串的大小,a < b返回-1,a == b返回0,a > b返回1。这里的比较方式是字典序!
(3) strcpy(a, b),将字符串b复制给从a开始的字符数组。
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char a[100] = "hello world!", b[100];
cout << strlen(a) << endl;
strcpy(b, a);
cout << strcmp(a, b) << endl;
return 0;
}
2.3 遍历字符数组中的字符:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char a[100] = "hello world!";
// 注意:下述for循环每次均会执行strlen(a),运行效率较低,最好将strlen(a)用一个变量存下来
for (int i = 0; i < strlen(a); i ++ )
cout << a[i] << endl;
return 0;
}
3. 标准库类型string
可变长的字符序列,比字符数组更加好用。需要引入头文件:
#include <string>
3.1 定义和初始化
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1; // 默认初始化,s1是一个空字符串
string s2 = s1; // s2是s1的副本,注意s2只是与s1的值相同,并不指向同一段地址
string s3 = "hiya"; // s3是该字符串字面值的副本
string s4(10, 'c'); // s4的内容是 "cccccccccc"
return 0;
}
3.2 string上的操作
(1) string的读写:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1, s2;
cin >> s1 >> s2;
cout << s1 << s2 << endl;
return 0;
}
注意:不能用printf直接输出string,需要写成:printf(“%s”, s.c_str());
(2) 使用getline读取一整行
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
getline(cin, s);
cout << s << endl;
return 0;
}
(3) string的empty和size操作(注意size是无符号整数,因此 s.size() <= -1一定成立):
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1, s2 = "abc";
cout << s1.empty() << endl;
cout << s2.empty() << endl;
cout << s2.size() << endl;
return 0;
}
(4) string的比较:
支持 >, <, >=, <=, ==, !=等所有比较操作,按字典序进行比较。
(5) 为string对象赋值:
string s1(10, 'c'), s2; // s1的内容是 cccccccccc;s2是一个空字符串
s1 = s2; // 赋值:用s2的副本替换s1的副本
// 此时s1和s2都是空字符串
(6) 两个string对象相加:
做加法运算时,字面值和字符都会被转化成string对象,因此直接相加就是将这些字面值串联起来:
string s1 = "hello", s2 = "world"; // 在s1和s2中都没有标点符号
string s3 = s1 + ", " + s2 + '\n';
(7) 字面值和string对象相加:
做加法运算时,字面值和字符都会被转化成string对象,因此直接相加就是将这些字面值串联起来:
string s1 = "hello", s2 = "world"; // 在s1和s2中都没有标点符号
string s3 = s1 + ", " + s2 + '\n';
当把string对象和字符字面值及字符串字面值混在一条语句中使用时,必须确保每个加法运算符的两侧的运算对象至少有一个是string:
string s4 = s1 + ", "; // 正确:把一个string对象和有一个字面值相加
string s5 = "hello" + ", "; // 错误:两个运算对象都不是string
string s6 = s1 + ", " + "world"; // 正确,每个加法运算都有一个运算符是string
string s7 = "hello" + ", " + s2; // 错误:不能把字面值直接相加,运算是从左到右进行的
3.3 处理string对象中的字符
可以将string对象当成字符数组来处理:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = "hello world";
for (int i = 0; i < s.size(); i ++ )
cout << s[i] << endl;
return 0;
}
或者使用基于范围的for语句:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = "hello world";
for (char c: s) cout << c << endl;
for (char& c: s) c = 'a';
cout << s << endl;
return 0;
}
4.string中元素的访问
1、[ ]+下标
因为string类对[ ]运算符进行了重载,所以我们可以直接使用[ ]+下标访问对象中的元素。并且该重载使用的是引用返回,所以我们可以通过[ ]+下标修改对应位置的元素。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("CSDN");
//[]+下标访问对象元素
for (size_t i = 0; i < s.size(); i++)
{
cout << s[i];
}
cout << endl;
//[]+下标修改对象元素内容
for (size_t i = 0; i < s.size(); i++)
{
s[i] = 'x';
}
cout << s << endl; //xxxx
return 0;
}
2、使用at访问对象中的元素
因为at函数也是使用的引用返回,所以我们也可以通过at函数修改对应位置的元素。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("CSDN");
for (size_t i = 0; i < s.size(); i++)
{
//at(pos)访问pos位置的元素
cout << s.at(i);
}
cout << endl;
for (size_t i = 0; i < s.size(); i++)
{
//at(pos)访问pos位置的元素,并对其进行修改
s.at(i) = 'x';
}
cout << s << endl; //xxxx
return 0;
}
3、使用范围for访问对象中的元素
需要特别注意的是:若是需要通过范围for修改对象的元素,则用于接收元素的变量e的类型必须是引用类型,否则e只是对象元素的拷贝,对e的修改不会影响到对象的元素。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("CSDN");
//使用范围for访问对象元素
for (auto e : s)
{
cout << e;
}
cout << endl; //CSDN
//使用范围for访问对象元素,并对其进行修改
for (auto& e : s) //需要修改对象的元素,e必须是引用类型
{
e = 'x';
}
cout << s << endl; //xxxx
return 0;
}
5.string中子字符串的提取
1、使用substr函数提取string中的子字符串
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("abcdef");
string s2;
//substr(pos, n)提取pos位置开始的n个字符序列作为返回值
s2 = s1.substr(2, 4);
cout << s2 << endl; //cdef
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("abcdef");
char str[20];
//copy(str, n, pos)复制pos位置开始的n个字符到str字符串
size_t length = s.copy(str, 4, 2);
//copy函数不会在复制内容的末尾附加'\0',需要手动加
str[length] = '\0';
cout << str << endl; //cdef
return 0;
}