由单引号括起来的一个字符被称作 char 型字面值,双引号括起来的零个或多个字符则构成字符串型字面值。字符串字面值的类型实际上就是由常量字符构成的数组,,编译器在每一个字符串后面添加一个空字符(’\0’),因此字符串的实际长度要比他的内容多1。
如字面值 ‘A’ 表示的就是单独字符 A ,而字符串 “A” 代表了一个包含两个字符的字符数组,分别是字母 A 和空字符。
字符型
定义:存储单个字符的数据类型
例如:‘1’ ‘A’
字符变量
数据类型 变量名
例如:char name;
数据类型 变量名 = 初始值;
例如: char name = ‘A’;
ASCII码表

特别记忆:
字符‘0’:48
字符‘A’:65
字符‘a’:97
转换关系
- int 与 char的转换(本质是字符的 ASCII 码值)
char ch = 数值;
int asci = 字符;
- 大小写的转换
大写字母 + 32 -> 小写字母
小写字母 – 32 ->大写字母
- 数字字符和数字的转换
数字 + 48 -> 8 数字字符
数字 + ‘0’ -> 数字字符
数字字符 – 48 -> 数字
数字字符 – ‘0’ -> 数字
string字符串
是C++语言中包含若干字符的数据类型
定义: string 变量名
例如:string word;
使用指定字符串初始化string变量
string word = "hello!";
创建一个长度为10的string变量,每个字符都被初始化为字符a
string word(10,'a');
string输入输出
格式:cout << 变量名;
string word = "hello!";
cout << word;
格式:cin >> 变量名;
string word;
cin >> word;
输入时遇到空格或回车键将停止。但需要注意的是只有按下回车键时才会结束输入执行,当按下空格后还能继续输入,但最终存到字符串中的只是第一个空格之前输入的字符串(开头的空白除外,程序会自动忽略开头的空白的),空格操作可以用来同时对多个字符串进行初始化,如下例
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string s1, s2, s3; // 初始化一个空字符串
// 单字符串输入,读入字符串,遇到空格或回车停止
cin >> s1;
// 多字符串的输入,遇到空格代表当前字符串赋值完成,转到下个字符串赋值,回车停止
cin >> s2 >> s3;
// 输出字符串
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
return 0;
}
// 运行结果 //
abc def hig
abc
def
hig
如果希望在最终读入的字符串中保留空格,可以使用 getline 函数,例子如下:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string s1 ; // 初始化一个空字符串
getline(cin , s1);
cout << s1 << endl; // 输出
return 0;
}
// 结果输出 //
abc def hi
abc def hi
通过下标获取字符串元素的值
格式:变量名[下标];
string word = "hello world";
cout << word[0];
- 获取有效长度
格式:变量名.size();
string word = "hello";
cout << word.size();
格式:变量名.length();
string word;
cout << word.length();
- 使用关系运算符进行比较
比较规则:
根据ASCII码从前到后逐位比较
string a = "abb", b = "abc";
if (a > b) cout << 1 << endl;
else cout << 0 << endl;
- 拼接、比比较等操作
s1+s2 // 返回 s1 和 s2 拼接后的结果。加号两边至少有一个 string 对象,不能都是字面值
s1 == s2 // 如果 s1 和 s2 中的元素完全相等则它们相等,区分大小写
s1 != s2
<, <=, >, >= // 利用字符的字典序进行比较,区分大小写
字符串函数
查找函数
搜索操作返回指定字符出现的下标,如果未找到返回 string::npos
s.find(args) // 查找 字符串s 中 args 第一次出现的位置
s.rfind(args) // 查找 字符串s 中 args 最后一次出现的位置
s.find_first_of(args) // 在 s 中查找 args 中任何一个字符最早出现的位置
s.find_last_of(args) // 在 s 中查找 args 中任何一个字符最晚出现的位置
例如:
string s1 = "nice to meet you~";
cout << s1.find_first_of("mey") << endl; // 输出结果为 3,'e' 出现的最早
截取函数:
substr()是C++标准库 中std::string类的一个成员函数,它用于从一个字符串中提取指定位置和长度的子字符串。想象一下,你有一整段文字,但只需要其中的一部分——substr()就是帮你完成这个任务的“剪刀”。
- 语法:变量名.substr(位置,[ 长度 ])
substr(pos, count):从位置pos开始,提取count个字符substr(pos):从位置pos开始,提取到字符串末尾
#include <iostream>
#include <string>
using namespace std;
int main() {
string text = "Hello, C++ World!";
// 提取从位置7开始的子字符串
string sub1 = text.substr(7);
cout << sub1 << endl; // 输出: C++ World!
// 提取从位置0开始,长度为5的子字符串
string sub2 = text.substr(0, 5);
cout << sub2 << endl; // 输出: Hello
return 0;
}
插入函数
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str("All that exists is what's ahead.");
string a, b;
a = str.insert(4,"sky");
//在下标为4的位置,插入字符串sky
cout << a << endl; //输出All skythat exists is what's ahead.
cout << str << endl;//原字符串str也会被修改 str == a
str = "All that exists is what's ahead.";
b = str.insert(4,5,'x');
//在下标为4的位置,插入字符串5个字符x
cout << b << endl; //输出 All xxxxxthat exists is what's ahead.
cout << str << endl;//原字符串str也会被修改 str == b
return 0;
}
替换函数
replace函数可以替换字符串中的指定字符或子字符串。它的用法如下:
变量名.replase(位置,长度,变量名/字面量)
string str = "Hello World";
str.replace(str.find("World"), 5, "C++");
cout << str << endl; // 输出:Hello C++
连续读入
int n;
while(cin >> n)
{
cout << n << endl;
}
用于从标准输入读取数据,直到文件结尾或遇到无效输入或ctrl+z结束,适用于处理若干组输入数据
排序/翻转
string word = "hello";
sort(word.begin(), word.end());
cout << word << endl;
reverse(word.begin(), word.end());
cout << word << endl;
- sort(变量名.begin(),变量名.end());
- reverse(变量名.begin(), 变量名.end());
string 汇总









暂无评论内容