2021-06-09 16:44:22 +08:00
|
|
|
---
|
|
|
|
|
title: C++
|
|
|
|
|
date: 2021-06-01 11:51:44
|
2021-12-15 10:48:31 +08:00
|
|
|
background: bg-[#6d94c7]
|
2021-06-09 16:44:22 +08:00
|
|
|
tags:
|
|
|
|
|
categories:
|
|
|
|
|
- Programming
|
|
|
|
|
intro: |
|
2024-05-16 03:25:36 -07:00
|
|
|
C++ quick reference cheat sheet that provides basic syntax and methods.
|
2023-03-06 16:19:19 +08:00
|
|
|
plugins:
|
2024-05-16 03:25:36 -07:00
|
|
|
- copyCode
|
2025-01-05 20:10:05 +02:00
|
|
|
- runCode
|
2021-06-09 16:44:22 +08:00
|
|
|
---
|
|
|
|
|
|
2024-05-16 03:25:36 -07:00
|
|
|
## Getting Started
|
2021-06-09 16:44:22 +08:00
|
|
|
|
|
|
|
|
### hello.cpp
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
int main() {
|
2023-06-15 10:37:46 +08:00
|
|
|
std::cout << "Hello CheatSheets\n";
|
2021-06-09 16:44:22 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
```
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
Compiling and running
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```shell script
|
|
|
|
|
$ g++ hello.cpp -o hello
|
|
|
|
|
$ ./hello
|
2023-06-15 10:37:46 +08:00
|
|
|
Hello CheatSheets
|
2021-06-09 16:44:22 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Variables
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
int number = 5; // Integer
|
|
|
|
|
float f = 0.95; // Floating number
|
|
|
|
|
double PI = 3.14159; // Floating number
|
|
|
|
|
char yes = 'Y'; // Character
|
|
|
|
|
std::string s = "ME"; // String (text)
|
|
|
|
|
bool isRight = true; // Boolean
|
|
|
|
|
|
|
|
|
|
// Constants
|
|
|
|
|
const float RATE = 0.8;
|
|
|
|
|
```
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2024-05-16 03:25:36 -07:00
|
|
|
---
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
int age {25}; // Since C++11
|
|
|
|
|
std::cout << age; // Print 25
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-15 12:26:19 +08:00
|
|
|
### Primitive Data Types
|
2021-06-09 16:44:22 +08:00
|
|
|
|
2021-09-14 13:03:55 +08:00
|
|
|
| Data Type | Size | Range |
|
2024-05-16 03:25:36 -07:00
|
|
|
| --------- | -------------- | ------------------- |
|
2021-09-14 13:03:55 +08:00
|
|
|
| `int` | 4 bytes | -2^31^ ^to^ 2^31^-1 |
|
|
|
|
|
| `float` | 4 bytes | _N/A_ |
|
|
|
|
|
| `double` | 8 bytes | _N/A_ |
|
|
|
|
|
| `char` | 1 byte | -128 ^to^ 127 |
|
|
|
|
|
| `bool` | 1 byte | true / false |
|
|
|
|
|
| `void` | _N/A_ | _N/A_ |
|
|
|
|
|
| `wchar_t` | 2 ^or^ 4 bytes | 1 wide character |
|
2024-05-16 03:25:36 -07:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
{.show-header}
|
|
|
|
|
|
|
|
|
|
### User Input
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
int num;
|
|
|
|
|
|
|
|
|
|
std::cout << "Type a number: ";
|
|
|
|
|
std::cin >> num;
|
|
|
|
|
|
|
|
|
|
std::cout << "You entered " << num;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Swap
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
2022-08-10 22:52:07 +08:00
|
|
|
int a = 5, b = 10;
|
|
|
|
|
std::swap(a, b);
|
2021-06-09 16:44:22 +08:00
|
|
|
|
|
|
|
|
// Outputs: a=10, b=5
|
|
|
|
|
std::cout << "a=" << a << ", b=" << b;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Comments
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
// A single one line comment in C++
|
|
|
|
|
|
|
|
|
|
/* This is a multiple line comment
|
|
|
|
|
in C++ */
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### If statement
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
if (a == 10) {
|
|
|
|
|
// do something
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-15 12:26:19 +08:00
|
|
|
See: [Conditionals](#c-conditionals)
|
2021-06-09 16:44:22 +08:00
|
|
|
|
|
|
|
|
### Loops
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
|
std::cout << i << "\n";
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-15 12:26:19 +08:00
|
|
|
See: [Loops](#c-loops)
|
2021-06-09 16:44:22 +08:00
|
|
|
|
|
|
|
|
### Functions
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
#include <iostream>
|
2024-05-16 03:25:36 -07:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
void hello(); // Declaring
|
2024-05-16 03:25:36 -07:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
int main() { // main function
|
|
|
|
|
hello(); // Calling
|
|
|
|
|
}
|
2024-05-16 03:25:36 -07:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
void hello() { // Defining
|
2023-06-15 10:37:46 +08:00
|
|
|
std::cout << "Hello CheatSheets!\n";
|
2021-06-09 16:44:22 +08:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-15 12:26:19 +08:00
|
|
|
See: [Functions](#c-functions)
|
2021-06-09 16:44:22 +08:00
|
|
|
|
|
|
|
|
### References
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
int i = 1;
|
|
|
|
|
int& ri = i; // ri is a reference to i
|
|
|
|
|
|
|
|
|
|
ri = 2; // i is now changed to 2
|
|
|
|
|
std::cout << "i=" << i;
|
|
|
|
|
|
|
|
|
|
i = 3; // i is now changed to 3
|
|
|
|
|
std::cout << "ri=" << ri;
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-15 12:26:19 +08:00
|
|
|
`ri` and `i` refer to the same memory location.
|
2021-06-09 16:44:22 +08:00
|
|
|
|
2021-06-29 21:37:28 +08:00
|
|
|
### Namespaces
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-29 21:37:28 +08:00
|
|
|
```cpp
|
|
|
|
|
#include <iostream>
|
|
|
|
|
namespace ns1 {int val(){return 5;}}
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
std::cout << ns1::val();
|
|
|
|
|
}
|
2021-06-09 16:44:22 +08:00
|
|
|
```
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-29 21:37:28 +08:00
|
|
|
---
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
2021-06-29 21:37:28 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
namespace ns1 {int val(){return 5;}}
|
|
|
|
|
using namespace ns1;
|
|
|
|
|
using namespace std;
|
|
|
|
|
int main()
|
|
|
|
|
{
|
2024-05-16 03:25:36 -07:00
|
|
|
cout << val();
|
2021-06-29 21:37:28 +08:00
|
|
|
}
|
2021-06-09 16:44:22 +08:00
|
|
|
```
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-29 21:37:28 +08:00
|
|
|
Namespaces allow global identifiers under a name
|
2021-06-09 16:44:22 +08:00
|
|
|
|
2024-05-16 03:25:36 -07:00
|
|
|
## C++ Arrays
|
2021-06-09 16:44:22 +08:00
|
|
|
|
|
|
|
|
### Declaration
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
2022-08-15 12:27:22 +08:00
|
|
|
std::array<int, 3> marks; // Definition
|
2021-06-09 16:44:22 +08:00
|
|
|
marks[0] = 92;
|
|
|
|
|
marks[1] = 97;
|
|
|
|
|
marks[2] = 98;
|
|
|
|
|
|
2022-08-15 12:27:22 +08:00
|
|
|
// Define and initialize
|
|
|
|
|
std::array<int, 3> = {92, 97, 98};
|
2021-06-09 16:44:22 +08:00
|
|
|
|
|
|
|
|
// With empty members
|
2022-08-15 12:27:22 +08:00
|
|
|
std::array<int, 3> marks = {92, 97};
|
2021-06-09 16:44:22 +08:00
|
|
|
std::cout << marks[2]; // Outputs: 0
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Manipulation
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
┌─────┬─────┬─────┬─────┬─────┬─────┐
|
|
|
|
|
| 92 | 97 | 98 | 99 | 98 | 94 |
|
|
|
|
|
└─────┴─────┴─────┴─────┴─────┴─────┘
|
|
|
|
|
0 1 2 3 4 5
|
|
|
|
|
```
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
---
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
2022-08-15 12:27:22 +08:00
|
|
|
std::array<int, 6> marks = {92, 97, 98, 99, 98, 94};
|
2021-06-09 16:44:22 +08:00
|
|
|
|
|
|
|
|
// Print first element
|
|
|
|
|
std::cout << marks[0];
|
|
|
|
|
|
2023-05-26 15:37:59 +09:30
|
|
|
// Change 2nd element to 99
|
2021-06-09 16:44:22 +08:00
|
|
|
marks[1] = 99;
|
|
|
|
|
|
|
|
|
|
// Take input from the user
|
|
|
|
|
std::cin >> marks[2];
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Displaying
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
char ref[5] = {'R', 'e', 'f'};
|
|
|
|
|
|
|
|
|
|
// Range based for loop
|
|
|
|
|
for (const int &n : ref) {
|
|
|
|
|
std::cout << std::string(1, n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Traditional for loop
|
|
|
|
|
for (int i = 0; i < sizeof(ref); ++i) {
|
|
|
|
|
std::cout << ref[i];
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Multidimensional
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
j0 j1 j2 j3 j4 j5
|
|
|
|
|
┌────┬────┬────┬────┬────┬────┐
|
|
|
|
|
i0 | 1 | 2 | 3 | 4 | 5 | 6 |
|
|
|
|
|
├────┼────┼────┼────┼────┼────┤
|
|
|
|
|
i1 | 6 | 5 | 4 | 3 | 2 | 1 |
|
|
|
|
|
└────┴────┴────┴────┴────┴────┘
|
|
|
|
|
```
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
---
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
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] << " ";
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-16 03:25:36 -07:00
|
|
|
// Outputs: 1 2 3 4 5 6 6 5 4 3 2 1
|
2021-06-09 16:44:22 +08:00
|
|
|
```
|
|
|
|
|
|
2024-05-16 03:25:36 -07:00
|
|
|
## C++ Conditionals
|
2021-06-09 16:44:22 +08:00
|
|
|
|
|
|
|
|
### If Clause
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
if (a == 10) {
|
|
|
|
|
// do something
|
|
|
|
|
}
|
|
|
|
|
```
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
---
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
int number = 16;
|
|
|
|
|
|
|
|
|
|
if (number % 2 == 0)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "even";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
std::cout << "odd";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Outputs: even
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Else if Statement
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
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?";
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Operators {.row-span-2}
|
|
|
|
|
|
|
|
|
|
#### Relational Operators
|
|
|
|
|
|
|
|
|
|
| | |
|
2024-05-16 03:25:36 -07:00
|
|
|
| -------- | ---------------------------- |
|
2021-06-09 16:44:22 +08:00
|
|
|
| `a == b` | a is equal to b |
|
|
|
|
|
| `a != b` | a is NOT equal to b |
|
|
|
|
|
| `a < b` | a is less than b |
|
|
|
|
|
| `a > b` | a is greater b |
|
|
|
|
|
| `a <= b` | a is less than or equal to b |
|
|
|
|
|
| `a >= b` | a is greater or equal to b |
|
|
|
|
|
|
|
|
|
|
#### Assignment Operators
|
|
|
|
|
|
2024-05-16 03:25:36 -07:00
|
|
|
| Example | Equivalent to |
|
|
|
|
|
| -------- | ---------------- |
|
|
|
|
|
| `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 |
|
2021-06-09 16:44:22 +08:00
|
|
|
|
|
|
|
|
#### Logical Operators
|
|
|
|
|
|
2024-05-16 03:25:36 -07:00
|
|
|
| Example | Meaning |
|
|
|
|
|
| --------------------------- | ---------------------- |
|
|
|
|
|
| `exp1 && exp2` | Both are true _(AND)_ |
|
|
|
|
|
| <code>exp1 \|\| exp2</code> | Either is true _(OR)_ |
|
|
|
|
|
| `!exp` | `exp` is false _(NOT)_ |
|
2021-06-09 16:44:22 +08:00
|
|
|
|
|
|
|
|
#### Bitwise Operators
|
|
|
|
|
|
2024-05-16 03:25:36 -07:00
|
|
|
| Operator | Description |
|
|
|
|
|
| ------------------- | ----------------------- |
|
|
|
|
|
| `a & b` | Binary AND |
|
|
|
|
|
| <code>a \| b</code> | Binary OR |
|
|
|
|
|
| `a ^ b` | Binary XOR |
|
|
|
|
|
| `~ a` | Binary One's Complement |
|
|
|
|
|
| `a << b` | Binary Shift Left |
|
|
|
|
|
| `a >> b` | Binary Shift Right |
|
2021-06-09 16:44:22 +08:00
|
|
|
|
|
|
|
|
### Ternary Operator
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```
|
|
|
|
|
┌── True ──┐
|
|
|
|
|
Result = Condition ? Exp1 : Exp2;
|
|
|
|
|
└───── False ─────┘
|
|
|
|
|
```
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
---
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
int x = 3, y = 5, max;
|
|
|
|
|
max = (x > y) ? x : y;
|
|
|
|
|
|
|
|
|
|
// Outputs: 5
|
|
|
|
|
std::cout << max << std::endl;
|
|
|
|
|
```
|
2022-08-15 12:26:19 +08:00
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
int x = 3, y = 5, max;
|
|
|
|
|
if (x > y) {
|
|
|
|
|
max = x;
|
|
|
|
|
} else {
|
|
|
|
|
max = y;
|
|
|
|
|
}
|
|
|
|
|
// Outputs: 5
|
|
|
|
|
std::cout << max << std::endl;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Switch Statement
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2024-05-16 03:25:36 -07:00
|
|
|
## C++ Loops
|
2021-06-09 16:44:22 +08:00
|
|
|
|
|
|
|
|
### While
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
int i = 0;
|
|
|
|
|
while (i < 6) {
|
|
|
|
|
std::cout << i++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Outputs: 012345
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Do-while
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
int i = 1;
|
|
|
|
|
do {
|
|
|
|
|
std::cout << i++;
|
|
|
|
|
} while (i <= 5);
|
|
|
|
|
|
|
|
|
|
// Outputs: 12345
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Continue statements
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
|
if (i % 2 == 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
std::cout << i;
|
|
|
|
|
} // Outputs: 13579
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Infinite loop
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
while (true) { // true or 1
|
|
|
|
|
std::cout << "infinite loop";
|
|
|
|
|
}
|
|
|
|
|
```
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
---
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
for (;;) {
|
|
|
|
|
std::cout << "infinite loop";
|
|
|
|
|
}
|
|
|
|
|
```
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
---
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
for(int i = 1; i > 0; i++) {
|
|
|
|
|
std::cout << "infinite loop";
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### for_each (Since C++11)
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
#include <iostream>
|
2025-01-05 20:10:05 +02:00
|
|
|
#include <array>
|
2021-06-09 16:44:22 +08:00
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
2022-08-15 12:27:22 +08:00
|
|
|
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);
|
2021-06-09 16:44:22 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Range-based (Since C++11)
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
2022-08-15 12:27:22 +08:00
|
|
|
for (int n : {1, 2, 3, 4, 5}) {
|
2021-06-09 16:44:22 +08:00
|
|
|
std::cout << n << " ";
|
|
|
|
|
}
|
|
|
|
|
// Outputs: 1 2 3 4 5
|
|
|
|
|
```
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
---
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
2023-06-15 10:37:46 +08:00
|
|
|
std::string hello = "CheatSheets.zip";
|
2021-06-09 16:44:22 +08:00
|
|
|
for (char c: hello)
|
|
|
|
|
{
|
|
|
|
|
std::cout << c << " ";
|
|
|
|
|
}
|
2024-05-16 03:25:36 -07:00
|
|
|
// Outputs: Q u i c k R e f . M E
|
2021-06-09 16:44:22 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Break statements
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
int password, times = 0;
|
|
|
|
|
while (password != 1234) {
|
|
|
|
|
if (times++ >= 3) {
|
|
|
|
|
std::cout << "Locked!\n";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
std::cout << "Password: ";
|
|
|
|
|
std::cin >> password; // input
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Several variations
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
for (int i = 0, j = 2; i < 3; i++, j--){
|
|
|
|
|
std::cout << "i=" << i << ",";
|
|
|
|
|
std::cout << "j=" << j << ";";
|
|
|
|
|
}
|
|
|
|
|
// Outputs: i=0,j=2;i=1,j=1;i=2,j=0;
|
|
|
|
|
```
|
|
|
|
|
|
2024-05-16 03:25:36 -07:00
|
|
|
## C++ Functions
|
2021-06-09 16:44:22 +08:00
|
|
|
|
|
|
|
|
### Arguments & Returns
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
int add(int a, int b) {
|
2024-05-16 03:25:36 -07:00
|
|
|
return a + b;
|
2021-06-09 16:44:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
2024-05-16 03:25:36 -07:00
|
|
|
std::cout << add(10, 20);
|
2021-06-09 16:44:22 +08:00
|
|
|
}
|
|
|
|
|
```
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-29 21:37:28 +08:00
|
|
|
`add` is a function taking 2 ints and returning int
|
2021-06-09 16:44:22 +08:00
|
|
|
|
|
|
|
|
### Overloading
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
void fun(string a, string b) {
|
|
|
|
|
std::cout << a + " " + b;
|
|
|
|
|
}
|
|
|
|
|
void fun(string a) {
|
|
|
|
|
std::cout << a;
|
|
|
|
|
}
|
|
|
|
|
void fun(int a) {
|
|
|
|
|
std::cout << a;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Built-in Functions
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <cmath> // import library
|
2024-05-16 03:25:36 -07:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
int main() {
|
|
|
|
|
// sqrt() is from cmath
|
|
|
|
|
std::cout << sqrt(9);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2024-05-16 03:25:36 -07:00
|
|
|
## C++ Classes & Objects {.cols-2}
|
2021-06-09 16:44:22 +08:00
|
|
|
|
2023-04-06 13:45:07 +08:00
|
|
|
### Defining a Class
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
2023-04-06 13:45:07 +08:00
|
|
|
class MyClass {
|
|
|
|
|
public: // Access specifier
|
|
|
|
|
int myNum; // Attribute (int variable)
|
|
|
|
|
string myString; // Attribute (string variable)
|
|
|
|
|
};
|
2021-06-09 16:44:22 +08:00
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-04-06 13:45:07 +08:00
|
|
|
### Creating an Object
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
2023-04-06 13:45:07 +08:00
|
|
|
MyClass myObj; // Create an object of MyClass
|
|
|
|
|
|
|
|
|
|
myObj.myNum = 15; // Set the value of myNum to 15
|
|
|
|
|
myObj.myString = "Hello"; // Set the value of myString to "Hello"
|
|
|
|
|
|
|
|
|
|
cout << myObj.myNum << endl; // Output 15
|
|
|
|
|
cout << myObj.myString << endl; // Output "Hello"
|
2021-06-09 16:44:22 +08:00
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-04-06 13:45:07 +08:00
|
|
|
### Constructors
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
2023-04-06 13:45:07 +08:00
|
|
|
class MyClass {
|
|
|
|
|
public:
|
|
|
|
|
int myNum;
|
|
|
|
|
string myString;
|
|
|
|
|
MyClass() { // Constructor
|
|
|
|
|
myNum = 0;
|
|
|
|
|
myString = "";
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MyClass myObj; // Create an object of MyClass
|
|
|
|
|
|
|
|
|
|
cout << myObj.myNum << endl; // Output 0
|
|
|
|
|
cout << myObj.myString << endl; // Output ""
|
2021-06-09 16:44:22 +08:00
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-04-06 13:45:07 +08:00
|
|
|
### Destructors
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
class MyClass {
|
|
|
|
|
public:
|
|
|
|
|
int myNum;
|
|
|
|
|
string myString;
|
|
|
|
|
MyClass() { // Constructor
|
|
|
|
|
myNum = 0;
|
|
|
|
|
myString = "";
|
|
|
|
|
}
|
|
|
|
|
~MyClass() { // Destructor
|
|
|
|
|
cout << "Object destroyed." << endl;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MyClass myObj; // Create an object of MyClass
|
|
|
|
|
|
|
|
|
|
// Code here...
|
|
|
|
|
|
|
|
|
|
// Object is destroyed automatically when the program exits the scope
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Class Methods
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
class MyClass {
|
|
|
|
|
public:
|
|
|
|
|
int myNum;
|
|
|
|
|
string myString;
|
|
|
|
|
void myMethod() { // Method/function defined inside the class
|
|
|
|
|
cout << "Hello World!" << endl;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MyClass myObj; // Create an object of MyClass
|
|
|
|
|
myObj.myMethod(); // Call the method
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Access Modifiers
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
class MyClass {
|
|
|
|
|
public: // Public access specifier
|
|
|
|
|
int x; // Public attribute
|
|
|
|
|
private: // Private access specifier
|
|
|
|
|
int y; // Private attribute
|
|
|
|
|
protected: // Protected access specifier
|
|
|
|
|
int z; // Protected attribute
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MyClass myObj;
|
|
|
|
|
myObj.x = 25; // Allowed (public)
|
|
|
|
|
myObj.y = 50; // Not allowed (private)
|
|
|
|
|
myObj.z = 75; // Not allowed (protected)
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Getters and Setters
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
class MyClass {
|
|
|
|
|
private:
|
|
|
|
|
int myNum;
|
|
|
|
|
public:
|
|
|
|
|
void setMyNum(int num) { // Setter
|
|
|
|
|
myNum = num;
|
|
|
|
|
}
|
|
|
|
|
int getMyNum() { // Getter
|
|
|
|
|
return myNum;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MyClass myObj;
|
|
|
|
|
myObj.setMyNum(15); // Set the value of myNum to 15
|
|
|
|
|
cout << myObj.getMyNum() << endl; // Output 15
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Inheritance
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-09 16:44:22 +08:00
|
|
|
```cpp
|
2023-04-06 13:45:07 +08:00
|
|
|
class Vehicle {
|
|
|
|
|
public:
|
|
|
|
|
string brand = "Ford";
|
|
|
|
|
void honk() {
|
|
|
|
|
cout << "Tuut, tuut!" << endl;
|
|
|
|
|
}
|
|
|
|
|
};
|
2021-06-09 16:44:22 +08:00
|
|
|
|
2023-04-06 13:45:07 +08:00
|
|
|
class Car : public Vehicle {
|
|
|
|
|
public:
|
|
|
|
|
string model = "Mustang";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Car myCar;
|
|
|
|
|
myCar.honk(); // Output "Tuut, tuut!"
|
|
|
|
|
cout << myCar.brand + " " + myCar.model << endl; // Output "Ford Mustang"
|
2021-06-09 16:44:22 +08:00
|
|
|
```
|
|
|
|
|
|
2024-05-16 03:25:36 -07:00
|
|
|
## C++ Preprocessor
|
2021-06-29 21:37:28 +08:00
|
|
|
|
|
|
|
|
### Preprocessor {.row-span-3}
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-29 21:37:28 +08:00
|
|
|
- [if](https://en.cppreference.com/w/cpp/preprocessor/conditional)
|
|
|
|
|
- [elif](https://en.cppreference.com/w/cpp/preprocessor/conditional)
|
|
|
|
|
- [else](https://en.cppreference.com/w/cpp/preprocessor/conditional)
|
|
|
|
|
- [endif](https://en.cppreference.com/w/cpp/preprocessor/conditional)
|
|
|
|
|
- [ifdef](https://en.cppreference.com/w/cpp/preprocessor/conditional)
|
|
|
|
|
- [ifndef](https://en.cppreference.com/w/cpp/preprocessor/conditional)
|
|
|
|
|
- [define](https://en.cppreference.com/w/cpp/preprocessor/replace)
|
|
|
|
|
- [undef](https://en.cppreference.com/w/cpp/preprocessor/replace)
|
|
|
|
|
- [include](https://en.cppreference.com/w/cpp/preprocessor/include)
|
|
|
|
|
- [line](https://en.cppreference.com/w/cpp/preprocessor/line)
|
|
|
|
|
- [error](https://en.cppreference.com/w/cpp/preprocessor/error)
|
|
|
|
|
- [pragma](https://en.cppreference.com/w/cpp/preprocessor/impl)
|
|
|
|
|
- [defined](https://en.cppreference.com/w/cpp/preprocessor/conditional)
|
2024-05-16 03:25:36 -07:00
|
|
|
- [\_\_has_include](https://en.cppreference.com/w/cpp/feature_test)
|
|
|
|
|
- [\_\_has_cpp_attribute](https://en.cppreference.com/w/cpp/feature_test)
|
2021-06-29 21:37:28 +08:00
|
|
|
- [export](https://en.cppreference.com/w/cpp/keyword/export)
|
2024-05-16 03:25:36 -07:00
|
|
|
- [import](https://en.cppreference.com/mwiki/index.php?title=cpp/keyword/import&action=edit&redlink=1)
|
|
|
|
|
- [module](https://en.cppreference.com/mwiki/index.php?title=cpp/keyword/module&action=edit&redlink=1)
|
|
|
|
|
|
2022-11-20 14:18:58 +08:00
|
|
|
{.marker-none .cols-2}
|
2021-06-29 21:37:28 +08:00
|
|
|
|
|
|
|
|
### Includes
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
#include "iostream"
|
|
|
|
|
#include <iostream>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Defines
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
#define FOO
|
|
|
|
|
#define FOO "hello"
|
|
|
|
|
|
|
|
|
|
#undef FOO
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### If {.row-span-2}
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-29 21:37:28 +08:00
|
|
|
```cpp
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
console.log('hi');
|
|
|
|
|
#elif defined VERBOSE
|
|
|
|
|
...
|
|
|
|
|
#else
|
|
|
|
|
...
|
|
|
|
|
#endif
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Error
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
#if VERSION == 2.0
|
|
|
|
|
#error Unsupported
|
|
|
|
|
#warning Not really supported
|
|
|
|
|
#endif
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Macro
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
#define DEG(x) ((x) * 57.29)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Token concat
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
#define DST(name) name##_s name##_t
|
|
|
|
|
DST(object); #=> object_s object_t;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Stringification
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
#define STR(name) #name
|
|
|
|
|
char * a = STR(object); #=> char * a = "object";
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### file and line
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
#define LOG(msg) console.log(__FILE__, __LINE__, msg)
|
|
|
|
|
#=> console.log("file.txt", 3, "hey")
|
|
|
|
|
```
|
2021-06-09 16:44:22 +08:00
|
|
|
|
2024-05-16 03:25:36 -07:00
|
|
|
## Miscellaneous
|
2021-06-09 16:44:22 +08:00
|
|
|
|
|
|
|
|
### Escape Sequences
|
|
|
|
|
|
|
|
|
|
| Escape Sequences | Characters |
|
2024-05-16 03:25:36 -07:00
|
|
|
| ---------------- | --------------------- |
|
2021-09-14 13:03:55 +08:00
|
|
|
| `\b` | Backspace |
|
|
|
|
|
| `\f` | Form feed |
|
|
|
|
|
| `\n` | Newline |
|
|
|
|
|
| `\r` | Return |
|
|
|
|
|
| `\t` | Horizontal tab |
|
|
|
|
|
| `\v` | Vertical tab |
|
|
|
|
|
| `\\` | Backslash |
|
|
|
|
|
| `\'` | Single quotation mark |
|
|
|
|
|
| `\"` | Double quotation mark |
|
|
|
|
|
| `\?` | Question mark |
|
|
|
|
|
| `\0` | Null Character |
|
2021-06-09 16:44:22 +08:00
|
|
|
|
2024-05-16 03:25:36 -07:00
|
|
|
### Keywords {.col-span-2 .row-span-2}
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-29 21:37:28 +08:00
|
|
|
- [alignas](https://en.cppreference.com/w/cpp/keyword/alignas)
|
|
|
|
|
- [alignof](https://en.cppreference.com/w/cpp/keyword/alignof)
|
|
|
|
|
- [and](https://en.cppreference.com/w/cpp/keyword/and)
|
|
|
|
|
- [and_eq](https://en.cppreference.com/w/cpp/keyword/and_eq)
|
|
|
|
|
- [asm](https://en.cppreference.com/w/cpp/keyword/asm)
|
|
|
|
|
- [atomic_cancel](https://en.cppreference.com/w/cpp/keyword/atomic_cancel)
|
|
|
|
|
- [atomic_commit](https://en.cppreference.com/w/cpp/keyword/atomic_commit)
|
|
|
|
|
- [atomic_noexcept](https://en.cppreference.com/w/cpp/keyword/atomic_noexcept)
|
|
|
|
|
- [auto](https://en.cppreference.com/w/cpp/keyword/auto)
|
|
|
|
|
- [bitand](https://en.cppreference.com/w/cpp/keyword/bitand)
|
|
|
|
|
- [bitor](https://en.cppreference.com/w/cpp/keyword/bitor)
|
|
|
|
|
- [bool](https://en.cppreference.com/w/cpp/keyword/bool)
|
|
|
|
|
- [break](https://en.cppreference.com/w/cpp/keyword/break)
|
|
|
|
|
- [case](https://en.cppreference.com/w/cpp/keyword/case)
|
|
|
|
|
- [catch](https://en.cppreference.com/w/cpp/keyword/catch)
|
|
|
|
|
- [char](https://en.cppreference.com/w/cpp/keyword/char)
|
|
|
|
|
- [char8_t](https://en.cppreference.com/w/cpp/keyword/char8_t)
|
|
|
|
|
- [char16_t](https://en.cppreference.com/w/cpp/keyword/char16_t)
|
|
|
|
|
- [char32_t](https://en.cppreference.com/w/cpp/keyword/char32_t)
|
|
|
|
|
- [class](https://en.cppreference.com/w/cpp/keyword/class)
|
|
|
|
|
- [compl](https://en.cppreference.com/w/cpp/keyword/compl)
|
|
|
|
|
- [concept](https://en.cppreference.com/w/cpp/keyword/concept)
|
|
|
|
|
- [const](https://en.cppreference.com/w/cpp/keyword/const)
|
|
|
|
|
- [consteval](https://en.cppreference.com/w/cpp/keyword/consteval)
|
|
|
|
|
- [constexpr](https://en.cppreference.com/w/cpp/keyword/constexpr)
|
|
|
|
|
- [constinit](https://en.cppreference.com/w/cpp/keyword/constinit)
|
|
|
|
|
- [const_cast](https://en.cppreference.com/w/cpp/keyword/const_cast)
|
|
|
|
|
- [continue](https://en.cppreference.com/w/cpp/keyword/continue)
|
|
|
|
|
- [co_await](https://en.cppreference.com/w/cpp/keyword/co_await)
|
|
|
|
|
- [co_return](https://en.cppreference.com/w/cpp/keyword/co_return)
|
|
|
|
|
- [co_yield](https://en.cppreference.com/w/cpp/keyword/co_yield)
|
|
|
|
|
- [decltype](https://en.cppreference.com/w/cpp/keyword/decltype)
|
|
|
|
|
- [default](https://en.cppreference.com/w/cpp/keyword/default)
|
|
|
|
|
- [delete](https://en.cppreference.com/w/cpp/keyword/delete)
|
|
|
|
|
- [do](https://en.cppreference.com/w/cpp/keyword/do)
|
|
|
|
|
- [double](https://en.cppreference.com/w/cpp/keyword/double)
|
|
|
|
|
- [dynamic_cast](https://en.cppreference.com/w/cpp/keyword/dynamic_cast)
|
|
|
|
|
- [else](https://en.cppreference.com/w/cpp/keyword/else)
|
|
|
|
|
- [enum](https://en.cppreference.com/w/cpp/keyword/enum)
|
|
|
|
|
- [explicit](https://en.cppreference.com/w/cpp/keyword/explicit)
|
|
|
|
|
- [export](https://en.cppreference.com/w/cpp/keyword/export)
|
|
|
|
|
- [extern](https://en.cppreference.com/w/cpp/keyword/extern)
|
|
|
|
|
- [false](https://en.cppreference.com/w/cpp/keyword/false)
|
|
|
|
|
- [float](https://en.cppreference.com/w/cpp/keyword/float)
|
|
|
|
|
- [for](https://en.cppreference.com/w/cpp/keyword/for)
|
|
|
|
|
- [friend](https://en.cppreference.com/w/cpp/keyword/friend)
|
|
|
|
|
- [goto](https://en.cppreference.com/w/cpp/keyword/goto)
|
|
|
|
|
- [if](https://en.cppreference.com/w/cpp/keyword/if)
|
|
|
|
|
- [inline](https://en.cppreference.com/w/cpp/keyword/inline)
|
|
|
|
|
- [int](https://en.cppreference.com/w/cpp/keyword/int)
|
|
|
|
|
- [long](https://en.cppreference.com/w/cpp/keyword/long)
|
|
|
|
|
- [mutable](https://en.cppreference.com/w/cpp/keyword/mutable)
|
|
|
|
|
- [namespace](https://en.cppreference.com/w/cpp/keyword/namespace)
|
|
|
|
|
- [new](https://en.cppreference.com/w/cpp/keyword/new)
|
|
|
|
|
- [noexcept](https://en.cppreference.com/w/cpp/keyword/noexcept)
|
|
|
|
|
- [not](https://en.cppreference.com/w/cpp/keyword/not)
|
|
|
|
|
- [not_eq](https://en.cppreference.com/w/cpp/keyword/not_eq)
|
|
|
|
|
- [nullptr](https://en.cppreference.com/w/cpp/keyword/nullptr)
|
|
|
|
|
- [operator](https://en.cppreference.com/w/cpp/keyword/operator)
|
|
|
|
|
- [or](https://en.cppreference.com/w/cpp/keyword/or)
|
|
|
|
|
- [or_eq](https://en.cppreference.com/w/cpp/keyword/or_eq)
|
|
|
|
|
- [private](https://en.cppreference.com/w/cpp/keyword/private)
|
|
|
|
|
- [protected](https://en.cppreference.com/w/cpp/keyword/protected)
|
|
|
|
|
- [public](https://en.cppreference.com/w/cpp/keyword/public)
|
|
|
|
|
- [reflexpr](https://en.cppreference.com/w/cpp/keyword/reflexpr)
|
|
|
|
|
- [register](https://en.cppreference.com/w/cpp/keyword/register)
|
|
|
|
|
- [reinterpret_cast](https://en.cppreference.com/w/cpp/keyword/reinterpret_cast)
|
|
|
|
|
- [requires](https://en.cppreference.com/w/cpp/keyword/requires)
|
|
|
|
|
- [return](https://en.cppreference.com/w/cpp/language/return)
|
|
|
|
|
- [short](https://en.cppreference.com/w/cpp/keyword/short)
|
|
|
|
|
- [signed](https://en.cppreference.com/w/cpp/keyword/signed)
|
|
|
|
|
- [sizeof](https://en.cppreference.com/w/cpp/keyword/sizeof)
|
|
|
|
|
- [static](https://en.cppreference.com/w/cpp/keyword/static)
|
|
|
|
|
- [static_assert](https://en.cppreference.com/w/cpp/keyword/static_assert)
|
|
|
|
|
- [static_cast](https://en.cppreference.com/w/cpp/keyword/static_cast)
|
|
|
|
|
- [struct](https://en.cppreference.com/w/cpp/keyword/struct)
|
|
|
|
|
- [switch](https://en.cppreference.com/w/cpp/keyword/switch)
|
|
|
|
|
- [synchronized](https://en.cppreference.com/w/cpp/keyword/synchronized)
|
|
|
|
|
- [template](https://en.cppreference.com/w/cpp/keyword/template)
|
|
|
|
|
- [this](https://en.cppreference.com/w/cpp/keyword/this)
|
|
|
|
|
- [thread_local](https://en.cppreference.com/w/cpp/keyword/thread_local)
|
|
|
|
|
- [throw](https://en.cppreference.com/w/cpp/keyword/throw)
|
|
|
|
|
- [true](https://en.cppreference.com/w/cpp/keyword/true)
|
|
|
|
|
- [try](https://en.cppreference.com/w/cpp/keyword/try)
|
|
|
|
|
- [typedef](https://en.cppreference.com/w/cpp/keyword/typedef)
|
|
|
|
|
- [typeid](https://en.cppreference.com/w/cpp/keyword/typeid)
|
|
|
|
|
- [typename](https://en.cppreference.com/w/cpp/keyword/typename)
|
|
|
|
|
- [union](https://en.cppreference.com/w/cpp/keyword/union)
|
|
|
|
|
- [unsigned](https://en.cppreference.com/w/cpp/keyword/unsigned)
|
|
|
|
|
- [using](https://en.cppreference.com/w/cpp/keyword/using)
|
|
|
|
|
- [virtual](https://en.cppreference.com/w/cpp/keyword/virtual)
|
|
|
|
|
- [void](https://en.cppreference.com/w/cpp/keyword/void)
|
|
|
|
|
- [volatile](https://en.cppreference.com/w/cpp/keyword/volatile)
|
|
|
|
|
- [wchar_t](https://en.cppreference.com/w/cpp/keyword/wchar_t)
|
|
|
|
|
- [while](https://en.cppreference.com/w/cpp/keyword/while)
|
|
|
|
|
- [xor](https://en.cppreference.com/w/cpp/keyword/xor)
|
|
|
|
|
- [xor_eq](https://en.cppreference.com/w/cpp/keyword/xor_eq)
|
|
|
|
|
- [final](https://en.cppreference.com/w/cpp/language/final)
|
|
|
|
|
- [override](https://en.cppreference.com/w/cpp/language/override)
|
|
|
|
|
- [transaction_safe](https://en.cppreference.com/w/cpp/language/transactional_memory)
|
2024-05-16 03:25:36 -07:00
|
|
|
- [transaction_safe_dynamic](https://en.cppreference.com/w/cpp/language/transactional_memory) {.marker-none .cols-5}
|
2021-06-29 21:37:28 +08:00
|
|
|
|
|
|
|
|
### Preprocessor
|
2022-08-15 12:26:19 +08:00
|
|
|
|
2021-06-29 21:37:28 +08:00
|
|
|
- [if](https://en.cppreference.com/w/cpp/preprocessor/conditional)
|
|
|
|
|
- [elif](https://en.cppreference.com/w/cpp/preprocessor/conditional)
|
|
|
|
|
- [else](https://en.cppreference.com/w/cpp/preprocessor/conditional)
|
|
|
|
|
- [endif](https://en.cppreference.com/w/cpp/preprocessor/conditional)
|
|
|
|
|
- [ifdef](https://en.cppreference.com/w/cpp/preprocessor/conditional)
|
|
|
|
|
- [ifndef](https://en.cppreference.com/w/cpp/preprocessor/conditional)
|
|
|
|
|
- [define](https://en.cppreference.com/w/cpp/preprocessor/replace)
|
|
|
|
|
- [undef](https://en.cppreference.com/w/cpp/preprocessor/replace)
|
|
|
|
|
- [include](https://en.cppreference.com/w/cpp/preprocessor/include)
|
|
|
|
|
- [line](https://en.cppreference.com/w/cpp/preprocessor/line)
|
|
|
|
|
- [error](https://en.cppreference.com/w/cpp/preprocessor/error)
|
|
|
|
|
- [pragma](https://en.cppreference.com/w/cpp/preprocessor/impl)
|
|
|
|
|
- [defined](https://en.cppreference.com/w/cpp/preprocessor/conditional)
|
2024-05-16 03:25:36 -07:00
|
|
|
- [\_\_has_include](https://en.cppreference.com/w/cpp/feature_test)
|
|
|
|
|
- [\_\_has_cpp_attribute](https://en.cppreference.com/w/cpp/feature_test)
|
2021-06-29 21:37:28 +08:00
|
|
|
- [export](https://en.cppreference.com/w/cpp/keyword/export)
|
2024-05-16 03:25:36 -07:00
|
|
|
- [import](https://en.cppreference.com/mwiki/index.php?title=cpp/keyword/import&action=edit&redlink=1)
|
|
|
|
|
- [module](https://en.cppreference.com/mwiki/index.php?title=cpp/keyword/module&action=edit&redlink=1) {.marker-none
|
|
|
|
|
.cols-2}
|
2021-06-09 16:44:22 +08:00
|
|
|
|
|
|
|
|
## Also see
|
|
|
|
|
|
2022-08-15 12:26:19 +08:00
|
|
|
- [C++ Infographics & Cheat Sheets](https://hackingcpp.com/cpp/cheat_sheets.html) _(hackingcpp.com)_
|
2021-06-09 16:44:22 +08:00
|
|
|
|
2022-08-15 12:26:19 +08:00
|
|
|
- [C++ reference](https://en.cppreference.com/w/) _(cppreference.com)_
|
|
|
|
|
- [C++ Language Tutorials](http://www.cplusplus.com/doc/tutorial/) _(cplusplus.com)_
|