掌握这些核心知识点,轻松应对90%的C++技术面试
// 1. 常量变量
const int MAX_SIZE = 100;
// 2. 常量指针与指针常量
const int* ptr1 = &var; // 指向常量的指针
int* const ptr2 = &var; // 常量指针
const int* const ptr3 = &var; // 指向常量的常量指针
// 3. 常量成员函数
class MyClass {
public:
int getValue() const; // 保证不修改成员变量
};
// 1. 静态局部变量
void func() {
static int count = 0; // 只初始化一次
count++;
}
// 2. 静态成员变量
class MyClass {
public:
static int sharedValue; // 所有实例共享
};
// 3. 静态成员函数
class MyClass {
public:
static void utilityFunc(); // 不依赖于特定实例
};
// 4. 静态全局变量
static int fileScopeVar; // 文件作用域
class Person {
public:
// 构造函数
Person() : age(0) {} // 默认构造
Person(int a) : age(a) {} // 参数化构造
Person(const Person& other) : age(other.age) {} // 拷贝构造
// 析构函数
~Person() {
// 资源清理代码
}
private:
int age;
};
class Shape {
public:
virtual void draw() const = 0; // 纯虚函数
virtual ~Shape() {} // 虚析构函数
};
class Circle : public Shape {
public:
void draw() const override { // 重写虚函数
cout << "Drawing Circle" << endl;
}
};
// 使用多态
Shape* shape = new Circle();
shape->draw(); // 输出 "Drawing Circle"
delete shape;
特性 |
栈内存 |
堆内存 |
分配方式 |
自动分配/释放 |
手动分配/释放 |
大小限制 |
较小(通常几MB) |
较大(受系统内存限制) |
访问速度 |
快 |
慢 |
碎片问题 |
无 |
可能产生碎片 |
作用域 |
局部作用域 |
全局作用域 |
#include
// 1. unique_ptr: 独占所有权
std::unique_ptr uptr(new int(10));
// 2. shared_ptr: 共享所有权
std::shared_ptr sptr1 = std::make_shared(20);
std::shared_ptr sptr2 = sptr1; // 共享所有权
// 3. weak_ptr: 解决循环引用
std::weak_ptr wptr = sptr1;
if(auto temp = wptr.lock()) {
// 使用temp访问资源
}
template
T max(T a, T b) {
return (a > b) ? a : b;
}
// 使用
int i = max(10, 5); // T 推导为 int
double d = max(3.14, 2.72); // T 推导为 double
template
class Box {
public:
void setContent(T t) { content = t; }
T getContent() { return content; }
private:
T content;
};
// 特化版本
template <>
class Box {
public:
void setContent(char c) { content = c; }
char getContent() { return content; }
private:
char content;
};
容器 |
特点 |
适用场景 |
vector |
动态数组,随机访问快 |
需要频繁随机访问 |
list |
双向链表,插入删除快 |
需要频繁插入删除 |
map |
红黑树实现,键值对有序 |
需要有序键值对存储 |
unordered_map |
哈希表实现,查找O(1) |
需要快速查找 |
deque |
双端队列,两端操作高效 |
需要双端操作 |
#include
#include
vector vec = {5, 3, 1, 4, 2};
// 排序
sort(vec.begin(), vec.end()); // {1, 2, 3, 4, 5}
// 查找
auto it = find(vec.begin(), vec.end(), 3);
if(it != vec.end()) {
// 找到元素
}
// 遍历操作
for_each(vec.begin(), vec.end(), [ ](int n) {
cout << n << " ";
});
class ResourceHolder {
public:
// 移动构造函数
ResourceHolder(ResourceHolder&& other) noexcept
: resource(other.resource) {
other.resource = nullptr;
}
// 移动赋值运算符
ResourceHolder& operator=(ResourceHolder&& other) noexcept {
if(this != &other) {
delete resource;
resource = other.resource;
other.resource = nullptr;
}
return *this;
}
private:
Resource* resource;
};
// 基本语法
auto func = [capture](parameters) -> return_type {
// 函数体
};
// 示例
vector numbers = {1, 2, 3, 4, 5};
int threshold = 3;
// 使用lambda过滤大于threshold的数
auto it = remove_if(numbers.begin(), numbers.end(),
[threshold](int n) {
return n > threshold;
}
);
// 带捕获的lambda
int a = 10;
auto add = [a](int b) { return a + b; };
cout << add(5); // 输出15
C++通过虚函数表(vtable)实现多态:
每个包含虚函数的类都有一个虚函数表
对象中包含指向虚函数表的指针(vptr)
调用虚函数时通过vptr找到对应的函数地址
class Base {
public:
virtual void func1() {}
virtual void func2() {}
};
class Derived : public Base {
public:
void func1() override {}
void func3() {}
};
// 虚函数表示例:
// Base vtable: [&Base::func1, &Base::func2]
// Derived vtable: [&Derived::func1, &Base::func2]
class B; // 前向声明
class A {
public:
shared_ptr b_ptr;
};
class B {
public:
shared_ptr a_ptr;
};
void createCycle() {
auto a = make_shared();
auto b = make_shared();
a->b_ptr = b;
b->a_ptr = a; // 循环引用,内存泄漏!
}
// 解决方案:使用weak_ptr打破循环
class B {
public:
weak_ptr a_ptr; // 改为weak_ptr
};
// 定义概念
template
concept Integral = std::is_integral_v;
// 使用概念约束模板
template
T add(T a, T b) {
return a + b;
}
// 编译时检查
add(5, 3); // OK
add(2.5, 1.2); // 编译错误
#include
#include
#include
vector nums = {1, 2, 3, 4, 5};
// 使用管道操作符组合操作
auto result = nums | views::filter([ ](int n){ return n % 2 == 0; })
| views::transform([ ](int n){ return n * 2; });
// 输出: 4 8
for(int n : result) {
cout << n << " ";
}
基础为王:牢固掌握指针、内存管理、面向对象基础
理解原理:深入理解虚函数、模板、STL实现机制
现代特性:熟练掌握C++11/14/17核心特性
实践至上:通过实际项目加深理解
阅读源码:学习优秀开源项目(如STL实现)的代码
C++学习是一个持续的过程,每天进步一点点,坚持带来大改变!
推荐学习资源:
《Effective C++》系列
《C++ Primer》
CppReference.com
Leetcode C++练习
掌握这些核心知识点,相信你能在C++面试中游刃有余!