提供基本语法和方法的 C++ 快速参考备忘单
#include <iostream>
int main() {
std::cout << "Hello Quick Reference\n";
return 0;
}
编译运行
$ g++ hello.cpp -o hello
$ ./hello
Hello Quick Reference
int number = 5; // 整数
float f = 0.95; // 浮点数
double PI = 3.14159; // 浮点数
char yes = 'Y'; // 特点
std::string s = "ME"; // 字符串(文本)
bool isRight = true; // 布尔值
// 常量
const float RATE = 0.8;
int age {25}; // 自 C++11
std::cout << age; // 打印 25
数据类型 | 大小 | 范围 |
---|---|---|
int | 4 bytes | -231 到 231-1 |
float | 4 bytes | N/A |
double | 8 bytes | N/A |
char | 1 byte | -128 到 127 |
bool | 1 byte | true / false |
void | N/A | N/A |
wchar_t | 2 到 4 bytes | 1 个宽字符 |
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 (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
int i = 1;
int& ri = i; // ri 是对 i 的引用
ri = 2; // i 现在改为 2
std::cout << "i=" << i;
i = 3; // i 现在改为 3
std::cout << "ri=" << ri;
ri
和 i
指的是相同的内存位置
#include <iostream>
namespace ns1 {int val(){return 5;}}
int main()
{
std::cout << ns1::val();
}
#include <iostream>
namespace ns1 {int val(){return 5;}}
using namespace ns1;
using namespace std;
int main()
{
cout << val();
}
名称空间允许名称下的全局标识符
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 |
└─────┴─────┴─────┴─────┴─────┴─────┘
0 1 2 3 4 5
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 (const int &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
if (a == 10) {
// do something
}
int number = 16;
if (number % 2 == 0)
{
std::cout << "even";
}
else
{
std::cout << "odd";
}
// 输出: even
int score = 99;
if (score == 100) {
std::cout << "Superb";
}
else if (score >= 90) {
std::cout << "Excellent";
}
else if (score >= 80) {
std::cout << "Very Good";
}
else if (score >= 70) {
std::cout << "Good";
}
else if (score >= 60)
std::cout << "OK";
else
std::cout << "What?";
:-- | -- |
---|---|
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 | |
!exp | exp is false (NOT) |
Operator | Description |
---|---|
a & b | Binary AND |
`a | b` |
a ^ b | Binary XOR |
a ~ b | Binary One's Complement |
a << b | Binary Shift Left |
a >> b | Binary 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;
int num = 2;
switch (num) {
case 0:
std::cout << "Zero";
break;
case 1:
std::cout << "One";
break;
case 2:
std::cout << "Two";
break;
case 3:
std::cout << "Three";
break;
default:
std::cout << "What?";
break;
}
int i = 0;
while (i < 6) {
std::cout << i++;
}
// 输出: 012345
int i = 1;
do {
std::cout << i++;
} while (i <= 5);
// 输出: 12345
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";
}
#include <iostream>
int main()
{
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);
return 0;
}
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
}
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;
#include <iostream>
int add(int a, int b) {
return a + b;
}
int main() {
std::cout << add(10, 20);
}
add
是一个接受 2 个整数并返回整数的函数
void fun(string a, string b) {
std::cout << a + " " + b;
}
void fun(string a) {
std::cout << a;
}
void fun(int a) {
std::cout << a;
}
#include <iostream>
#include <cmath> // 导入库
int main() {
// sqrt() 来自 cmath
std::cout << sqrt(9);
}
#include "iostream"
#include <iostream>
#define FOO
#define FOO "hello"
#undef FOO
#ifdef DEBUG
console.log('hi');
#elif defined VERBOSE
...
#else
...
#endif
#if VERSION == 2.0
#error Unsupported
#warning Not really supported
#endif
#define DEG(x) ((x) * 57.29)
#define DST(name) name##_s name##_t
DST(object); #=> object_s object_t;
#define STR(name) #name
char * a = STR(object); #=> char * a = "object";
#define LOG(msg) console.log(__FILE__, __LINE__, msg)
#=> console.log("file.txt", 3, "hey")
转义序列 | 说明 |
---|---|
\b | 退格键 |
\f | 换页 |
\n | 换行 |
\r | 返回 |
\t | 水平制表符 |
\v | 垂直制表符 |
\\ | 反斜杠 |
\' | 单引号 |
\" | 双引号 |
\? | 问号 |
\0 | 空字符 |