C++基础知识笔记模板
2025-10-11 22:32:12
发布于:广东
虽然我C++ 学的不多,但我总结了直到一维数组的C++ 基础知识笔记(我学了一年半的C++总结的,不一定完全,欢迎发到评论区!)。
1. 头文件
万能头文件
#include <bits/stdc++.h>
using namespace std;
int main() {
return 0;
}
提供文件输入输出流对象
#include <fstream>
using namespace std;
int main() {
return 0;
}
提供标准输入输出流,如cin和cout
#include <iostream>
using namespace std;
int main() {
return 0;
}
C风格的输入输出函数。
#include <cstdio>
using namespace std;
int main() {
return 0;
}
提供数学函数,如sin、cos、sqrt、max、min、pow、abs、sqrt等
#include <cmath>
using namespace std;
int main() {
return 0;
}
支持复数运算
#include <complex>
using namespace std;
int main() {
return 0;
}
数值运算工具
#include <numeric>
using namespace std;
int main() {
return 0;
}
随机数生成器
#include <random>
using namespace std;
int main() {
return 0;
}
动态数组容器
#include <vector>
using namespace std;
int main() {
return 0;
}
双向链表容器
#include <list>
using namespace std;
int main() {
return 0;
}
映射容器,用于键值对存储
#include <map>
using namespace std;
int main() {
return 0;
}
提供常用算法,如排序、查找等
#include <algorithm>
using namespace std;
int main() {
return 0;
}
提供字符串类std::string
#include <string>
using namespace std;
int main() {
return 0;
}
C风格字符串处理函数
#include <cstring>
using namespace std;
int main() {
return 0;
}
支持正则表达式处理。
#include <regex>
using namespace std;
int main() {
return 0;
}
提供时间和日期相关函数
#include <ctime>
using namespace std;
int main() {
return 0;
}
高精度时间处理工具
#include <chrono>
using namespace std;
int main() {
return 0;
}
提供多线程支持
#include <thread>
using namespace std;
int main() {
return 0;
}
互斥锁,用于线程同步
#include <mutex>
using namespace std;
int main() {
return 0;
}
条件变量,用于线程间通信
#include <condition_variable>
using namespace std;
int main() {
return 0;
}
异常处理
#include <exception>
using namespace std;
int main() {
return 0;
}
提供标准异常类型,如runtime_error
#include <stdexcept>
using namespace std;
int main() {
return 0;
}
基于字符串的流操作
#include <sstream>
using namespace std;
int main() {
return 0;
}
底层流缓冲支持
#include <streambuf>
using namespace std;
int main() {
return 0;
}
提供通用工具类,如stdpair和stdmove
#include <utility>
using namespace std;
int main() {
return 0;
}
位集容器
#include <bitset>
using namespace std;
int main() {
return 0;
}
定义函数对象和绑定操作
#include <functional>
using namespace std;
int main() {
return 0;
}
2.输出与换行
cout << 123;//"cout"是输出的意思
则输出
123
cout << 123 << endl;//"endl"是换行的意思
cout << 456;
则输出
123
456
! ! !注意加分号“;”在句末! ! !
3.运算符
左右都是整数,结果是整数
7 / 2 = 3
8 / 3 = 2
左右其中一个是小数, 结果是小数
7.0 / 2 = 3.5
8.0 / 3 = 2.666
"%"是取余符号,求余数
8 % 3 = 2;//相当于8 / 3 = 2 ...... 2的余数2
加是“+“
减是“-”
乘是“*”
除是“/”
取余是“%”
4.数据类型
整数int 、long long
浮点数(小数)double 、float
字符char
int 16位 -32768至+32767
double精度高,有效数字16位,float精度7位。但double消耗内存是float的两倍,double的运算速度比float慢得多,在不确定的情况下还是尽量用double以保持正确性
5.变量与输入
变量名只能由数字、字母、下划线组成,且开头不能是数字
int n;//"n"是变量名
cin >> n;//"cin"为输入的意思
cout << n;//输出
6.格式化输出
int a, b;
cin >> a >> b;
printf("%8d",a);
cout << endl;
printf("%08d",b);
如果输入123 456
则输出
123
00000456
//是对齐的,一个用空格(加数字共8位),一个用0(加数字共8位)
输出
printf("%d",3);//相当于cout << 3;
保留n位小数
double a, b;
double c = a / b;
printf("%.nlf",c); //n是小数位数 , c可以改为别的数
7.if语句
单分支
if(分支条件) {
// 分支中的代码
}
双分支
if(分支条件) {
// 分支中的代码
}else{
// 分支中的代码
}
多分支
if(分支条件) {
// 分支中的代码
}else if (分支条件){
// 分支中的代码
}else if (分支条件) {
// 分支中的代码
}else{
}
找最大数
#include <bits/stdc++.h>
using namespace std;
int main () {
int a, b, c;
cin >> a >> b >> c;
int max;
max = a;
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
cout << max;
return 0;
}
8.数学函数
#include <iostream>
#include <cmath>
using namespace std;
int main() {
//数学函数代码
return 0;
}
max 求同类型两者的较大者
同类型的 a 和 b
max(4,5) = 5
min 求同类型两者的较小者
同类型的 a 和 b
min(4,5) = 4
pow double 型的 a 和 b,返回a 的 b次方
double 型的 a 和 b
pow(2, 4) = 16
abs 求数字 x 的绝对值,返回 |x|
数字 x (浮点型 / 整型)
abs(-3) = 3
sqrt double 型的 x,求x的平方根
double 类型的 x
sqrt(81) = 9.0
9.bool类型
bool分为true(1)、false(0)
bool flag = true;
cout << flag;
则输出1
, 因为1 = true
bool flag = false;
cout << flag;
则输出0
因为0 = false
非0
为true
10.break & continue
break
:停止,遇到它的时候循环后续内容不再执行
continue
:暂停,遇到它的时候这轮暂停,后续继续执行
11.switch语句
switch(条件表达式) {
case 1:// 语句1的代码 ;break;
case 2:// 语句2的代码 ;break;
case 3:// 语句3的代码 ;break;
case 4:// 语句3的代码 ;break;
...
default://否则...;
}
没遇到break
会一直执行下去
12.字符变量char
int n = 1//整数
char c = '1'//字符
对应数值
c = '0' = (int)48
c = 'A' = (int)65
c = 'a' = (int)97
大写转小写
#include <bits/stdc++.h>
using namespace std;
int main () {
char c;
cin >> c;
cout << (char)(c + 32);
return 0;
}
小写转大写
#include <bits/stdc++.h>
using namespace std;
int main () {
char c;
cin >> c;
cout << (char)(c - 32);
return 0;
}
13.逻辑运算符
“与”
if( && ) { // "&&"是与运算符,当两个条件同时成立
}
“或”
if ( || ) { // "||"是或运算符,一个条件成立即可
}
“非”
if (! ) { // "!"是非运算符,表条件否定
}
14.for循环
for循环框架
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) {
//循环内容
}
return 0;
}
for嵌套循环
图形输出(1)
#include <bits/stdc++.h>
using namespace std;
int main () {
int n;
cin >> n;
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= n;j++) {
cout << "*";
}
cout << endl;
}
return 0;
}
输入3
输出
***
***
***
图形输出(2)
#include <bits/stdc++.h>
using namespace std;
int main () {
int n;
cin >> n;
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= i;j++) {
cout << "*";
}
cout << endl;
}
return 0;
}
输入3
输出
*
**
***
图形输出(3)
#include <bits/stdc++.h>
using namespace std;
int main () {
int n;
cin >> n;
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= n - i + 1;j++) {
cout << "*";
}
cout << endl;
}
return 0;
}
输入3
输出
***
**
*
判断质数
#include <bits/stdc++.h>
using namespace std;
int main () {
int n;
cin >> n;
int f;
for (int i = 1;i <= n;i++) {
if (n % i == 0) {
f = false;
break;
}
}
if (n > 1 && f == true) {
cout << "yes";
}else{
cout << "no";
}
return 0;
}
求最小公倍数
#include <bits/stdc++.h>
using namespace std;
int main () {
int n;
int m;
cin >> n;
cin >> m;
int a;
int x = n, y = m;
int b;
if (n == m) {
cout << n * m << " ";
return 0;
}
for (int i = 1;;i++) {
if (n > m) {
a = n - m;
n = a;
m = m;
}else if (m > n){
a = m - n;
m = a;
n = n;
}else{
b = m;
break;
}
}
int c = x * y / b;
cout << c;
return 0;
}
求最大公因数
#include <bits/stdc++.h>
using namespace std;
int main () {
int n, m;
cin >> n >> m;
int x;
int a, b;
a = n;
b = m;
if (a == b) {
cout << 1;
return 0;
}
for (int i = 1;;i++) {
x = n % m;
n = m;
m = x;
if (m == 0) {
break;
}
}
cout << n;
return 0;
}
15.while循环
while框架
while (条件表达式) {
// 循环体中的代码
}
do while框架
do {
// 循环体中的代码
} while (条件表达式);
输出各位数字倒序
#include <bits/stdc++.h>
using namespace std;
int main () {
int n;
cin >> n;
while (n != 0) {
cout << n % 10 << " ";
n /= 10;
}
return 0;
}
输出各位数字和
#include <bits/stdc++.h>
using namespace std;
int main () {
int n;
cin >> n;
int s = 0;
while (n != 0) {
s += n % 10;
n /= 10;
}
cout << s;
return 0;
}
输出位数
#include <bits/stdc++.h>
using namespace std;
int main () {
int n;
cin >> n;
int s = 0;
while (n != 0) {
s++;
n /= 10;
}
cout << s;
return 0;
}
判断回文数
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int m = n;
int sum = 0;
while (n > 0) {
int s = n % 10;
sum = sum * 10 + s;
n /= 10;
}
if (m == sum) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
这里空空如也
有帮助,赞一个