C++ 学习笔记
1、类型表示范围
| 类型 | 字节数 | 位宽 | 十进制范围(大约) | 具体值范围 |
|---|
| char | 1 | 8 位 | -128 ~ 127 | -2⁷ ~ 2⁷-1 |
| short | 2 | 16 位 | -32,768 ~ 32,767 | -2¹⁵ ~ 2¹⁵-1 |
| int | 4 | 32 位 | -21 亿 ~ 21 亿 | -2,147,483,648 ~ 2,147,483,647 |
| long | 4/8 | 32/64 位 | 同 int 或 long long | 系统相关 |
| long long | 8 | 64 位 | -922 亿亿 ~ 922 亿亿 | -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 |
| 类型 | 字节数 | 十进制范围(大约) | 具体值范围 |
|---|
| unsigned char | 1 | 0 ~ 255 | 0 ~ 2⁸-1 |
| unsigned short | 2 | 0 ~ 65,535 | 0 ~ 2¹⁶-1 |
| unsigned int | 4 | 0 ~ 42.9 亿 | 0 ~ 2³²-1 |
| unsigned long | 4/8 | 同 uint 或 ulong long | 系统相关 |
| unsigned long long | 8 | 0 ~ 1844 亿亿 | 0 ~ 2⁶⁴-1 |
| 类型 | 字节数 | 十进制范围(大约) | 最小值(大约) | 有效数字(十进制) | 精度说明 |
|---|
| float | 4 | ±3.4×10³⁸ | ±1.2×10⁻³⁸ | 6~7 位 | 单精度浮点 |
| double | 8 | ±1.8×10³⁰⁸ | ±2.2×10⁻³⁰⁸ | 15~16 位 | 双精度浮点 |
| long double | 8/16 | 范围更大 | 精度更高 | 18~19 位或更多 | 扩展精度浮点 |
TIP:float 的表示范围比 long long 大得多
| 后缀 | 含义 |
|---|
| U | 表示无符号数 |
| L | long long |
| UL | unsigned long |
| ULL | unsigned long long |
| F | float |
| D | double |
2、cout
| 标志 | 功能 | 示例代码 | 输出示例 |
|---|
fixed | 固定小数格式 | cout << fixed << 123.456; | 123.456000 |
scientific | 科学计数法 | cout << scientific << 123.456; | 1.234560e+02 |
defaultfloat | 恢复默认格式 | cout << defaultfloat << 123.456; | 123.456 |
showpoint | 总是显示小数点 | cout << showpoint << 100; | 100.000 |
noshowpoint | 取消显示小数点 | cout << noshowpoint << 100.0; | 100 |
showpos | 显示正号 | cout << showpos << 42; | +42 |
noshowpos | 取消显示正号 | cout << noshowpos << +42; | 42 |
boolalpha | 布尔显示 true/false | cout << boolalpha << true; | true |
noboolalpha | 布尔显示 1/0 | cout << noboolalpha << true; | 1 |
| 方法/标志 | 功能 | 示例代码 | 输出示例 |
|---|
width(n) | 设置输出宽度 | cout.width(10); cout << 123; | 123 |
setw(n) | 设置宽度 (常用) | cout << setw(10) << 456; | 456 |
left | 左对齐 | cout << left << setw(10) << "ABC"; | ABC |
right | 右对齐 (默认) | cout << right << setw(10) << "DEF"; | DEF |
internal | 内部对齐 (符号左) | cout << internal << setw(10) << -123; | - 123 |
fill(ch) | 设置填充字符 | cout.fill('*'); cout.width(10) << 123; | *******123 |
setfill(ch) | 设置填充字符 | cout << setfill('-') << setw(10) << 456; | -------456 |
| 方法 | 功能 | 示例代码 | 输出示例 |
|---|
precision(n) | 设置精度 | cout.precision(3); cout << 3.14159; | 3.14 |
setprecision(n) | 设置精度 (常用) | cout << setprecision(5) << 3.14159; | 3.1416 |
fixed + setprecision | 固定小数位数 | cout << fixed << setprecision(2) << 3.14159; | 3.14 |
scientific + setprecision | 科学计数法精度 | cout << scientific << setprecision(3) << 3.14159; | 3.142e+00 |
| 标志/方法 | 功能 | 示例代码 | 输出示例 |
|---|
dec | 十进制 (默认) | cout << dec << 255; | 255 |
hex | 十六进制 | cout << hex << 255; | ff |
oct | 八进制 | cout << oct << 255; | 377 |
showbase | 显示进制前缀 | cout << showbase << hex << 255; | 0xff |
noshowbase | 取消进制前缀 | cout << noshowbase << hex << 255; | ff |
uppercase | 大写字母 | cout << uppercase << hex << 255; | FF |
nouppercase | 小写字母 | cout << nouppercase << hex << 255; | ff |
bitset<n>(num) | 二进制输出 | cout << bitset<8>(255); | 11111111 |
| 方法/标志 | 功能 | 示例代码 | 说明 |
|---|
flush | 刷新缓冲区 | cout << "处理中..." << flush; | 立即输出 |
endl | 换行并刷新 | cout << "Line 1" << endl; | 换行 + 刷新 |
ends | 输出空字符 | cout << "Hello" << ends; | 输出 Hello\0 |
unitbuf | 每次输出都刷新 | cout << unitbuf; | 无缓冲输出 |
nounitbuf | 恢复缓冲 | cout << nounitbuf; | 正常缓冲 |
| 用途 | 示例代码 | 输出示例 |
|---|
| 表格对齐 | cout << left << setw(10) << "姓名" << right << setw(8) << "成绩"; | 姓名 成绩 |
| 货币格式 | cout << fixed << setprecision(2) << "$" << 19.99; | $19.99 |
| 科学数据 | cout << scientific << setprecision(3) << 0.00123456; | 1.235e-03 |
| 填充编号 | cout << setfill('0') << right << setw(5) << 42; | 00042 |
| 地址格式 | cout << hex << showbase << uppercase << 0xdeadbeef; | 0xDEADBEEF |
TIP:使用 setw, setprecision, setfill 需要 #include <iomanip>。使用二进制输出,需要 #include <bitset>。作用范围:有些设置只影响下一个输出例如 width(),有些设置会保持有效直到被更改。
3、char
- char 的本质上是 数字,通过 ASCII 表跟对应的数值联系起来
- char 和整形运算,会先将字符转换为对应的数值,运算结果是一个数字
| 类型 | 字节数 | 范围 | 映射表 |
|---|
| [signed] char | 1B | -128~127 | ASCII |
| unsigned char | 1B | 0~255 | ASCII |
| 常见转义字符 | 含义 | ASCII 值 |
|---|
| \n | 换行 | 010 |
| \t | 水平制表 | 009 |
| \ | 反斜杠字符 | 092 |
| ' | 单引号字符 | 039 |
| " | 双引号字符 | 034 |
4、string
| 容量操作方法 | 功能 | 示例 |
|---|
length() / size() | 字符串长度 | s.length(); / s.size(); |
empty() | 是否为空 | if(s.empty()) { ... } |
capacity() | 容量大小 | s.capacity(); |
reserve(n) | 预分配空间 | s.reserve(100); |
resize(n) | 调整大小 | s.resize(10); |
resize(n, c) | 调整大小并填充 | s.resize(10, 'x'); |
shrink_to_fit() | 释放多余空间 | s.shrink_to_fit(); |
max_size() | 最大可能长度 | s.max_size(); |
| 元素访问方法 | 功能 | 示例 |
|---|
operator[] | 访问字符 | char c = s[0]; |
at(n) | 访问字符 (检查边界) | char c = s.at(0); |
front() | 第一个字符 | char c = s.front(); |
back() | 最后一个字符 | char c = s.back(); |
| 修改方法 | 功能 | 示例 |
|---|
push_back(c) | 末尾添加字符 | s.push_back('!'); |
append(str) | 追加字符串 | s.append(" world"); |
operator+= | 追加 | s += " world"; |
insert(pos, str) | 插入字符串 | s.insert(0, "Hello "); |
insert(pos, n, c) | 插入 n 个字符 | s.insert(5, 3, '*'); |
| 删除方法 | 功能 | 示例 |
|---|
pop_back() | 删除末尾字符 | s.pop_back(); |
erase(pos) | 删除 pos 处字符 | s.erase(5); |
erase(pos, len) | 删除从 pos 开始的 len 个字符 | s.erase(5, 3); |
erase(iterator) | 删除迭代器指向的字符 | s.erase(s.begin()); |
erase(first, last) | 删除迭代器范围 | s.erase(s.begin(), s.begin()+3); |
clear() | 清空字符串 | s.clear(); |
| 替换方法 | 功能 | 示例 |
|---|
replace(pos, len, str) | 替换子串 | s.replace(0, 5, "Hi"); |
replace(first, last, str) | 替换迭代器范围 | s.replace(s.begin(), s.begin()+5, "Hi"); |
| 字符串操作 | 功能 | 示例 |
|---|
substr(pos) | 从 pos 开始的子串 | string sub = s.substr(6); |
substr(pos, len) | 从 pos 开始 len 长度的子串 | string sub = s.substr(0, 5); |
find(str, pos) | 查找子串位置 | int pos = s.find("world"); |
rfind(str, pos) | 从后往前查找 | int pos = s.rfind("world"); |
find_first_of(str, pos) | 查找任意字符首次出现 | int pos = s.find_first_of("aeiou"); |
find_last_of(str, pos) | 查找任意字符最后出现 | int pos = s.find_last_of("aeiou"); |
find_first_not_of(str, pos) | 查找不在 str 中的字符 | int pos = s.find_first_not_of(" "); |
find_last_not_of(str, pos) | 从后往前查找不在 str 中的字符 | int pos = s.find_last_not_of(" "); |
compare(str) | 比较字符串 | if(s.compare("hello") == 0) { ... } |
swap(str) | 交换字符串 | s1.swap(s2); |
copy(buf, len, pos) | 复制到字符数组 | char buf[10]; s.copy(buf, 5, 0); |
| 迭代器 | 功能 | 示例 |
|---|
begin() / end() | 正向迭代器 | for(auto it=s.begin(); it!=s.end(); ++it) |
cbegin() / cend() | const 正向迭代器 | for(auto it=s.cbegin(); it!=s.cend(); ++it) |
rbegin() / rend() | 反向迭代器 | for(auto it=s.rbegin(); it!=s.rend(); ++it) |
crbegin() / crend() | const 反向迭代器 | for(auto it=s.crbegin(); it!=s.crend(); ++it) |
| 数字转换 | 功能 | 示例 |
|---|
to_string(num) | 数字转字符串 | string s = to_string(123); |
stoi(str) | 字符串转 int | int n = stoi("123"); |
stol(str) | 字符串转 long | long n = stol("123"); |
stoll(str) | 字符串转 long long | long long n = stoll("123"); |
stof(str) | 字符串转 float | float f = stof("123.45"); |
stod(str) | 字符串转 double | double d = stod("123.45"); |
stold(str) | 字符串转 long double | long double ld = stold("123.45"); |
5、逻辑运算符
优先级:非 > 与 > 异或 > 或
6、枚举
- 枚举本质上是一个 被命名的整型常数集合,在 C/C++ 语言中它是一种基本数据类型
- 枚举的作用是提高代码的可读性、可维护性和键入性,因为枚举可以将一些数字或字符串符号化 —— 将数字标号定义为具体的符号
#include <iostream>
using namespace std;
enum Season {
SPRING,
SUMMER = 3,
AUTUMN,
WINTER
};
int main() {
Season s1 = AUTUMN;
cout << s1;
switch(s1) {
case SPRING: cout << "春天" << endl; break;
default: cout << "不是春天" << endl;
}
return 0;
}
7、随机数
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0));
int r = rand() % 100;
return 0;
}
Tip
- 需要设置随机种子,才能使得每次运行生成的随机数不同
- 设置完随机种子后,会生成一个随机数序列,每次调用 rand() 会从这个序列中选出一个随机数
- time(0) 返回以秒为单位的时间戳,如果将 srand(time(0)) 放在 for 循环里面,而每次 for 循环执行的时间间隔少于一秒,则每次循环都会生成相同的随机数序列
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
for(int i = 1; i < 5; i++) {
srand(time(0));
cout << rand() << " " << rand() << endl;
}
return 0;
}
8、数组
#include <iostream>
using namespace std;
int main() {
int arr1[5] = {1, 2, 3, 4, 5};
cout << arr1[0] << endl;
for (int i = 0; i < 5; i++) {
cout << arr1[i] << " ";
}
return 0;
}
注意:数组下标从 0 开始,访问越界会导致未定义行为。建议使用 std::vector 替代原生数组以获得更好的安全性。
9、其他
- 预处理指令:
#include, #define, #ifdef 等
- 命名空间:
using namespace std; 避免重复输入 std
- 注释:
// 单行注释,/* */ 多行注释
本文主要介绍了 C++ 基础部分的第一章内容,后续章节将涵盖指针、结构体、引用、函数及 static 关键字等进阶主题。建议读者结合实践练习巩固上述知识点。