提供基本语法和方法的 C++ 快速参考备忘单。
开始
你好。
#include <iostream> int main() { std::cout << "Hello QuickRef\n"; return 0; }
编译和运行
$ g++ hello.cpp -o hello $ ./hello Hello QuickRef
变量
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;
int age {25}; // Since C++11 std::cout << age; // Print 25
原始数据类型
数据类型 | 尺寸 | 范围 |
---|---|---|
int | 4字节 | -2 31 ~ 2 31 -1 |
float | 4字节 | 不适用 |
double | 8 字节 | 不适用 |
char | 1 字节 | -128 ~ 127 |
bool | 1 字节 | 真假 |
void | 不适用 | 不适用 |
wchar_t | 2 或 4 个字节 | 1 个宽字符 |
用户输入
int num; std::cout << "Type a number: "; std::cin >> num; std::cout << "You entered " << num;
交换
int a = 5, b = 10, temp; temp = a; a = b; b = temp; // Outputs: a=10, b=5 std::cout << "a=" << a << ", b=" << b;
注释
// A single one line comment in C++
如果不一致
if (a == 10) { // do something }
请参阅: 条件
用于循环
for (int i = 0; i < 10; i++) { std::cout << i << "\n"; }
请参阅: 循环
函数
#include <iostream> void hello(); // Declaring int main() { // main function hello(); // Calling } void hello() { // Defining std::cout << "Hello QuickRef!\n"; }
请参阅: 函数
参考
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;
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(); }
命名空间允许名称下的全局标识符
C++ 数组
声明
int marks[3]; // Declaration marks[0] = 92; marks[1] = 97; marks[2] = 98; // Declare and initialize int marks[3] = {92, 97, 98}; int marks[] = {92, 97, 98}; // With empty members int marks[3] = {92, 97}; std::cout << marks[2]; // Outputs: 0
操作
┌─────┬─────┬─────┬─────┬─────┬─────┐ | 92 | 97 | 98 | 99 | 98 | 94 | └─────┴─────┴─────┴─────┴─────┴─────┘ 0 1 2 3 4 5
int marks[6] = {92, 97, 98, 99, 98, 94}; // Print first element std::cout << marks[0]; // Change 2th element to 99 marks[1] = 99; // Take input from the user std::cin >> marks[2];
显示
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]; }
多维数组
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] << " "; } } // Outputs: 1 2 3 4 5 6 6 5 4 3 2 1
C++ 条件
如果不一致
if (a == 10) { // do something }
int number = 16; if (number % 2 == 0) { std::cout << "even"; } else { std::cout << "odd"; } // Outputs: 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?";