高频关键字、函数、容器、智能指针和算法例子
1. 关键字(Keywords)
(1) const
// 1. 常量变量
const int MAX_SIZE = 100; // 不可修改// 2. 常量引用参数(避免拷贝+防止修改)
void print(const std::string& s) {std::cout << s; // s 不能被修改
}// 3. 常量成员函数
class Circle {double radius;
public:double getRadius() const { // 承诺不修改对象状态return radius;}
};
(2) static
// 1. 静态局部变量(持久化)
void counter() {static int count = 0; // 只初始化一次count++;std::cout << count;
}// 2. 静态成员变量(类共享)
class Player {
public:static int totalPlayers; // 所有对象共享
};
int Player::totalPlayers = 0; // 必须在类外初始化
(3) virtual
/ override
/ final
class Animal {
public:virtual void speak() { // 虚函数(可被重写)std::cout << "Animal sound";}
};class Dog : public Animal {
public:void speak() override { // 显式重写std::cout << "Woof!";}
};class FinalDog : public Dog {
public:void speak() final { // 禁止进一步重写std::cout << "Final Woof!";}
};
(4) auto
和 decltype
auto x = 42; // x 推导为 int
auto s = std::string("Hello"); // s 推导为 std::stringdecltype(x) y = x; // y 的类型与 x 相同(int)
2. 关键函数/操作
(1) 类型转换
double d = 3.14;
int i = static_cast<int>(d); // 安全转换(去小数)class Base { virtual void foo() {} };
class Derived : public Base {};
Base* b = new Derived;
Derived* dd = dynamic_cast<Derived*>(b); // 安全向下转型int* p = reinterpret_cast<int*>(0x1234); // 危险!强制指针转换
(2) sizeof
和 typeid
std::cout << sizeof(int); // 输出 4(32位系统)int x;
std::cout << typeid(x).name(); // 输出 "i"(GCC 中表示 int)
3. STL 容器
(1) vector
(动态数组)
std::vector<int> v = {1, 2, 3};
v.push_back(4); // 尾部插入
v.emplace_back(5); // 更高效的插入(C++11)
for (int x : v) { // 范围for循环std::cout << x;
}
(2) map
(有序键值对)
std::map<std::string, int> ages = {{"Alice", 25}, {"Bob", 30}};
ages["Charlie"] = 28; // 插入或修改
if (ages.find("Alice") != ages.end()) { // 查找std::cout << ages["Alice"];
}
(3) unordered_map
(哈希表)
std::unordered_map<std::string, int> cache;
cache["key1"] = 42; // O(1) 插入
std::cout << cache.bucket_count(); // 哈希桶数量
4. 智能指针
(1) unique_ptr
(独占所有权)
std::unique_ptr<int> ptr = std::make_unique<int>(42); // C++14
// auto ptr = std::make_unique<int>(42); // 简化写法
std::cout << *ptr; // 解引用
(2) shared_ptr
(共享所有权)
auto ptr1 = std::make_shared<int>(42);
auto ptr2 = ptr1; // 共享所有权(引用计数+1)
std::cout << ptr1.use_count(); // 输出 2
(3) weak_ptr
(解决循环引用)
auto shared = std::make_shared<int>(42);
std::weak_ptr<int> weak = shared;
if (auto locked = weak.lock()) { // 尝试提升为 shared_ptrstd::cout << *locked;
}
5. STL 算法
(1) sort
和 find
std::vector<int> v = {3, 1, 4, 1, 5};
std::sort(v.begin(), v.end()); // 排序 {1, 1, 3, 4, 5}auto it = std::find(v.begin(), v.end(), 4); // 查找元素
if (it != v.end()) {std::cout << "Found at position: " << it - v.begin();
}
(2) accumulate
(累加)
std::vector<int> nums = {1, 2, 3, 4};
int sum = std::accumulate(nums.begin(), nums.end(), 0); // 输出 10
6. 完整示例:综合使用
#include <iostream>
#include <vector>
#include <algorithm>
#include <memory>int main() {// 1. vector + sortstd::vector<int> v = {5, 3, 1, 4, 2};std::sort(v.begin(), v.end());// 2. map 查找std::map<std::string, int> m = {{"A", 1}, {"B", 2}};std::cout << m["A"] << std::endl;// 3. 智能指针auto ptr = std::make_unique<int>(42);std::cout << *ptr << std::endl;return 0;
}
总结
- 关键字:
const
保护数据,static
控制生命周期,virtual
实现多态。 - 容器:
vector
用于动态数组,map
用于有序键值对,unordered_map
用于快速查找。 - 智能指针:优先用
unique_ptr
,共享用shared_ptr
,循环引用用weak_ptr
。 - 算法:
sort
、find
、accumulate
是高频操作。
通过具体代码实践,能更直观理解这些核心概念!