Skip to content

Latest commit

History

History
792 lines (657 loc) · 18.9 KB

File metadata and controls

792 lines (657 loc) · 18.9 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;

注释

// 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> = {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
!expexp is false (NOT)

位运算符

OperatorDescription
a & bBinary AND
`ab`
a ^ bBinary XOR
a ~ bBinary One's Complement
a << bBinary Shift Left
a >> bBinary Shift Right

三元运算符

 ┌── 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;

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

C++ 预处理器

预处理器

Includes

#include"iostream"
#include<iostream>

Defines

#defineFOO
#defineFOO"hello"
#undef FOO

If

#ifdef DEBUG
console.log('hi');
#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空字符

关键字

预处理器

另见