提供基本语法和方法的 C++ 快速参考备忘单
if (a == 10) {
// do something
}
查看: 条件
for (int i = 0; i < 10; i++) {
std::cout << i << "\n";
}
查看: 循环 Loops
#include <iostream>
void hello(); // 声明
int main() { // 主函数
hello(); // 执行函数
}
void hello() { // 定义
std::cout << "Hello Quick Reference!\n";
}
查看: 函数 Functions
j0 j1 j2 j3 j4 j5
┌────┬────┬────┬────┬────┬────┐
i0 | 1 | 2 | 3 | 4 | 5 | 6 |
├────┼────┼────┼────┼────┼────┤
i1 | 6 | 5 | 4 | 3 | 2 | 1 |
└────┴────┴────┴────┴────┴────┘
int x[2][6] = {
{1,2,3,4,5,6}, {6,5,4,3,2,1}
};
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 6; ++j) {
std::cout << x[i][j] << " ";
}
}
// 输出: 1 2 3 4 5 6 6 5 4 3 2 1
| :-- | -- |
|---|---|
a == b | a 等于 b |
a != b | a 不等于 b |
a < b | a 小于 b |
a > b | a 大于 b |
a <= b | a 小于或等于 b |
a >= b | a 大于或等于 b |
| 范例 | 相当于 |
|---|---|
a += b | Aka a = a + b |
a -= b | Aka a = a - b |
a *= b | Aka a = a * b |
a /= b | Aka a = a / b |
a %= b | Aka a = a % b |
| Example | Meaning |
|---|---|
exp1 && exp2 | Both are true (AND) |
exp1 || exp2 | Either is true (OR) |
!exp | exp is false (NOT) |
| 运算符 | 描述 |
|---|---|
a & b | 按位与 |
a | b | 按位或 |
a ^ b | 按位异或 |
~a | 按位取反 |
a << b | 左移 |
a >> b | 右移 |
Lambda 表达式可以在函数内定义,可以理解为在函数内定义的临时函数。格式:
auto func = []() -> return_type { };
[]为捕获列表,能够捕获其所在函数的局部变量
一个空的捕获列表代表Lambda表达式不捕获任何的变量
对于值捕获,直接在中括号中填写要捕获的变量即可:
int val = 5;
auto func = [val]() -> return_type { };
对于引用捕获,需要在捕获的变量前添加&:
string str("hello world!");
auto func = [&str]() -> return_type { };
如果变量太多,需要编译器根据我们编写的代码自动捕获,可以采用隐式捕获的方式。
全部值捕获:
int val1, val2;
auto func = [=]() -> int
{
return val1 + val2;
};
全部引用捕获:
string str1("hello"), str2("word!");
auto func = [&]() -> string
{
return str1 + str2;
};
混合隐式捕获:
如果希望对一部分变量采用值捕获,对其他变量采用引用捕获,可以混合使用:
int val1 = 123, val2 = 456;
string str1("123"), str2(456);
auto func1 = [=, &str1]() -> int
{
return val1 == std::stoi(str1)
? val1 : val2;
};
auto func2 = [&, val1]() -> string
{
return str1 == std::to_string(val1)
? str1 : str2;
};
() 是参数列表,我们只需要按照普通函数的使用方法来使用即可
return_type 是函数的返回类型,-> return_type 可以不写,编译器会自动推导
{} 中的内容就是函数体,依照普通函数的使用方法使用即可
此处给出一个 Lambda 表达式的实际使用例子(当然可以使用 str::copy):
// vec中包含1, 2, 3, 4, 5
std::vector<int> vec({1, 2, 3, 4, 5});
std::for_each(vec.begin(), vec.end(),
[](int& ele) -> void
{
std::cout << ele
<< " ";
});
#include <memory>
// 创建独占所有权的指针
std::unique_ptr<int> p1 = std::make_unique<int>(42);
// 不能复制,只能移动
std::unique_ptr<int> p2 = std::move(p1);
// p1 现在为 nullptr
// 创建共享所有权的指针
std::shared_ptr<int> sp1 = std::make_shared<int>(42);
// 可以复制,引用计数增加
std::shared_ptr<int> sp2 = sp1;
// 获取引用计数
std::cout << sp1.use_count(); // 输出: 2
// 创建弱引用,不增加引用计数
std::weak_ptr<int> wp = sp1;
// 创建方式1:使用 make_unique (C++14)
auto p1 = std::make_unique<int>(42);
// 创建方式2:直接构造
std::unique_ptr<int> p2(new int(42));
// 访问资源
std::cout << *p1 << std::endl;
*p1 = 100;
// 获取原始指针(不转移所有权)
int* raw = p1.get();
// 释放所有权并返回原始指针
int* released = p1.release();
// p1 现在为 nullptr
// 替换管理的对象
p1.reset(new int(50));
// 创建方式1:使用 make_shared
auto sp1 = std::make_shared<int>(42);
// 创建方式2:直接构造
std::shared_ptr<int> sp2(new int(42));
// 复制和共享所有权
std::shared_ptr<int> sp3 = sp1;
std::cout << sp1.use_count(); // 输出: 2
// 访问资源
std::cout << *sp1 << std::endl;
*sp1 = 100; // 所有指向该资源的shared_ptr都会看到这个修改
// 重置指针
sp1.reset(); // sp1变为nullptr,引用计数减1
std::shared_ptr<int> sp = std::make_shared<int>(42);
std::weak_ptr<int> wp = sp;
// 检查引用对象是否存在
if (auto locked = wp.lock()) {
std::cout << *locked << std::endl; // 输出: 42
} else {
std::cout << "对象已被销毁" << std::endl;
}
// 检查是否过期
bool is_expired = wp.expired(); // false
// 获取引用计数
std::cout << wp.use_count(); // 输出: 1
// 当所有shared_ptr都被销毁时
sp.reset();
if (wp.expired()) {
std::cout << "对象已被销毁" << std::endl;
}
struct Node {
std::string name;
std::shared_ptr<Node> next;
// 使用weak_ptr避免循环引用
std::weak_ptr<Node> parent;
Node(const std::string& n) : name(n) {}
~Node() { std::cout << "销毁: " << name << std::endl; }
};
// 创建循环引用
void createCycle() {
auto node1 = std::make_shared<Node>("Node1");
auto node2 = std::make_shared<Node>("Node2");
node1->next = node2;
node2->parent = node1; // 使用weak_ptr避免循环引用
// 函数结束时,node1和node2会被正确销毁
// 如果parent也是shared_ptr,则会造成内存泄漏
}
以普通函数作为线程入口函数:
void entry_1() { }
void entry_2(int val) { }
std::thread my_thread_1(entry_1);
std::thread my_thread_2(entry_2, 5);
以类对象作为线程入口函数:
class Entry
{
void operator()() { }
void entry_function() { }
};
Entry entry;
// 调用operator()()
std::thread my_thread_1(entry);
// 调用Entry::entry_function
std::thread my_thread_2(&Entry::entry_function, &entry);
以lambda表达式作为线程入口函数:
std::thread my_thread([]() -> void
{
// ...
});
#include <mutex>
创建锁
std::mutex m;
上锁
m.lock();
解锁
m.unlock();
尝试上锁:成功返回true,失败返回false
m.try_lock();
解锁
m.unlock();
std::lock_guard<Mutex>构造时上锁,析构时解锁
std::mutex m;
std::lock_guard<std::mutex> lock(m);
额外参数:std::adopt_lock:只需解锁,无需上锁
// 手动上锁
m.lock();
std::lock_guard<mutex> lock(m,
std::adopt_lock);
unique_lock<Mutex>构造上锁,析构解锁
std::mutex m;
std::unique_lock<mutex> lock(m);
std::adopt_lock只需解锁,无需上锁
// 手动上锁
m.lock();
std::unique_lock<mutex> lock(m,
std::adopt_lock);
std::try_to_lock尝试上锁,可以通过std::unique_lock<Mutex>::owns_lock()查看状态
std::unique_lock<mutex> lock(m,
std::try_to_lock);
if (lock.owns_lock())
{
// 拿到了锁
}
else
{
// 没有
}
std::defer_lock绑定锁,但不上锁
std::unique_lock<mutex> lock(m,
std::defer_lock);
lock.lock();
lock.unlock();
std::unique_lock<Mutex>::release返回所管理的mutex对象指针,**释放所有权。**一旦释放了所有权,那么如果原来互斥量处于互斥状态,程序员有责任手动解锁。
std::call_once当多个线程通过这个函数调用一个可调用对象时,只会有一个线程成功调用。
std::once_flag flag;
void foo() { }
std::call_once(flag, foo);
std::condition_variablestd::condition_variable cond;
std::unique_lock<std::mutex>
lock;
extern bool predicate();
// 调用方式 1
cond.wait(lock);
// 调用方式 2
cond.wait(lock, predicate);
wait不断地尝试重新获取并加锁该互斥量,如果获取不到,它就卡在这里并反复尝试重新获取,如果获取到了,执行流程就继续往下走wait在获取到互斥量并加锁了互斥量之后:
wait被提供了可调用对象,那么就执行这个可调用对象:
false,那么wait继续加锁,直到再次被 notifiedtrue,那么wait返回,继续执行流程wait没有第二个参数,那么直接返回,继续执行std::condition_variable::notify_onenotify_one 唤醒一个调用 wait 的线程。注意在唤醒之前要解锁,否则调用 wait 的线程也会因为无法加锁而阻塞。
std::condition_variable::notify_all唤醒所有调用 wait 的线程。
#include <future>
double func(int val);
// 使用std::async创建异步任务
// 使用std::future获取结果
// future模板中存放返回值类型
std::future<double> result =
std::async(func, 5);
等待异步任务结束,但是不获取返回值:
result.wait();
获取异步任务的返回值:
int val = result.get();
注:
get()返回右值,因此只可调用一次std::async 的额外参数额外参数可以被放在 std::async 的第一个参数位置,用于设定 std::async 的行为:
std::launch::deferred:入口函数的运行会被推迟到std::future<T>::get()或者std::future<T>::wait()被调用时。此时调用线程会直接运行线程入口函数,换言之,不会创建子线程std::launch::async:立即创建子线程,并运行线程入口函数std::launch::deferred | std::launch::async:默认值,由系统自行决定让当前线程等待一段时间(等待到指定时间点),以期待返回值准备好:
extern double foo(int val) {}
std::future<double> result =
std::async(foo, 5);
//返回值类型
std::future_status status;
// 等待一段时间
status = result.wait_for(
std::chrono::seconds(1)
);
// 等待到某一时间点
status = result.wait_until(
std::chrono::now() +
std::chrono::seconds(1)
);
在指定的时间过去后,可以获取等待的结果:
// 返回值已经准备好
if (status ==
std::future_status::ready)
{
}
// 超时:尚未准备好
else if (status ==
std::future_status::timeout)
{ }
// 尚未启动: std::launch::deferred
else if (status ==
std::future_status::deferred)
{ }
如果要多次获取结果,可以使用std::shared_future,其会返回结果的一个拷贝。
std::shared_future<T> result;
对于不可拷贝对象,可以在std::shared_future中存储对象的指针,而非指针本身。