初学者 C++ 速查表 🔥

2025-06-10

初学者 C++ 速查表 🔥

大家好!最近我发现了速查表的威力,于是就想做一个 C++ 速查表,因为 C++ 有个说法叫“太难了”。😞 任何语言,即使是 Python,如果你没试过,也可能觉得很难。

C++ 是 C 语言的扩展,我们之前说过 C 语言易学,但它也是一种通用编程语言。据说 Google Chromium 浏览器、微软的几款应用程序,甚至摩根士丹利的金融建模都是用 C++ 完成的。

如果你说它很难,那只有三个原因。

  • 它具有复杂的语法来支持多功能性
  • 这是一种宽容的语言——你可以做任何技术上可行的事情,即使逻辑上不正确
  • 最好由已经具备 C 语言编程基础的人来学习

好了,这些只是暂时的记录。现在忘掉所有事情,深呼吸,开始吧!这不会无聊的,我会分享一些游戏给你,让你放松一下!😊


介绍

什么是 C++?

C++ 是一种通用编程语言,它是对 C 语言的增强,包含面向对象范式。它是一种命令式语言和编译型语言。

图像.png
C++ 是一种中级语言,这使得它既适合编写低级(驱动程序、内核)程序,也适合编写高级应用程序(游戏、GUI、桌面应用程序等)。C 和 C++ 的基本语法和代码结构相同。

C++ 是如何使用的?
C++ 广泛应用于各种应用,从电脑游戏到操作系统和编译器。这种编程语言是对 C 编程语言的扩展,并包含了现代编程。C
++ 与 C 语言高度兼容,无需修改任何源代码即可编译 99% 以上的 C 程序。
从数字操作到文本操作,任何计算机能够实际执行的操作都可以使用 C++ 进行编程。


弦乐(2).png



#include <iostream>
using namespace std;

int main() {
  cout << "Hello World!";
  return 0;
}


Enter fullscreen mode Exit fullscreen mode

让我们分解代码以便更好地理解它:

  • 第 1 行-#include <iostream>是一个头文件库,用于处理输入和输出对象,例如 cout(在第 5 行使用)。头文件为 C++ 程序添加了功能。
  • 第 2 行-using namespace std表示我们可以使用标准库中的对象和变量的名称。

不用担心这些。只要把它想象成程序中(几乎)总是会出现的东西就行了。

  • 第 3 行- 空行。C++ 不关心空格。
  • 第 4 行- C++ 程序中另一个经常出现的东西是int main()。这被称为函数。其花括号内的任何代码都{}将被执行。
  • 第 5 行- cout(发音 - “see out”)是一个对象,与插入运算符 (<<) 一起使用来输出/打印文本。在我们的示例中,它将输出“Hello World”。通过发音“see out”很容易记住这一点。
  • 第 6 行——return 0结束主要功能。

⚠️注意事项:

  • C++ 中的每一行都以分号结尾;
  • 不要忘记添加右花括号}来真正结束主函数。

弦乐(5).png

  • C++ 中的注释可以是单行或多行,/**/
  • 注释也可以以 开头//,延伸至行尾。

弦乐(3).png

图像.png

图像.png

让我们进一步了解 Future 中的数据类型。😉


弦乐(4).png
变量是存储数据值的容器。

在 C++ 中,有不同类型的变量(用不同的关键字定义),例如:

  • int- 存储整数(整数),不带小数,例如 123 或 -123
  • double- 存储带有小数的浮点数,例如 19.99 或 -19.99
  • char- 存储单个字符,例如 'a' 或 'b'。字符值用单引号括起来
  • string- 存储文本,例如“Hello World”。字符串值用双引号括起来
  • float- 存储具有两种状态的布尔值:真或假

C++ 中的变量必须是指定的数据类型



int myNum = 12; // Integer (whole number without decimals)
string name = "Unity Buddy"; // String(text)
char myLetter = 'U'; //character
float myFloatNum = 5.99; // Floating point number (with decimals)


Enter fullscreen mode Exit fullscreen mode

弦乐(6).png

我们已经学习了cout“显示输出”的用法。因此,对于“获取用户输入”,我们使用“查看输出”的反义词cin,它表示“查看输入”。查看输出查看输入,很简单!我们来举个例子。



int x;  // making a variable called x.
cout << "Type a number: "; // Type a number and press enter
cin >> x; // Get user input from the keyboard
cout << "Your number is: " << x; // Display the input value


Enter fullscreen mode Exit fullscreen mode

⚠️记住,

  • cout用于显示输出并使用插入运算符(<<
  • cin用于获取用户输入并使用提取运算符(>>

字符串.png

  • 变量string包含用双引号括起来的字符集合:```c++

字符串 myName =“ Unity Buddy 先生”

To use strings, you must import `<string>` library.
```c++


#include <string>


Enter fullscreen mode Exit fullscreen mode

字符串连接

为了组合两个字符串,我们使用+符号。



string name = "Unity Buddy";
string age = "1102 years old."
cout << name + age;


Enter fullscreen mode Exit fullscreen mode

字符串长度

要查找字符串的长度,我们可以使用length()函数。(size()函数也用于同样的事情。)



string name = "Unity Buddy"
cout << "The length of your name is:" << name.length()


Enter fullscreen mode Exit fullscreen mode

字符串(1).png
C++ 支持数学中常见的逻辑条件:

  • 少于:a < b
  • 小于或等于:a <= b
  • 大于:a > b
  • 大于或等于:a >= b
  • 等于:a==b
  • 不等于:a != b

如果

在 C++ 中,我们使用 if 语句来指定在条件为真时要执行的 C++ 代码块。



if (20 > 18) {
  cout << "20 is greater than 18";
}


Enter fullscreen mode Exit fullscreen mode

别的

语句else用于指定在条件满足时要执行的代码块false



int time = 20;
if (time < 18) {
  cout << "Good day.";
} else {
  cout << "Good evening.";
}
// Outputs "Good evening."


Enter fullscreen mode Exit fullscreen mode

否则,如果

如果第一个条件为假,则使用 elseif语句指定新条件。



if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if the condition1 is false and condition2 is true
} else {
  // block of code to be executed if the condition1 is false and condition2 is false
}


Enter fullscreen mode Exit fullscreen mode

弦乐(21).png
只要满足指定条件,循环就会执行一段代码。循环非常方便,因为它可以节省时间、减少错误,并使代码更具可读性。

While 循环

只要指定条件为真, while 循环就会循环执行代码块



int main() {
  int i = 0;
  while (i < 5) {
    cout << i << "\n" ; //make new lines for each
    i++; //Add +1.
  }
  return 0;
 /* output - 1
             2
             3
             4
             5
   */                      
}
}


Enter fullscreen mode Exit fullscreen mode

Do/While 循环

图像.png
do/while 循环是 while 循环的变体。该循环会在检查条件是否为真之前执行一次代码块,然后只要条件为真,就会重复循环。



int i = 0;
do {
  cout << i << "\n";
  i++;
}
while (i < 5);


Enter fullscreen mode Exit fullscreen mode

意思是,当你阅读这篇文章时,你的知识会提高 (+1) 希望你明白了。

For 循环

当你确切知道需要循环代码块的次数时,请使用 for 循环而不是 while 循环。假设你工作到下午 12:30,并且总是工作到下午 12:30,那么每小时可以赚 50 美元。你对此很确定。所以你必须使用for循环来估算你的收入。



for (bool works = true; works = true; pay = pay+50 ) {
    cout << pay << "\n";
}
//this is not the best way to do it. Just to understand


Enter fullscreen mode Exit fullscreen mode

如果您不确定是否工作到下午 12:30,则必须使用 while 循环。



 while (bool works = true) {
    cout << pay << "\n";
    pay = pay+50;
}


Enter fullscreen mode Exit fullscreen mode

中断并继续

break语句还可用于跳出循环。



for (int i = 0; i < 10; i++) {
  if (i == 4) {
    break;
  }
  cout << i << "\n";
}


Enter fullscreen mode Exit fullscreen mode

如果发生指定的条件,该continue语句将中断一次迭代(在循环中),并继续循环中的下一次迭代。

此示例跳过值 4:



for (int i = 0; i < 10; i++) {
  if (i == 4) {
    continue;
  }
  cout << i << "\n";
}


Enter fullscreen mode Exit fullscreen mode

这就是关于循环!


弦乐(8).png
数组用于在单个变量中存储多个值,而不是为每个值声明单独的变量。

要声明一个数组,请定义变量类型,指定数组的名称(后跟方括号),并指定它应存储的元素数量:



string games[4];


Enter fullscreen mode Exit fullscreen mode

现在,我们声明了一个变量,它保存着一个包含四个字符串的数组。要向其中插入值,我们可以使用数组字面量——将值放在花括号内,并以逗号分隔的列表中:



string games[4] = {"COD", "Cyberpunk", "Among Us", "Warplanes"} ;


Enter fullscreen mode Exit fullscreen mode

访问数组元素

您可以通过索引号访问数组元素。
注意 - 数组索引从 0 开始;[0] 是第一个元素。[1] 是第二个元素,依此类推。



string games[4] = {"COD", "Cyberpunk", "Among Us", "Warplanes"} ;
cout << games[0];
//Outputs COD


Enter fullscreen mode Exit fullscreen mode

要更改数组中的元素,



string games[4] = {"COD", "Cyberpunk", "Among Us", "Warplanes"} ;
games[0] = "Overwatch";
cout << games[0]
// Outputs Overwatch


Enter fullscreen mode Exit fullscreen mode

弦乐(9).png
最后!你真是个好学生!

C++ 提供了一些预定义函数,例如 main(),用于执行代码。但您也可以创建自己的函数来执行某些操作。

要创建(通常称为声明)一个函数,请指定函数的名称,后跟括号()



void buddyFunction(){
// code to be executed
}


Enter fullscreen mode Exit fullscreen mode

调用函数

声明的函数不会立即执行。它们被“保存以备后用”,稍后调用时才会执行。

要调用函数,请写下函数的名称,后跟两个括号()和一个semicolon

在下面的例子中,buddyFunction()当调用它时,用于打印文本(动作):



void buddyFunction(){
cout << "I am happy now!"

int main() {
  myFunction(); // call the function
  return 0;
}
//Outputs "I am happy now!"


Enter fullscreen mode Exit fullscreen mode

朋友们,文章到此结束,希望你们有所收获。我是否遗漏了什么必须包含的内容?请在评论区留言告诉我!

感谢您阅读这份长篇备忘单,祝您编码愉快!

最初发表于 Hashnode

鏂囩珷鏉ユ簮锛�https://dev.to/unitybuddy/c-cheat-sheet-for-beginners-33h1
PREV
如何像程序员一样思考?
NEXT
React 新手入门指南:7 天指南