跳到主要内容
C++ 语言基础核心知识点汇总 | 极客日志
C++ 算法
C++ 语言基础核心知识点汇总 综述由AI生成 C++ 是一门融合了面向过程、面向对象和泛型编程的多范式语言,广泛应用于系统开发、游戏引擎等领域。系统梳理了 C++ 的核心知识点,涵盖基础语法、控制流、函数封装、数组与字符串、面向对象特性(类、继承、多态)、STL 容器及异常处理。每个概念均配有可运行的代码示例,帮助初学者快速掌握从入门到进阶的学习路径。
人间过客 发布于 2026/2/9 更新于 2026/6/2 26 浏览一、基础语法:程序的骨架
1.1 第一个 C++ 程序
#include <iostream>
int main () {
std::cout << "Hello, C++!" << std::endl;
return 0 ;
}
编译运行 :使用 g++ 编译:g++ hello.cpp -o hello,执行 ./hello 输出 Hello, C++!。
核心说明 :
#include <iostream>:引入标准输入输出库,必须包含才能使用 cout。
std::cout:标准输出对象,std 是命名空间(避免命名冲突)。
main():程序唯一入口,返回 0 表示成功,非 0 表示异常。
1.2 变量与数据类型
C++ 是强类型语言,变量必须先声明类型再使用,基本数据类型如下:
类型 说明 示例 int整数(4 字节) int age = 20;float单精度浮点数(4 字节) float pi = 3.14f;double双精度浮点数(8 字节) double salary = 5000.5;char字符(1 字节) char grade = 'A';bool布尔值(true/false) bool isStudent = true;
示例 :
#include <iostream>
using namespace std;
{
a = ;
b = ;
c = ;
d = ;
cout << << a << endl;
cout << << b << endl;
cout << << c << endl;
cout << << d << endl;
;
}
int main ()
int
10
double
3.14159
char
'Z'
bool
false
"整数:"
"浮点数:"
"字符:"
"布尔值(1=true,0=false):"
return
0
1.3 常量与指针
常量 :值不可修改,用 const 声明。
指针 :存储变量地址的变量,通过 * 访问指向的值,通过 & 获取变量地址。
#include <iostream>
using namespace std;
int main () {
const int MAX_AGE = 120 ;
int num = 100 ;
int * p = #
cout << "num 的值:" << num << endl;
cout << "num 的地址:" << &num << endl;
cout << "指针 p 存储的地址:" << p << endl;
cout << "指针 p 指向的值:" << *p << endl;
*p = 200 ;
cout << "修改后 num 的值:" << num << endl;
return 0 ;
}
二、控制流:程序的执行逻辑
2.1 条件语句(if-else/switch) #include <iostream>
using namespace std;
int main () {
int score = 85 ;
if (score >= 90 ) {
cout << "优秀" << endl;
} else if (score >= 60 ) {
cout << "及格" << endl;
} else {
cout << "不及格" << endl;
}
char grade = 'B' ;
switch (grade) {
case 'A' : cout << "90-100 分" ; break ;
case 'B' : cout << "80-89 分" ; break ;
case 'C' : cout << "60-79 分" ; break ;
default : cout << "不及格" ;
}
return 0 ;
}
2.2 循环语句(for/while/do-while) #include <iostream>
using namespace std;
int main () {
cout << "for 循环:" ;
for (int i = 1 ; i <= 5 ; i++) {
cout << i << " " ;
}
cout << endl;
cout << "while 循环求和:" ;
int sum = 0 , n = 1 ;
while (n <= 10 ) {
sum += n;
n++;
}
cout << sum << endl;
cout << "do-while 循环:" ;
int m = 1 ;
do {
cout << m << " " ;
m++;
} while (m < 1 );
return 0 ;
}
2.3 跳转语句(break/continue/return) #include <iostream>
using namespace std;
int main () {
cout << "break 示例:" ;
for (int i = 1 ; i <= 10 ; i++) {
if (i == 5 ) break ;
cout << i << " " ;
}
cout << endl;
cout << "continue 示例:" ;
for (int i = 1 ; i <= 5 ; i++) {
if (i == 3 ) continue ;
cout << i << " " ;
}
return 0 ;
}
三、函数:代码的封装与复用
3.1 函数的定义与调用 函数由'返回类型、函数名、参数列表、函数体'组成,用于封装特定功能。
#include <iostream>
using namespace std;
int add (int a, int b) {
return a + b;
}
void printHello () {
cout << "Hello from function!" << endl;
}
int main () {
printHello ();
int x = 10 , y = 20 ;
int result = add (x, y);
cout << "10 + 20 = " << result << endl;
return 0 ;
}
3.2 函数重载(同一函数名,不同参数) C++ 允许同一作用域内定义多个同名函数,只要参数列表(类型 / 数量 / 顺序)不同。
#include <iostream>
using namespace std;
int add (int a, int b) {
return a + b;
}
int add (int a, int b, int c) {
return a + b + c;
}
double add (double a, double b) {
return a + b;
}
int main () {
cout << add (1 , 2 ) << endl;
cout << add (1 , 2 , 3 ) << endl;
cout << add (1.5 , 2.5 ) << endl;
return 0 ;
}
3.3 递归函数(函数调用自身) 递归是解决分治问题的常用方法(如阶乘、斐波那契数列)。
#include <iostream>
using namespace std;
int factorial (int n) {
if (n == 1 ) return 1 ;
return n * factorial (n - 1 );
}
int main () {
cout << "5 的阶乘:" << factorial (5 ) << endl;
return 0 ;
}
四、数组与字符串:批量数据处理
4.1 数组(同一类型元素的集合) #include <iostream>
using namespace std;
int main () {
int numbers[5 ] = {10 , 20 , 30 , 40 , 50 };
cout << "第 3 个元素:" << numbers[2 ] << endl;
int sum = 0 ;
for (int i = 0 ; i < 5 ; i++) {
sum += numbers[i];
}
cout << "数组总和:" << sum << endl;
int matrix[2 ][3 ] = {{1 , 2 , 3 }, {4 , 5 , 6 }};
cout << "二维数组 [1][2]:" << matrix[1 ][2 ] << endl;
return 0 ;
}
4.2 字符串(C 风格与 C++ 风格)
C 风格字符串 :以 � 结尾的字符数组(如 char str[] = "hello")。
C++ 风格字符串 :string 类(需包含 <string>,支持动态长度和丰富操作)。
#include <iostream>
#include <string>
using namespace std;
int main () {
char cStr[] = "Hello" ;
cout << "C 风格字符串:" << cStr << endl;
string cppStr = "World" ;
cout << "C++ 字符串:" << cppStr << endl;
string fullStr = cStr + " " + cppStr;
cout << "拼接结果:" << fullStr << endl;
cout << "长度:" << fullStr.size () << endl;
if (cppStr == "World" ) {
cout << "字符串相等" << endl;
}
return 0 ;
}
五、面向对象:类与对象
5.1 类的定义与对象的创建 类是'数据(成员变量)+ 操作(成员函数)'的封装,对象是类的实例。
#include <iostream>
#include <string>
using namespace std;
class Student {
private :
string name;
int age;
public :
Student (string n, int a) {
name = n;
age = a;
}
string getName () {
return name;
}
void setAge (int a) {
if (a > 0 && a < 150 ) {
age = a;
}
}
void printInfo () {
cout << "姓名:" << name << ",年龄:" << age << endl;
}
};
int main () {
Student s ("张三" , 20 ) ;
s.printInfo ();
s.setAge (21 );
cout << "修改后姓名:" << s.getName () << endl;
s.printInfo ();
return 0 ;
}
5.2 继承(代码复用) 子类继承父类的属性和方法,并可添加新功能或重写父类方法。
#include <iostream>
using namespace std;
class Person {
protected :
string name;
int age;
public :
Person (string n, int a) : name (n), age (a) {}
void printBaseInfo () {
cout << "姓名:" << name << ",年龄:" << age << endl;
}
};
class Student : public Person {
private :
int id;
public :
Student (string n, int a, int i) : Person (n, a), id (i) {}
void printStudentInfo () {
printBaseInfo ();
cout << "学号:" << id << endl;
}
};
int main () {
Student s ("李四" , 18 , 10001 ) ;
s.printStudentInfo ();
return 0 ;
}
5.3 多态(同一接口,不同实现) 通过'虚函数'实现多态:父类指针指向子类对象时,调用的是子类重写的方法。
#include <iostream>
using namespace std;
class Shape {
public :
virtual double getArea () {
return 0 ;
}
};
class Circle : public Shape {
private :
double radius;
public :
Circle (double r) : radius (r) {}
double getArea () override {
return 3.14 * radius * radius;
}
};
class Rectangle : public Shape {
private :
double width, height;
public :
Rectangle (double w, double h) : width (w), height (h) {}
double getArea () override {
return width * height;
}
};
int main () {
Shape* shape1 = new Circle (2 );
Shape* shape2 = new Rectangle (3 , 4 );
cout << "圆面积:" << shape1->getArea () << endl;
cout << "矩形面积:" << shape2->getArea () << endl;
delete shape1;
delete shape2;
return 0 ;
}
六、STL 容器:标准模板库 STL(Standard Template Library)提供了常用数据结构(容器)和算法,无需重复造轮子。
6.1 向量(vector):动态数组 #include <iostream>
#include <vector>
using namespace std;
int main () {
vector<int > nums;
nums.push_back (10 );
nums.push_back (20 );
nums.push_back (30 );
cout << "第二个元素:" << nums[1 ] << endl;
cout << "所有元素:" ;
for (vector<int >::iterator it = nums.begin (); it != nums.end (); it++) {
cout << *it << " " ;
}
cout << endl;
cout << "元素个数:" << nums.size () << endl;
nums.clear ();
cout << "清空后个数:" << nums.size () << endl;
return 0 ;
}
6.2 映射(map):键值对集合 #include <iostream>
#include <map>
using namespace std;
int main () {
map<string, int > scoreMap;
scoreMap["张三" ] = 90 ;
scoreMap["李四" ] = 85 ;
scoreMap["王五" ] = 95 ;
cout << "张三的分数:" << scoreMap["张三" ] << endl;
cout << "所有分数:" << endl;
for (map<string, int >::iterator it = scoreMap.begin (); it != scoreMap.end (); it++) {
cout << it->first << ":" << it->second << endl;
}
return 0 ;
}
七、异常处理:程序的健壮性 通过 try-catch 捕获并处理异常,避免程序崩溃。
#include <iostream>
using namespace std;
double divide (int a, int b) {
if (b == 0 ) {
throw string ("错误:除数不能为 0!" );
}
return (double )a / b;
}
int main () {
int x = 10 , y = 0 ;
try {
double result = divide (x, y);
cout << "结果:" << result << endl;
} catch (string errorMsg) {
cout << "捕获到异常:" << errorMsg << endl;
}
return 0 ;
}
八、总结:C++ 学习路径
基础阶段 :掌握变量、函数、数组、指针等语法,能编写简单程序。
面向对象阶段 :理解类、继承、多态,掌握封装思想。
STL 阶段 :熟练使用 vector、map 等容器,提升开发效率。
进阶阶段 :学习模板、智能指针、多线程等高级特性,结合项目实战(如控制台小游戏、数据结构实现)。
C++ 的灵活性带来了学习难度,但也使其成为'接近系统底层'的强大工具。多写代码、调试报错、阅读开源项目(如 STL 源码)是提升的关键。
相关免费在线工具 加密/解密文本 使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
Gemini 图片去水印 基于开源反向 Alpha 混合算法去除 Gemini/Nano Banana 图片水印,支持批量处理与下载。 在线工具,Gemini 图片去水印在线工具,online
Base64 字符串编码/解码 将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
Base64 文件转换器 将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
Markdown转HTML 将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online
HTML转Markdown 将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online