Skip to content

Latest commit

History

History
1431 lines (1128 loc) · 31.6 KB

File metadata and controls

1431 lines (1128 loc) · 31.6 KB

C++ 备忘清单

提供基本语法和方法的 C++ 快速参考备忘单

入门

hello.cpp

#include<iostream>intmain() {
std::cout << "Hello Quick Reference\n";
return0;
}

编译运行

$ g++ hello.cpp -o hello
$ ./hello
Hello Quick Reference

变量

int number = 5; // 整数float f = 0.95; // 浮点数doublePI = 3.14159; // 浮点数char yes = 'Y'; // 字符
std::string s = "ME"; // 字符串(文本)bool isRight = true; // 布尔值// 常量constfloatRATE = 0.8;

int age {25}; // 自 C++11
std::cout << age; // 打印 25

原始数据类型

数据类型大小范围
int4 bytes-231 到 231-1
float4 bytesN/A
double8 bytesN/A
char1 byte-128 到 127
bool1 bytetrue / false
voidN/AN/A
wchar_t2 到 4 bytes1 个宽字符

用户输入

int num;
std::cout << "Type a number: ";
std::cin >> num;
std::cout << "You entered " << num;

交换

int a = 5, b = 10;
std::swap(a, b);
// 输出: a=10, b=5
std::cout << "a=" << a << ", b=" << b;
// 整数交换的奇技淫巧
(x ^= y), (y ^= x), (x ^= y);
// 注意! 以下操作会造成 undefined behavior
x ^= y ^= x ^= y;

注释

// C++中的单行注释/* 这是一个多行注释 在 C++ 中 */

If 语句

if (a == 10) {
// do something
}

查看: 条件

循环

for (int i = 0; i < 10; i++) {
std::cout << i << "\n";
}

查看: 循环 Loops

函数

#include<iostream>voidhello(); // 声明intmain() { // 主函数hello(); // 执行函数
}
voidhello() { // 定义
std::cout << "Hello Quick Reference!\n";
}

查看: 函数 Functions

引用

int i = 1;
int& ri = i; // ri 是对 i 的引用
ri = 2; // i 现在改为 2
std::cout << "i=" << i;
i = 3; // i 现在改为 3
std::cout << "ri=" << ri;

rii 指的是相同的内存位置

命名空间

#include<iostream>namespacens1 {intval(){return5;}}
intmain()
{
std::cout << ns1::val();
}

#include<iostream>namespacens1 {intval(){return5;}}
usingnamespacens1;usingnamespacestd;intmain()
{
cout << val();
}

名称空间允许名称下的全局标识符

C++ 数组

定义

std::array<int, 3> marks; // 定义
marks[0] = 92;
marks[1] = 97;
marks[2] = 98;
// 定义和初始化
std::array<int, 3> marks = {92, 97, 98};
// 有空成员
std::array<int, 3> marks = {92, 97};
std::cout << marks[2]; // 输出: 0

操控

┌─────┬─────┬─────┬─────┬─────┬─────┐
| 92 | 97 | 98 | 99 | 98 | 94 |
└─────┴─────┴─────┴─────┴─────┴─────┘
012345

std::array<int, 6> marks = {
92, 97, 98, 99, 98, 94
};
// 打印第一个元素
std::cout << marks[0];
// 将第 2 个元素更改为 99
marks[1] = 99;
// 从用户那里获取输入
std::cin >> marks[2];

展示

char ref[5] = {'R', 'e', 'f'};
// 基于范围的for循环for (constint &n : ref) {
std::cout << std::string(1, n);
}
// 传统的for循环for (int i = 0; i < sizeof(ref); ++i) {
std::cout << ref[i];
}

多维

 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

C++ 条件

If Clause

if (a == 10) {
// do something
}

int number = 16;
if (number % 2 == 0)
{
std::cout << "even";
}
else
{
std::cout << "odd";
}
// 输出: even

Else if 语句

int score = 99;
if (score == 100) {
std::cout << "Superb";
}
elseif (score >= 90) {
std::cout << "Excellent";
}
elseif (score >= 80) {
std::cout << "Very Good";
}
elseif (score >= 70) {
std::cout << "Good";
}
elseif (score >= 60)
std::cout << "OK";
else
std::cout << "What?";

运算符

关系运算符

:----
a == ba 等于 b
a != ba 不等于 b
a < ba 小于 b
a > ba 大于 b
a <= ba 小于或等于 b
a >= ba 大于或等于 b

赋值运算符

范例相当于
a += bAkaa = a + b
a -= bAkaa = a - b
a *= bAkaa = a * b
a /= bAkaa = a / b
a %= bAkaa = a % b

逻辑运算符

ExampleMeaning
exp1 && exp2Both are true (AND)
exp1 || exp2Either is true (OR)
!expexp is false (NOT)

位运算符

运算符描述
a & b按位与
a | b按位或
a ^ b按位异或
~a按位取反
a << b左移
a >> b右移

三元运算符

 ┌── True ──┐
Result = Condition ? Exp1 : Exp2;
└───── False ─────┘

int x = 3, y = 5, max;
max = (x > y) ? x : y;
// 输出: 5
std::cout << max << std::endl;

int x = 3, y = 5, max;
if (x > y) {
max = x;
} else {
max = y;
}
// 输出: 5
std::cout << max << std::endl;

switch 语句

int num = 2;
switch (num) {
case0:
std::cout << "Zero";
break;
case1:
std::cout << "One";
break;
case2:
std::cout << "Two";
break;
case3:
std::cout << "Three";
break;
default:
std::cout << "What?";
break;
}

C++ 循环

While

int i = 0;
while (i < 6) {
std::cout << i++;
}
// 输出: 012345

Do-while

int i = 1;
do {
std::cout << i++;
} while (i <= 5);
// 输出: 12345

Continue 语句

for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue;
}
std::cout << i;
} // 输出: 13579

无限循环

while (true) { // true or 1
std::cout << "无限循环";
}

for (;;) {
std::cout << "无限循环";
}

for(int i = 1; i > 0; i++) {
std::cout << "infinite loop";
}

for_each (C++11 起)

#include<iostream>intmain()
{
auto print = [](int num) {
std::cout << num << std::endl;
};
std::array<int, 4> arr = {1, 2, 3, 4};
std::for_each(arr.begin(), arr.end(), print);
return0;
}

基于范围 (C++11 起)

for (int n : {1, 2, 3, 4, 5}) {
std::cout << n << "";
}
// 输出: 1 2 3 4 5

std::string hello = "Quick Reference.ME";
for (char c: hello)
{
std::cout << c << "";
}
// 输出: Q u i c k R e f . M E

中断语句

int password, times = 0;
while (password != 1234) {
if (times++ >= 3) {
std::cout << "Locked!\n";
break;
}
std::cout << "Password: ";
std::cin >> password; // input
}

Several variations

for (int i = 0, j = 2; i < 3; i++, j--){
std::cout << "i=" << i << ",";
std::cout << "j=" << j << ";";
}
// 输出: i=0,j=2;i=1,j=1;i=2,j=0;

auto

std:: string s = "hello world";
for(auto c: s){
std:: cout << c << "";
}
// 输出: h e l l o w o r l d

C++ 函数

参数和返回

#include<iostream>intadd(int a, int b) {
return a + b;
}
intmain() {
std::cout << add(10, 20);
}

add 是一个接受 2 个整数并返回整数的函数

重载

voidfun(string a, string b) {
std::cout << a + "" + b;
}
voidfun(string a) {
std::cout << a;
}
voidfun(int a) {
std::cout << a;
}

内置函数

#include<iostream>
#include<cmath>// 导入库intmain() {
// sqrt() 来自 cmath
std::cout << sqrt(9);
}

Lambda 表达式

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
<< "";
});

C++智能指针

智能指针基础

#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;

unique_ptr

// 创建方式1:使用 make_unique (C++14)auto p1 = std::make_unique<int>(42);
// 创建方式2:直接构造
std::unique_ptr<int> p2(newint(42));
// 访问资源
std::cout << *p1 << std::endl;
*p1 = 100;
// 获取原始指针(不转移所有权)int* raw = p1.get();
// 释放所有权并返回原始指针int* released = p1.release();
// p1 现在为 nullptr// 替换管理的对象
p1.reset(newint(50));

shared_ptr

// 创建方式1:使用 make_sharedauto sp1 = std::make_shared<int>(42);
// 创建方式2:直接构造
std::shared_ptr<int> sp2(newint(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

weak_ptr

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;
}

循环引用问题

structNode {
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; }
};
// 创建循环引用voidcreateCycle() {
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,则会造成内存泄漏
}

C++多线程

多线程介绍

g++编译选项:-std=c++11。包含头文件:

  • #include <thread>:C++多线程库
  • #include <mutex>:C++互斥量库
  • #include <future>:C++异步库

线程的创建

以普通函数作为线程入口函数:

voidentry_1() { }
voidentry_2(int val) { }
std::thread my_thread_1(entry_1);
std::thread my_thread_2(entry_2, 5);

以类对象作为线程入口函数:

classEntry
{
voidoperator()() { }
voidentry_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
{
// ...
});

线程的销毁

thread my_thread;
// 阻塞
my_thread.join();
// 非阻塞
my_thread.detach();

this_thread

// 获取当前线程IDstd::this_thread::get_id();
// 使当前线程休眠一段指定时间std::this_thread::sleep_for();
// 使当前线程休眠到指定时间std::this_thread::sleep_until();
// 暂停当前线程的执行,让别的线程执行std::this_thread::yield();

#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;
voidfoo() { }
std::call_once(flag, foo);

std::condition_variable

创建条件变量

std::condition_variable cond;

等待条件变量被通知

std::unique_lock<std::mutex>
lock;
externboolpredicate();
// 调用方式 1
cond.wait(lock);
// 调用方式 2
cond.wait(lock, predicate);

  • wait不断地尝试重新获取并加锁该互斥量,如果获取不到,它就卡在这里并反复尝试重新获取,如果获取到了,执行流程就继续往下走
  • wait在获取到互斥量并加锁了互斥量之后:
    • 如果wait被提供了可调用对象,那么就执行这个可调用对象:
      • 如果返回值为false,那么wait继续加锁,直到再次被 notified
      • 如果返回值为true,那么wait返回,继续执行流程
    • 如果wait没有第二个参数,那么直接返回,继续执行

std::condition_variable::notify_one

notify_one 唤醒一个调用 wait 的线程。注意在唤醒之前要解锁,否则调用 wait 的线程也会因为无法加锁而阻塞。

std::condition_variable::notify_all

唤醒所有调用 wait 的线程。

获取线程的运行结果

#include <future>

创建异步任务

doublefunc(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:默认值,由系统自行决定

返回值的状态

让当前线程等待一段时间(等待到指定时间点),以期待返回值准备好:

externdoublefoo(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)
{
}
// 超时:尚未准备好elseif (status ==
std::future_status::timeout)
{ }
// 尚未启动: std::launch::deferredelseif (status ==
std::future_status::deferred)
{ }

多个返回值

如果要多次获取结果,可以使用std::shared_future,其会返回结果的一个拷贝

std::shared_future<T> result;

对于不可拷贝对象,可以在std::shared_future中存储对象的指针,而非指针本身。

创建线程

voidthreadFunction() {
// 线程函数体
std::cout << "From thread" << std::endl;
}
intmain() {
// 创建线程并开始执行线程函数
std::thread t(threadFunction);
// 等待线程执行完毕
t.join();
return0;
}

传递参数给线程函数

voidthreadFunction(int value) {
// 线程函数体
std::cout << "Received value: " << value << std::endl;
}
intmain() {
int data = 42;
std::thread t(threadFunction, data);
t.join();
return0;
}

使用Lambda表达式创建线程

intmain() {
int data = 42;
std::thread t([data]() {
// Lambda 表达式作为线程函数
std::cout << "Received value: " << data << std::endl;
});
t.join();
return0;
}

处理线程间的同步:

#include<mutex>
std::mutex mtx;
voidthreadFunction() {
std::lock_guard<std::mutex> lock(mtx);
std::cout << "Thread safe output." << std::endl;
}
intmain() {
std::thread t1(threadFunction);
std::thread t2(threadFunction);
t1.join();
t2.join();
return0;
}

使用std::async启动异步任务:

#include<future>inttaskFunction() {
// 异步任务return42;
}
intmain() {
// 启动异步任务
std::future<int> fut = std::async(std::launch::async, taskFunction);
// 获取异步任务的结果int result = fut.get();
std::cout << "Result: " << result << std::endl;
return0;
}

C++ 预处理器

预处理器

Includes

#include"iostream"
#include<iostream>

Defines

#defineFOO
#defineFOO"hello"
#undef FOO

If

#ifdef DEBUG
std::cout << "hi" << std::endl;
#elif defined VERBOSE
...
#else
...
#endif

Error

#if VERSION == 2.0
#error Unsupported
#warning Not really supported
#endif

#defineDEG(x) ((x) * 57.29)

令牌连接

#defineDST(name) name##_s name##_t
DST(object); #=> object_s object_t;

字符串化

#defineSTR(name) #name
char * a = STR(object); #=> char * a = "object";

文件和行

#defineLOG(msg) console.log(__FILE__, __LINE__, msg)
#=> console.log("file.txt", 3, "hey")

各种各样的

转义序列

转义序列说明
\b退格键
\f换页
\n换行
\r返回
\t水平制表符
\v垂直制表符
\\反斜杠
\'单引号
\"双引号
\?问号
\0空字符

关键字

预处理器

另见