fix(guide): simplify directory structure

This commit is contained in:
Mrugesh Mohapatra
2018-10-16 21:26:13 +05:30
parent f989c28c52
commit da0df12ab7
35752 changed files with 0 additions and 317652 deletions

View File

@@ -0,0 +1,23 @@
InlocaleTitle: undefined
t在C ++ STL库中生成集合 集合是一种关联容器,其中每个元素必须是唯一的。一旦将元素添加到集合中,就不能修改该元素的值,尽管可以删除并添加该元素的修改值。它们使用红黑树实现。
使用套装的好处
1. 它仅存储唯一值。
2. 元素的值标识自己。元素的值也是用于标识它的键。
3. 使用键即元素本身提供快速查找Olog n
4. 类定义集中有许多内置函数可以简化编程。
例: '''C ++
# 包括
使用命名空间std; int main { 组 S;
s.insert2; //在集合中插入元素2 s.insert3; s.insert5; s.insert2; //插入相同的元素2 s.insert6; forauto is cout << i <<“”; COUT << s.size<< ENDL; //给出集合的大小
s.erase5; //从集合中删除元素5 返回0; } “”” 创建一个set对象 '''C ++ S; “””
插入 '''C ++ s.insert _值_ be\_inserted; “””
访问集元素 '''C ++ :: iterator它; forit = s.begin; it= s.end; ++ it COUT << \*; “””

View File

@@ -0,0 +1,42 @@
---
title: C++ Arrays
localeTitle: C ++数组
---
## 什么是阵列?
数组是一系列相同数据类型的元素,它们存储在连续的内存位置,可以单独引用。
例如包含5个称为数字的整数值的数组声明如下
```C++
int numbers [5];
```
Initializiation
```C++
//Initialization with entries:
int numbers [5] = {1, 2, 3, 4, 5};
//Initialization with no values:
int numbers [5] = {};
//Initialization with declaration:
int numbers [] = {1, 2, 3, 4, 5};
//Note that here the number of values defines the size of the array.
//In the examples above, the size was fixed beforehand
```
**请注意** C ++中的数组在大小上是不可置换的这意味着一旦声明了大小为5的数组就无法将其放大或缩小。如果您确实需要具有相同条目的更大阵列则必须将所有条目复制到更大尺寸的新阵列。
### 访问:
可以通过引用它们在数组中的位置来访问数组中的元素。 从0开始计数
例:
```C++
x = numbers[0]; // = 1. [0] == first position
numbers[2] = 55; // Sets the third position (3) to the new number 55
//numbers[] is now: {1, 2, 55, 4, 5}
```

View File

@@ -0,0 +1,88 @@
---
title: Casting
localeTitle: 铸件
---
## 铸件
强制转换是一种特殊操作符,它强制将一种数据类型转换为另一种数据类型
C ++中的转换与C的转换有所不同.C ++使用不同的转换函数。
### 的static\_cast
静态强制转换用于基元和类型重载之间的隐式转换。
### const\_cast会
Const cast可用于抛弃常量。当需要改变常量值时这很有用。这应该谨慎使用相反在使用const-cast的情况下应该考虑使参数/函数为非const。
Const强制转换也可能导致未定义的行为。 const cast的唯一应用应该是从传递给函数并标记为const的值中删除const-ness。如果值是真正的const也就是说它在编译时被标记为const并赋值则const变量和变量的变异将导致未定义的行为。
```
const int y = 10; // y is set to 10.
const_cast<int &>(y) = 20; // undefined behaviour.
```
### 的dynamic\_cast
动态强制转换用于在其类层次结构中转换对象(对于父对象,从父对象到兄弟对象)。动态强制转换只能在多态类上调用。因此,在这种情况下, `MyClass`的原始类必须具有虚拟成员,该成员以虚拟析构函数的形式存在。
如果动态转换失败,它将返回`nullptr` 。动态强制转换在运行时确定对象类型时可能很有用。但是,应该注意的是,动态强制转换不是免费的,并且在某些情况下,其他技术可能在运行时确定类类型时更有效。
### reinterpret\_cast的
重新解释强制转换可能是所有C ++强制转换中最危险的,但如果使用正确,它可能是理想的。重新解释广播不会产生任何性能成本,因为它不会执行任何转换。它只是指示编译器将已转换的对象视为请求的类型。这也可能带来对齐问题,因此应谨慎使用,并且只有在已知并考虑副作用时才应使用。
#### 关于C风格演员表的说明
C ++支持使用C风格的强制转换但不推荐使用它们。使用C样式转换将指示编译器首先执行静态转换_如果静态转换_失败则在其位置使用reinterpret\_cast。因此C风格的演员阵容可能会产生不可预测的结果并带来意想不到的问题。
## 例子
```cpp
#include <iostream>
class MyClass {
public:
virtual ~MyClass() = default;
void greet() {
std::cout << "Hello World!" << std::endl;
}
};
class MyClassChild : public MyClass {
};
void reinterpretCastTest(void *objectPtr) {
// Let's assume we know objectPtr is of type MyClass *
auto myClassObj = reinterpret_cast<MyClassChild *>(objectPtr);
myClassObj->greet();
}
void constCastTest(const MyClassChild &myClassChild) {
auto nonConst = const_cast<MyClassChild &>(myClassChild);
nonConst.greet();
}
void dynamicCastTest(MyClass *myClass) {
auto *child = dynamic_cast<MyClassChild *>(myClass);
child->greet();
}
void staticCastTest(float floatVal) {
// Convert the float into an int.
auto intVal = static_cast<int>(floatVal);
std::cout << intVal << std::endl;
}
int main() {
MyClassChild myClass;
reinterpretCastTest(&myClass);
constCastTest(myClass);
dynamicCastTest(&myClass);
staticCastTest(10.5);
return 0;
}
```

View File

@@ -0,0 +1,109 @@
---
title: Clean Code Guidelines
localeTitle: 清洁代码指南
---
# 清洁代码指南
编码时,您遵循的编码风格非常重要。特别是当您与团队合作或计划分享您的团队时 码。 这些指南中的大部分都是标准的,可以应用于大多数编程语言,但是,这里有应用程序和 带有c ++代码的片段,因此您可以更轻松地熟悉它。 请记住,这些只是提高清晰度的建议,可以是个人偏好,所以请听取这些建议 考虑到但不要把他们带到信中。有时,破坏其中一些规则可以使代码更清晰。
## 使用好的变量名称并发表评论
确保你创建了一个好的变量名例如如果你正在创建一个游戏避免使用变量“a”使用类似“p1”的东西来指代玩家1. [匈牙利符号](https://en.wikipedia.org/wiki/Hungarian_notation)通常是传播的可以给你一些gidelines来声明变量。
另外,请使用评论,我甚至不开玩笑,只是尝试阅读你没有评论的一些旧项目......现在想象成为别人甚至没有编码。
## 全局变量
全局变量可以很容易使用,只需很少的代码就可以看起来像一个很好的解决方案。但是,当代码变得越来越大时,就越难以知道它们何时被使用。
您可以使用函数中声明的变量来代替使用全局变量,这些变量可以帮助您了解传递的值 并更快地识别错误。
```cpp
#include <iostream>
using namespace std;
// Global variables are declared outside functions
int cucumber; // global variable "cucumber"
```
## 使用goto继续等。
这是程序员之间的常见讨论,就像全局变量一样,这些类型的语句通常被认为是不好的做法。 他们被认为是坏的,因为他们导致[“spaguetti代码”](https://en.wikipedia.org/wiki/Spaghetti_code) 。当我们编程我们想要一个 线性流,当使用这些语句时,流被修改并导致“扭曲和纠结”的流动。
Goto过去曾被用过但是如果函数被创建那么就会引入那些结构化编程。 一般情况下请避免使用goto除非您确定它会使您的代码更清晰更易于阅读。一个例子可能是在嵌套循环中使用它。
break和continue的使用几乎相同。在开关中使用它们并尝试仅用于创建功能因此您只有一个出口点。
![IMG](https://imgs.xkcd.com/comics/goto.png)
## 避免更改for循环内的控制变量
通常有一些工作,看起来更清晰,更少混淆,例如。而循环。 做:
```cpp
int i=1;
while (i <= 5)
{
if (i == 2)
i = 4;
++i;
}
```
代替:
```cpp
for (int i = 1; i <= 5; i++)
{
if (i == 2)
{
i = 4;
}
// Do work
}
```
## 在顶部声明常量和类型
它们通常在库之后声明,这使它们变得更加容易阅读。 对于局部变量,它发生相同,在顶部声明它们(其他人为了节省内存而尽可能晚地声明它们看到: [cplusplus.com](http://www.cplusplus.com/forum/general/33612/)
## 最后只使用一个返回功能
就像我们之前说过的那样,我们倾向于只进行一次进入和退出以使流程更清晰。
## 即使在编写单行内容时也要使用花括号
系统地制作它将帮助您更快地完成它,如果您希望将来更改代码,您将能够毫无后顾之忧地完成它。
代替:
```cpp
for (int i = 1; i <= 5; i++)
//CODE
```
做:
```cpp
for (int i = 1; i <= 5; i++)
{
//CODE
}
```
## 其他建议
* #### 当您知道迭代次数时使用,而当您不知道迭代次数时使用。
* #### 在适当的时候使用const传递值/引用。这有助于节省内存。
* ####在大写字母中写入const以T开头的数据类型和小写的变量。
```cpp
const int MAX= 100; //Constant
typedef int TVector[MAX]; //Data type
TVector vector; //Vector
```

View File

@@ -0,0 +1,55 @@
---
title: C++ Compilers
localeTitle: C ++编译器
---
# C ++编译器简介
为了开始使用C ++您需要了解一些编译器以及C ++如何在您的计算机上运行。
完成所有工作后,计算机只能理解一种语言,机器语言。机器语言完全由 二进制位或0和1。虽然可以用二进制编程但这将是非常繁琐和耗时的。 因此我们人类开发了编程语言以便更容易地开发软件。汇编语言是机器的直接1对1 语言。 CC ++和COBOL等语言略高需要编译。它甚至更高。语言 像JavaScript和Python一样组件在编译之前会被翻译成C ++或其他低级语言, 有效地使它们成为比C或C ++更“高”的语言。 因为计算机体系结构由电子开关和电缆组成只能使用二进制1和0 您需要一个编译器将您的代码从高级C ++转换为CPU可以理解的机器语言。
编译器是实用程序,它将您的代码转换为可执行的机器代码文件。运行编译器时 在您的代码上首先预处理器读取源代码您刚刚编写的C ++文件)。预处理器搜索任何 预处理程序指令(以#开头的代码行)。预处理程序指令导致 预处理器以某种方式更改代码通常添加一些库或另一个C ++文件)。 接下来,编译器逐行处理预处理代码 每行进入相应的机器语言指令。这也将揭示您的任何语法错误 源代码并将向命令行抛出错误。最后,如果没有错误,编译器会创建一个对象 具有在您的机器上运行所必需的机器语言二进制文件。而编译器刚刚创建的目标文件 可能足以在您的计算机上执行某些操作它仍然不是您的C ++程序的可执行文件。有一个决赛 达到可执行程序的重要一步。
C ++包含一个庞大的库可以帮助执行I / O和硬件操作等困难任务。你可以包括这些 具有预处理程序指令的库,但预处理程序不会自动将它们添加到您的代码中。为了你有 最终的可执行程序,另一个称为链接器的实用程序必须将目标文件与库函数组合在一起 运行代码所必需的。把它想象成拥有所有必要的块 盖房子。编译器创建了所有块,但链接器是将它们全部组合在一起以最终创建一个房屋的链接器。 完成后,您现在拥有一个正常运行的可执行文件!
## 如何编译文件
假设您有一个名为`helloWorld.cpp`的C ++文件...
### 如果你在Windows上 -
#### 使用和CodeBlocks一样的IDE
它就像单击构建和运行按钮一样简单,它们将在项目文件夹中创建一个文件。 ![IMG](https://i.imgur.com/FwZuFGy.png)
#### 使用命令提示符
1. 打开开发人员命令提示符 - 对于此步骤您需要安装Microsoft Visual Studio或其他IDE 使您能够从命令行编译您的程序。您还可以在线搜索C ++编译器。
2. 直接导航到源代码
3. 在源代码上运行编译器假设您使用的是Microsoft Visual Studio编译器 `cl /EHsc helloWorld.cpp`
现在,这将创建一个目标文件并自动为您链接。如果你查看同一个文件夹,你会看到一个 hellWorld.exe可执行文件注意exe扩展名现在存在。
4. 在提示符下键入`helloWorld`以运行可执行文件
或者许多IDE允许快速构建和查看您的程序。这可能比你的版本更容易 Windows可能不会预先与编译器实用程序打包在一起。
### 如果您使用的是Linux或OSX -
1. 打开终端窗口并导航到源代码目录
2. 在源代码上运行编译器 `g++ helloWorld.cpp -o helloWorld`
这将创建一个目标文件并自动为您链接。查看该文件夹您将看到一个helloWorld.exe 可执行文件注意exe扩展名
3. 在终端窗口中键入`./helloWorld`以运行可执行文件
g ++是标准的Linux编译器是一个很棒的实用程序。它随操作系统一起提供。
注意: 直接编译和执行代码,运行 `g++ -o helloWorld helloWorld.cpp; ./helloWorld` 所以当你需要多次编译和运行你的代码时, 向上箭头输入
* * *
有许多不同类型的编译器。列出的两个是通常与Windows一起打包的两个 或Linux / OSX。

View File

@@ -0,0 +1,44 @@
---
title: Conditional Operator
localeTitle: 条件运算符
---
## 条件运算符
条件运算符是三元运算符它需要3个操作数。 它根据表达式的结果返回两个值中的一个 条件运算符用于替换简单的if-else语句。
句法
```cpp
(condition)?(expression-1):(expression-2);
```
这里,当条件为真时评估表达式-1当条件为假时评估表达式-2。 类似的if-else语句将是
```cpp
if(condition)
{
expression-1;
}
else
{
expression-2;
}
```
因此当您需要编写简单的if-else语句时条件运算符非常方便。它也可以在#define中使用 当在多个地方使用类似条件时的预处理器。
例如,要查找最多两个数字条件运算符,可以使用如下:
```cpp
#define big(a,b) (a>=b)?a:b
int maximum,x=5,y=6; // variable to store maximum of two numbers
maximum=(x>y)?x:y; // directly using conditional operator
maximum=big(x,y); // using the #define preprocessor defined above as big
```
**祝大家好运**
**快乐的编码! :)**
**随意在FreeCodeCamp的GitHub页面或[FreeCodeCamp的论坛](https://forum.freecodecamp.org/)上询问任何问题[](https://forum.freecodecamp.org/)**

View File

@@ -0,0 +1,43 @@
---
title: do while loop
localeTitle: 做循环
---
## 做循环
`do while loop`几乎与while循环相同。 `do while loop`具有以下形式:
```cpp
do
{
// do something;
} while(expression);
```
注意:请记住使用分号';'在条件结束时。
## 有关do-while循环的详细信息
只要您确定必须至少执行一次特定进程在循环内就会使用do-while循环。它具有许多优点例如不初始化检查变量例如char addmore ='Y')等。在结束时分号是必须的。
先做一些事情然后测试我们是否必须继续。结果是do块至少运行一次。 (因为表达测试后来)。看一个例子:
```cpp
#include <iostream>
using namespace std;
int main()
{
int counter, howmuch;
cin >> howmuch;
counter = 0;
do
{
counter++;
cout << counter << '\n';
}
while ( counter < howmuch);
return 0;
}
```

View File

@@ -0,0 +1,40 @@
---
title: Dynamic Memory Allocation
localeTitle: 动态内存分配
---
## C ++中的动态内存分配
### 什么是C ++中的动态内存分配?
* C ++中的**内存分配**是指**分配**给整个程序中使用的变量的内存。
* **动态内存分配**是在运行时**分配**给变量的内存,所需的内存量也在运行时决定。
* 这个内存来自**堆** 而非_静态_变量和_局部_变量从**堆栈中**获取内存。
* 在C ++中,程序员可以手动执行内存分配,并称为**_动态内存分配_** 。
* 在C中可以通过使用_calloc_和_malloc_函数来分配内存并使用_自由_函数来_解除_动态内存的分配来进行动态内存分配。
* 在C ++中,除了上述之外,还有两个函数, _new_和_delete_用于执行动态内存分配和解除分配。
### 新运营商
* `new`运算符可以从堆中授予程序员内存(如果可用)。如果程序员请求的存储器可用,则`new`运算符初始化存储器,然后返回分配的存储器的地址(引用)。
* **句法**
`pointer-variable-type` = **new** `data-type;`
示例1 `int *ptr` = **new** `int;`
例2 `int *ptr2` = **new** `int[10];`
这里, `pointer-variable-type``data type`的**指针** 。 `data-type`可以是intchar等也可以是用户定义的数据类型。
### DELETE运算符
* 程序员有责任解除分配动态分配的内存,否则在程序结束之前内存将无法重新分配。
* 要取消分配内存, `delete`操作符可用,程序员可以使用。
* **句法**
**delete** `pointer-type-variable;`
例如要释放上面示例1中分配的内存我们键入
`delete ptr;`
类似地例如2可以通过以下方式释放内存
`delete ptr2` ;
### 内存泄漏
当您无法释放在程序结束时通过`New`运算符分配的动态内存时会导致泄漏。如果不使用Delete运算符取消分配则每次程序运行时计算机都将继续在堆中创建新内存。这会导致计算机速度变慢因为内存未被删除且可用内存减少。

View File

@@ -0,0 +1,88 @@
---
title: Eraseremove idiom
localeTitle: 删除成语
---
## Desctiprion
如何从容器中删除元素是一个常见的C ++面试问题,如果您仔细阅读本页,您可以获得一些布朗尼点数。擦除 - 移除习语是一种C ++技术,用于从容器中消除满足特定标准的元素。但是,有可能用传统的手写循环来消除元素,但擦除 - 删除习语有几个优点。
### 对照
```cpp
// Using a hand-written loop
std::vector<int> v = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (auto iter = v.cbegin(); iter < v.cend(); /*iter++*/)
{
if (is_odd(*iter))
{
iter = v.erase(iter);
}
else
{
++iter;
}
}
// Using the eraseremove idiom
std::vector<int> v = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
v.erase(std::remove_if(v.begin(), v.end(), is_odd), v.end());
```
正如您所看到的,带有手写循环的代码需要更多的输入,但它也存在性能问题。每个`erase`调用必须在删除后删除所有元素,以避免集合中的“间隙”。在同一容器上多次调用`erase`产生大量移动元素的开销。
另一方面,具有擦除 - 删除习语的代码不仅更具表现力,而且更有效。首先,使用`remove_if/remove`将所有不符合删除条件的元素移动到范围的前面,保持元素的相对顺序。因此,在调用`remove_if/remove` ,单次调用`erase`会删除范围末尾的所有剩余元素。
### 例
```cpp
#include <vector> // the general-purpose vector container
#include <iostream> // cout
#include <algorithm> // remove and remove_if
bool is_odd(int i)
{
return (i % 2) != 0;
}
void print(const std::vector<int> &vec)
{
for (const auto& i : vec)
std::cout << i << ' ';
std::cout << std::endl;
}
int main()
{
// initializes a vector that holds the numbers from 1-10.
std::vector<int> v = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
print(v);
// removes all elements with the value 5
v.erase(std::remove(v.begin(), v.end(), 5), v.end());
print(v);
// removes all odd numbers
v.erase(std::remove_if(v.begin(), v.end(), is_odd), v.end());
print(v);
// removes multiples of 4 using lambda
v.erase(std::remove_if(v.begin(), v.end(), [](int n) { return (n % 4) == 0; }), v.end());
print(v);
return 0;
}
/*
Output:
1 2 3 4 5 6 7 8 9 10
1 2 3 4 6 7 8 9 10
2 4 6 8 10
2 6 10
*/
```
### 来源
“删除成语”维基百科:自由百科全书。 Wikimedia Foundation [Inc。en.wikipedia.org/wiki/Erase-remove\_idiom](https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom)
迈耶斯斯科特2001年。有效的STL50种改进标准模板库使用的具体方法。 Addison-Wesley出版社。

View File

@@ -0,0 +1,49 @@
---
title: Error Handling
localeTitle: 错误处理
---
# C ++异常处理
例外是在执行程序期间出现的问题。例外提供了一种将控制从程序的一个部分转移到另一个部分的方法。 C ++异常处理基于三个关键字try#catch和#throw
* # throw - 当问题出现时程序会抛出异常。这是使用throw关键字完成的。
* # catch - 程序在您要处理问题的程序中的位置捕获异常处理程序的异常。 catch关键字表示捕获异常。
* #try - try块标识将激活特定异常的代码块。接下来是一个或多个catch块。
```CPP
#include <iostream>
using namespace std;
int main()
{
int x = -1;
// Some code
cout << "Before try \n";
try {
cout << "Inside try \n";
if (x < 0)
{
throw x;
cout << "After throw (Never executed) \n";
}
}
catch (int x ) {
cout << "Exception Caught \n";
}
cout << "After catch (Will be executed) \n";
return 0;
}
```
# 在你继续之前......
## 回顾
* 错误类型的分组。
* 从正常代码中分离错误处理代码。
* 函数/方法可以处理他们选择的任何异常。

View File

@@ -0,0 +1,91 @@
---
title: For Loop
localeTitle: 对于循环
---
For循环是一个重复语句用于检查某些条件然后根据条件重复执行代码块直到满足指定条件。
for循环通过显式循环计数器或循环变量区别于其他循环语句循环变量允许循环体知道每次迭代的精确排序。
因此for循环是一种重复控制结构允许您有效地编写需要执行特定次数的循环。
## 句法
```
for ( init; condition; increment ) {
statement(s);
}
```
允许将增量放在for循环中就像在while循环中一样。这意味着这样的语法也可以。
```
for ( init; condition;) {
statement(s);
increment;
}
```
### 在里面
此步骤允许您声明和初始化任何循环控制变量。此步骤首先执行且仅执行一次。
### 条件
接下来评估条件。如果它成立则执行循环体。如果它保持为false则循环体不执行控制流跳转到下一次迭代重复进程
### 更新
update语句用于通过使用加法减法乘法或除法等简单操作来更改循环变量。 update语句在执行循环体之后执行。
## 实现:
```C++
#include <iostream>
using namespace std; // Here we use the scope resolution operator to define the scope of the standar functions as std::
int main () {
// for loop execution
for( int a = 10; a < 20; a = a + 1 ) { // The loop will run till the value of a is less than 20
cout << "value of a: " << a << endl;
}
return 0;
}```
Output:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
##Single lined loop
The body of the for loop need not be enclosed in braces if the loop iterates over only one satatement.
##Example
```
C ++ #包括 使用命名空间std;
int main{ //单行for循环 forint a = 10; a <20; a = a + 1 cout <<“a的价值”<< a << endl;
返回0; }\`\`\`
这将生成与前一个程序相同的输出。 即 输出: 价值10 价值11 价值12 价值a13 a的值14 价值a15 价值16 价值17 价值18 价值19
```
## Explanation
Here's the initialization condition is first set to a=10. The loop first checks for this condition. It then checks for the condition expression ie a<20 which holds true as 10<20(for the first case). Now the body of the loop is executed and we get the output "Value of a: 10". Then the update expression is executed which adds the number 1 to 'a' and the value of 'a' gets updated to 11 and the same steps are followed (as above) until the value of v reaches less than 20 ie 19.
# Range-based for-loop
C++ also has what we call range-based for loops which iterates through all the elements of a container(eg array).
## Syntax
```
forelementcontainer 声明S; }
int \[5\] array = {1,2,3,4,5} forint iarray cout << i << endl; }
输出 1 2 3 4 \`\`\`

View File

@@ -0,0 +1,59 @@
---
title: Functions in C++
localeTitle: C ++中的函数
---
## 定义:
函数是一组一起执行任务的语句。每个C ++程序至少有一个函数即main
函数声明告诉编译器函数的名称,返回类型和参数。函数定义提供函数的实际主体。
## C ++函数定义的一般形式:
```cpp
return_type function_name( parameter list )
{
body of the function
}
```
### 返回类型:
函数可以返回值。返回_类型是函数返回的值的数据类型。某些函数执行所需的操作而不返回值。在这种情况下返回_类型是关键字void。
### 功能名称:
这是函数的实际名称。函数名称和参数列表一起构成函数签名。
### 参数:
参数类似于占位符。调用函数时,将值传递给参数。该值称为实际参数或参数。参数列表是指函数参数的类型,顺序和数量。参数是可选的;也就是说,函数可能不包含任何参数。
### 功能体:
函数体包含一组语句,用于定义函数的功能。
## 例:
```cpp
int max(int num1, int num2)
{
// local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
```
## 功能为何重要?
函数支持模块化将工作分解为称为模块的较小部分这是OOP的一个基本特征它主要将C ++与C分离。 具有执行特定任务的特定功能可消除混淆并缩短主要功能的长度。 该函数还执行代码的可重用性。因此,下次您必须在同一程序中再次计算两个不同数字的最大值时,您无需复制和粘贴代码。你只需要调用该函数,它就可以完成其余的工作。
## 更多信息
* [TutorialsPoint](https://www.tutorialspoint.com/cplusplus/cpp_functions.htm)

View File

@@ -0,0 +1,27 @@
---
title: goto as a powerful utility
localeTitle: 转到一个强大的实用程序
---
# 介绍goto和标签的使用
goto是c ++中最强大但被高度低估的逻辑之一。如果正确使用goto可以使用goto实现疯狂的优化量。 它确实如此命名。它涉及到下一个标签的提及,无论它在哪里。
# 术语
```
goto - The keyword used to go to the particular label.
label - this can be named anything.
```
# 句法
跳转;
(没有<>;
//这会将exe带到标签的下一个外观。
goto是超越所有循环的东西。在这一点上更清楚这是一个例子。
https://code.sololearn.com/cI4qqQA8W2q3
但是必须非常谨慎地使用goto特别是在编码的早期阶段因为它可能导致疯狂的问题如果不能很好地理解的话。

View File

@@ -0,0 +1,192 @@
---
title: If-Else Statement
localeTitle: If-Else声明
---
## If-Else声明有什么作用
* If-Else语句是简单If语句的扩展。
* 在简单的If语句中如果测试表达式的值为false那么我们跳过块的代码并继续我们的下一个语句。
* 但很多时候如果test表达式的值为false我们希望执行某些步骤。
* 在这种情况下我们使用if-else语句。
### If-Else声明的一般形式
```cpp
if (test expression)
{
//statements that run if the test expression is true
}
else
{
//statements that run if the test expression is false
}
```
### If-Else语句的示例
如果test表达式为true
```cpp
int a=10;
if (a < 20) // This expression is true, so...
{
//...the code in this block gets executed, and...
}
else
{
//...the code in this block gets skipped.
}
//program continues
```
如果test表达式为false
```cpp
int a=10;
if (a>20) // This expression is false, so this time...
{
//...this code gets skipped...
}
else
{
//...and this code executes instead.
}
//program continues
```
### C ++中的示例:
```cpp
//Program to check whether number entered by user is positive or negative
#include <iostream>
using namespace std;
int main()
{
int no;
cout << "Enter a number: " << endl;
cin >> no;
// condition to check if number is positive or negative
if (no >= 0) // positive
{
// block if value is true
cout << "You entered a positive number: " << no << endl;
}
else // negative
{
// block if value is false
cout << "You entered a negative number: " << no << endl;
}
// program continues
cout << "This step is always printed" << endl;
return 0;
}
```
#### 产量
* 输入正数时:
```
Enter a number:
4
You entered a positive number: 4
This step is always printed
```
* 输入负数时:
```
Enter a number:
-200
You entered a negative number: -200
This step is always printed
```
[亲自尝试一下代码](https://repl.it/MzBq)
# **随意在FreeCodeCamp的GitHub页面或[FreeCodeCamp的论坛](https://forum.freecodecamp.org/)上询问任何问题[。](https://forum.freecodecamp.org/)**
[亲自尝试一下代码](https://repl.it/MzBq)
### 使用if ... else if ... else梯形图
如果我们必须使用if else基于多个条件做出决策。如果条件如下我们使用其他 -
```cpp
#include<iostream>
int main()
{
int score;
std::cout<<"Enter your score: \n";
std::cin>>score;
if(score>=90)
std::cout<<"Top performance.";
else if(score<90 && score>=70)
std::cout<<"Good performance";
else if(score<70 && score>=45)
std::cout<<"Average performance";
else if(score<45 && score>=30)
std::cout<<"You can improve it.";
return 0;
}
```
#### 产量
```
Enter your score:
85
Good performance
```
### if ... else if ... else ladder的另一个例子
假设我们有用户输入两个数字,我们将显示两个数字是否大于另一个数字。如果两者都不大于另一个,那么我们打印声明“两者都是平等的”。
在这个scinerio中我们需要一个if ... else if ... else梯形图。该程序将如下所示
```
#include<iostream>
using namespace std;
int main()
{
int number1,number2;
cout << "Enter first number: \n";
cin >> number1;
cout << "Enter second number: \n";
cin >> number2;
if(number1 > number2) // Checks if the first number is greater than the second number
{
cout << "Number 1 is greater.";
}
else if(number2 > number1) // Checks if the second number is greater than the first number
{
cout << "Number 2 is greater.";
}
else // If both of the above cases return false, then both numbers are equal
{
cout << "Both the numbers are equal.";
}
return 0;
}
```
#### 产量
```
Enter first number:
85
Enter second number:
86
Number 2 is greater.
```
* 请注意只有在不满足初始“if”条件时程序才会检查“else if”条件。如果这些条件都不满足则执行最后一个'else'块,打印语句:“两个数字相等。”。
* if ... else if ... else梯形图的大小可能会有所不同具体取决于程序试图解决的问题以及需要检查的条件数。
**祝大家好运**
**快乐的编码! :)**

View File

@@ -0,0 +1,103 @@
---
title: C++
localeTitle: C ++
---
# 你好,世界! - 您的第一个C ++程序
## 什么是C ++
* C ++是一种通用编程语言自1990年代开始使用
* 它由Bjarne Stroustrup设计名称为“C with classes”。
* 它是C的一个版本包括面向对象的元素包括类和函数。
* 它被认为是最大的编程语言之一,如下图所示: ![](http://static1.businessinsider.com/image/59deb30392406c21008b6148-1200/for-bonus-points-heres-the-chart-showing-these-languages-relative-popularity.jpg) _来源Github_
### 您在C ++中的第一个程序
```cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
return 0;
}
```
#### 该程序的输出将简单地是:
```
Hello World!
```
现在,让我们分解代码:
#### 第1行和第2行
```cpp
#include <iostream>
using namespace std;
```
* 第一行告诉计算机为此特定程序使用“iostream”头文件。头文件是带有预先编写的C ++代码的单独文件。还有许多其他头文件需要特定程序才能正常运行。其中一些是:数学,矢量和字符串。头文件通常用“.h”扩展名表示包含C ++标准库文件时不需要添加.h
* `iostream`代表输入输出流。 “iostream”文件包含允许计算机使用C ++语言获取输入并生成输出的代码。
* 第二行告诉计算机使用包含标准C ++功能的标准命名空间。你可以在没有这一行的情况下编写这个程序但是你必须在第4行使用`std::cout`而不是`cout``std::endl`而不是`endl` 。它使代码更具可读性,并且我们作为程序员的生活更容易。
#### 3号线和4号线
```cpp
int main()
{
```
* C ++从-main function- `int main()`开始执行程序。在执行期间,计算机开始从`{` (开始括号)到`}`每一行运行代码(结束括号) **注意:每个函数都以一个左大括号“{”开头,以一个右大括号“}”结束。**
* 第4行表示main函数的开始。
#### 第5,6和7行
```cpp
cout << "Hello World" << endl;
return 0;
}
```
* C ++中的单词`cout`用于输出。
* 接下来是`<<` _插入运算符_
* 无论是在双引号`""`被打印出来。某些特殊字符对于print语句具有不同的语法
* 现在要打印任何其他类型的数据,你必须添加`<<`
**_挑战尝试将Hello World更改为任何其他句子或单词。什么是输出_**
* 当使用C ++语言**结束此行并在输出期间转到下一行时,** `endl`是保留字。 - _cout代表“控制台输出”_
* 最后,用分号完成命令`;`
**注意:除主函数定义和#include指令之外的每个命令都需要以分号结束。没有 ”;” ,您可能会遇到错误。**
* `return 0;`在这种情况下安全地终止当前函数,即'main',因为在'main'之后没有函数跟随程序终止。
* 不要忘记告诉计算机这是main函数的结束。要执行此操作请添加结束大括号“}”。如果不包含**}**则在程序执行前会遇到错误。
### 代码看起来应该是这样的:
![](https://i.imgur.com/d1liGwI.png)
程序员使用Hello World程序就像这个程序作为使用新编程语言的仪式。这是好运的象征。
_您已经完成了第一个C ++程序的编码,并且已经理解了您编写/键入的大部分代码。恭喜_
**祝大家好运,编码愉快! :)**
**快乐的编码! :)**
**欢迎在FreeCodeCamp的GitHub页面或[FreeCodeCamp的论坛](https://forum.freecodecamp.org/)上提出任何问题[。](https://forum.freecodecamp.org/)**
[亲自尝试一下! :)](https://repl.it/L4k3)
**您可能需要一些软件来编写和执行C ++代码。我建议使用CodeBlocks。下面有一个下载链接**
下载链接: [在此下载](http://www.codeblocks.org/downloads/26)
* 单击用于Windows的GNU / GCC编译器的链接。这不需要额外安装
其他替代方案可以是visual studio使用编译器或在线IDE例如Cloud9或repl.it

View File

@@ -0,0 +1,80 @@
---
title: Inline Function
localeTitle: 内联函数
---
# 内联函数
## 介绍
内联函数是一个在C ++中定义的特殊函数,在调用时会内联扩展。
现在,这究竟意味着什么?
每当调用一个函数时,它需要花费很多额外的时间来执行一系列活动,例如跳转到函数,保存寄存器,将参数推入堆栈并返回到调用函数。所以需要很多时间。但内联函数是一种函数,其中已请求编译器执行内联扩展。函数请求编译器在调用函数的每个位置插入函数的完整主体,而不是生成代码以在定义它的一个位置调用函数。
但是,我们无法保证内联声明的每个函数都是内联的。因为当我们将函数声明为`inline` ,它是一个请求,而不是一个命令。在以下情况下,编译器可能会忽略内联请求: - 1如果函数包含循环例如`for`循环, `while`循环, `do-while`循环等。 2如果函数包含`switch``goto`语句。 3如果提到返回类型当然不是`void`的话),该功能不返回任何内容。 4如果函数包含静态变量。 5如果函数包含递归调用。
\`\`\`c ++
## 句法 -
内联返回_类型函数_名称argument\_list{
//函数体
}
```
## When to use Inline function
* When the function performs small tasks and is called very often.
* When performance is important.
* Instead of a macro.
```
C ++
# 包括
使用命名空间std;
class MathOperation {
上市:
```
inline int add(int x, int y){
return(x+y);
}
inline float div(int n1, float n2){
return(n1/n2);
}
```
};
int main{
MathOperation obj;
cout <<“Addition is<< obj.add34,12<< <\\ n \\ n“; cout <<“分区是:”<< obj.div12,3.4<<“\\ n”;
返回0;
} \`\`\`
## 内联功能的优点
* 它节省了函数返回调用的开销
* 它通过利用指令缓存增加了引用的局部性
* 它通过避免函数调用开销来加速您的程序
* 当函数调用发生时它可以节省堆栈上变量推/弹操作的开销
* 可以将函数定义放在头文件中即它可以包含在多个编译单元中而不会让链接器抱怨
## 内联函数的缺点
* 在标题中使用时它会使您的头文件更大其中包含用户不关心的信息
* 由于代码扩展它增加了可执行文件的大小
* C ++内联在编译时解析这意味着如果更改内联函数的代码 您需要使用它重新编译所有代码以确保它将被更新
* 如上所述它增加了可执行文件的大小这可能会导致内存中的颠簸 更多页面错误降低程序性能

View File

@@ -0,0 +1,119 @@
---
title: Inline Functions in C++
localeTitle: C ++中的内联函数
---
## C ++中的内联函数
当程序执行函数调用指令时CPU存储函数调用之后的指令的存储器地址复制函数的参数在堆栈上最后将控制转移到指定的函数。然后CPU执行功能代码将功能返回值存储在预定义的存储器位置/寄存器中,并将控制返回给调用功能。如果函数的执行时间小于从调用函数到被调用函数(被调用者)的切换时间,则这可能成为开销。对于大型和/或执行复杂任务的函数,与函数运行所花费的时间相比,函数调用的开销通常是微不足道的。但是,对于常用的小函数,进行函数调用所需的时间通常比实际执行函数代码所需的时间多得多。小功能的开销是因为小功能的执行时间小于切换时间。
C ++提供了一个内联函数来减少函数调用开销。内联函数是一个在调用时按行扩展的函数。当调用内联函数时内联函数的整个代码在内联函数调用点插入或替换。此替换由C ++编译器在编译时执行。内联函数如果很小则可以提高效率。 定义函数内联的语法是:
```cpp
inline return-type function-name(parameters)
{
// function code
}
```
请记住,内联只是对编译器的请求,而不是命令。编译器可以忽略内联请求。编译器可能不会在以下情况下执行内联:
* 如果函数包含循环。 forwhiledo-while
* 如果函数包含静态变量。
* 如果函数是递归的。
* 如果函数返回类型不是void则函数体中不存在return语句。
* 如果函数包含switch或goto语句。
### 内联函数具有以下优点:
* 不会发生函数调用开销。
* 调用函数时它还可以节省堆栈上push / pop变量的开销。
* 它还节省了函数返回调用的开销。
* 内联函数时,可以使编译器在函数体上执行特定于上下文的优化。对于正常的函数调用,这种优化是不可能的。通过考虑调用上下文和被调用上下文的流程,可以获得其他优化。
* 对于嵌入式系统,内联函数可能很有用(如果它很小),因为内联可以产生比函数调用前导码和返回更少的代码。
### 内联功能缺点:
* 内联函数中添加的变量会消耗额外的寄存器,如果要使用寄存器的变量数增加,则可能会在寄存器变量资源利用率上产生开销。这意味着当在函数调用点替换内联函数体时,函数使用的变量总数也会被插入。因此,用于变量的寄存器数量也将增加。因此,如果在函数内联变量数量大幅增加之后,它肯定会导致寄存器利用率的开销。
* 如果使用太多内联函数,则二进制可执行文件的大小将很大,因为相同代码的重复。
* 过多的内联也会降低指令缓存命中率,从而降低从缓存内存到主内存的指令获取速度。
* 如果有人更改了内联函数中的代码,则内联函数可能会增加编译时间开销,因此必须重新编译所有调用位置,因为编译器需要再次替换所有代码以反映更改,否则它将继续使用旧功能。
* 内联函数可能对许多嵌入式系统无用。因为在嵌入式系统中,代码大小比速度更重要。
* 内联函数可能会导致颠簸,因为内联可能会增加二进制可执行文件的大小。内存中的颠簸会导致计算机性能下降。
以下程序演示了这个概念:
```cpp
#include <iostream>
using namespace std;
class operation
{
int a,b,add,sub,mul;
float div;
public:
void get();
void sum();
void difference();
void product();
void division();
};
inline void operation :: get()
{
cout << "Enter first value:";
cin >> a;
cout << "Enter second value:";
cin >> b;
}
inline void operation :: sum()
{
add = a+b;
cout << "Addition of two numbers: " << a+b << "\n";
}
inline void operation :: difference()
{
sub = ab;
cout << "Difference of two numbers: " << ab << "\n";
}
inline void operation :: product()
{
mul = a*b;
cout << "Product of two numbers: " << a*b << "\n";
}
inline void operation ::division()
{
div=a/b;
cout<<"Division of two numbers: "<<a/b<<"\n" ;
}
int main()
{
cout << "Program using inline function\n";
operation s;
s.get();
s.sum();
s.difference();
s.product();
s.division();
return 0;
}
```
输出:
```
Enter first value: 45
Enter second value: 15
Addition of two numbers: 60
Difference of two numbers: 30
Product of two numbers: 675
Division of two numbers: 3
```

View File

@@ -0,0 +1,136 @@
---
title: Input and Output
localeTitle: 输入和输出
---
## 使用Streams输入和输出
要将内容打印到控制台或从中读取内容,请使用`cout``cin` ,即所谓的`streams` 。使用这个比喻是因为你使用的流就像使用接收器或水龙头一样:你要么将数据刷新到接收器( `cout` 要么从tap `cin` )中获取数据。
### 用cout输出
“Hello World”程序使用`cout`打印“Hello World”到控制台
```C++
#include<iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
}
```
顶部的前两行是您使用`cout`和其他流所必需的。 `#include<iostream>`使流对象可以`using namespace std;` ,并`using namespace std;`让你直接输入`cout`而不必输入`std::cout` ,也就是说,必须指定我们要从`std`命名空间使用`cout` 。
`cout`代表“控制台输出”是代表控制台的所谓_输出流_ 。当你想在控制台上打印东西时,你可以将它放入`cout` ;想象它是一个通往终端的洞。要把东西放进这个洞,一次一个,你使用`<<`运算符也就是_插入运算符_ 1 。操作员可以被链接,也就是说,您可以将多个东西一个接一个地放在一起。以下将打印“蛋糕是谎言。”:
`cout << "The cake " << "is " << "a " << "lie." << endl;`
`endl`代表“End Line”是另一个来自`<iostream>` 。当你把`endl`到`cout` ,它会打印一个换行符(“\\ n”到控制台并_刷新_ `cout` ,这意味着它会迫使`cout`打印你_现在_已经投入它的一切。如果您没有将`endl`放入`cout` `cout`可能会保留您放入其中的数据但在实际打印之前需要等待更多数据。这称为_缓冲_ ,非常适合性能,但如果您已经将它应该打印的所有内容都给了它,那么您希望`cout`立即打印它。因此,在有意义的地方结束使用`endl`是一种非常好的做法。
几乎所有东西都可以放入一个流:字符串,数字,变量,表达式等。这里有一些有效的流插入示例:
```C++
// Notice we can use the number 42 and not the string "42".
cout << "The meaning of life is " << 42 << endl;` // Output: The meaning of life is 42
```
```C++
string name = "Tim";
cout << "Except for you, " << name << endl;`// Output: Except for you, Tim
```
```C++
string name = "Tim";
cout << name;
cout << " is a great guy!" << endl;`
// Output: Tim is a great guy!
```
```C++
int a = 3;
cout << a*2 + 18/a << endl;`// Output: 12
```
### 关于空白的说明
C ++总是让_你_掌控并且只完成你告诉它要做的事情。这有时会令人惊讶如下例所示
```C++
string name = "Sarah";
cout << "Good morning" << name << "how are you today? << endl;
```
你可能期望它打印出来“早上好莎拉你今天过得怎么样?”,但实际上,输出将是“早上好,你今天好吗?”。出现此错误的原因是您没有在`name`周围的字符串中写入空格,因此,由于您没有指定任何空格,因此`cout`不会打印任何空格。正确的版本将是: `cout << "Good morning " << name << " how are you today? << endl;`
换行也不会自己发生。你可能会认为这会在四行打印一个食谱:
```C++
cout << "To make bread, you need:";
cout << "* One egg";
cout << "* One water";
cout << "* Two wheat";
```
但实际上产量都在一条线上:“要做面包,你需要:\*一个鸡蛋\*一个水\*两个小麦”。这是因为行的末尾没有换行符所以很自然地C ++假设我们不希望它打印换行符。
您可以通过在每行之后添加`endl`来解决此问题,因为如前所述, `endl`在换行流中插入换行符。但是,它也会强制冲洗缓冲区,这会让我们失去一点性能,因为我们可以一次打印所有的线条。因此,最好的方法是在行尾添加实际的换行符,并且最后只使用`endl`
```C++
cout << "To make bread, you need:\n";
cout << "* One egg\n";
cout << "* One water\n";
cout << "* Two wheat" << endl;
```
如果你只是打印一个小食谱,你节省的时间是微不足道的,不值得麻烦,但如果你打印数百万的项目,差异可能是非常明显的。
### 用cin输入
要从控制台中读取,您可以像使用`cout`一样使用_输入流_ `cin` ,但不要将内容放入`cin` ,而是“将它们取出”。以下程序从用户读取两个数字并将它们加在一起:
```C++
#include<iostream>
using namespace std;
int main()
{
int a, b; // Variables to hold the two numbers.
cout << "Please enter the first number:" << endl;
cin >> a;
cout << "Please enter the second number:" << endl;
cin >> b;
cout << "The sum of your numbers is: " << a + b << endl;
}
```
`cin`代表“控制台输入”是表示来自控制台的_输入的输入流_ 。在表达`cin >> a;` ,使用运算符`>>` _提取运算符_ 2 ,从`cin`读取数据并保存到变量`a` 。提取操作符读取写入我们指定的变量所需的数据并跳过任何空格因此如果用户键入“6”将只读取值`6` 。
值得注意的是, `cin`将停止整个程序等待用户输入其值。在用户按下回车之前程序将不会继续并且有一些数据要写入变量。如果用户只是在不输入任何内容的情况下按Enter键则`cin`将继续等待值。
提取运算符`<<`也可以链接。这是与上次相同的程序,但是以更简洁的方式编写:
```C++
#include<iostream>
using namespace std;
int main()
{
int a, b; // Variables to hold the two numbers.
cout << "Please enter two numbers:" << endl;
cin >> a >> b;
cout << "The sum of your numbers is: " << a + b << endl;
}
```
链接时,提取操作符将首先从`cin`读取数据以填充`a` ,然后读取数据以填充`b` 。
C的标准printf和scanf语句也可以通过导入'与c ++一起使用' '头文件。
## 来源
1. http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/
2. http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/

View File

@@ -0,0 +1,13 @@
---
title: C++ Lists
localeTitle: C ++列表
---
# 什么是STL清单
C ++中的列表是一个功能强大的工具类似于其众所周知的堂兄C ++ Vectors。而Vectors是一个顺序容器 如果元素在连续链中被索引,则列表也是一个顺序容器,但它们的组织方式不同。 List元素指向其下一个元素因此所有元素按顺序排序但它们不使用索引。 怎么样?你可能会问。他们这样做不是通过索引,而是使用一个称为迭代器的特殊工具。迭代器就像特殊指针一样 他的工作是保持列表元素的顺序,就像两辆火车车厢之间的联系。这是一个很好的视觉效果 与矢量和数组相比如何组织列表。 ![IMG](https://imgur.com/SiU8uTe.png)
## 如何声明一个List
如果要声明您编写的数字列表:
'''的std ::名单数;“””

View File

@@ -0,0 +1,84 @@
---
title: Loops
localeTitle: 循环
---
# 循环
## 介绍
现在让我们讨论称为循环的东西。假设您要在屏幕上打印1到1000的偶数。单程 这样做是为了写下面这几行
\`\`\`c ++ cout << 0 << endl; cout << 2 << endl; cout << 4 << endl; ...。 ...。 ...。 cout << 1000 << endl;
```
But the problem with this approach is that you have to write the same line again and again. And if suppose you have to print
prime numbers from 1 to 1000 then this will be more hectic.
Therefore, in order to solve such problems loops are introduced.
There are different types of loop functions:
### While and do while loops
While and do while loops allow you to make the loop until a condition finishes.
The difference between While and Do while is that Do while always executes once.
Here you can see an example:
```
C ++ whilecondition{ //条件为true时执行的代码 } { //将执行一次直到条件为假 } whilecondition;
```
### For loops
For loops are usually used when you know how many times the code will execute.
The flow can be seen in this [graph](https://www.tutorialspoint.com/cplusplus/images/cpp_for_loop.jpg).
They are declared this way:
```
C ++ for初始化变量;检查条件;增加初始化变量{ //要执行的代码 }
```
Lets write a program which will print numbers from 0 to 1000 including 1000 on the screen using a for loop.
```
C ++ forint i = 0; i <= 1000; i ++ { cout << i << endl; }
```
When you execute this code in a c++ program numbers from 1 to 1000 will be printed.
Now lets discuss how the for loop works.
* You start a for loop by typing the keyword 'for'. It means you are starting a for loop
` for `
* Next you open and close a round bracket. In this brackets you write some conditions which will be discussed later
` for()`
* Inside the brackets first you write the initial condition ie the value from where the loop will start. Like in the
above program we write int i = 0
` for(int i = 0)`
* Then you write the semicolon and then condition until when the loop will be executed. In the above code you define
i < 1000. It means until value of i is less then 1000 execuete the loop.
` for(int i=0;i<=1000) `
* Then you define the incremented value that is how much i has to be incremented in each iteration. In the above code
we write i++. It means value of i will be incremented by 1 every time.
` for(int i=0;i<=1000;i++) `
* If there is only one statement inside the loop then the curly bracket is optional but its better to write loop code
within brackets so that you don't get confused.
``` c++
for(int i=0;i<=1000;i++)
{
}
```
* Then inside the loop you write what do you want to do. In the above program we output the value of i.
So, in this way the for loop works
If you want to print even numbers from 1 to 1000 then your program will look like this
```
C ++ forint i = 0; i <= 1000; i = i + 2 { cout << i << endl; }
\`\`\`
* 第一个程序和第二个程序的区别是增量部分其余代码是一样的该程序将打印0和 然后添加2并在控制台上打印2依此类推i的值等于1000
我们打印偶数0到1000的最终程序将如下所示
\`\`\`c ++
# 包括
使用命名空间std; int main { forint i = 0; i <= 1000; i = i + 2 { cout << i << endl; } 返回0; } \`\`\`

View File

@@ -0,0 +1,81 @@
---
title: Map
localeTitle: 地图
---
## 地图介绍
`map`是一个关联容器,用于存储键值对中的元素。就像在`Java`有集合PHP中的关联数组等。
## 使用地图的好处
* 它仅存储唯一键,并且也基于其指定的排序标准按排序顺序存储。
* 由于键是按排序顺序的,因此通过键在地图中搜索元素非常快,即它需要对数时间。
*`map` ,每个键只附加一个值。
* `map`可以用作关联数组。
* 它可以使用平衡二叉树实现。
这是一个例子:
```c++
#include <iostream>
#include <map>
using namespace std;
int main (){
map<char,int> first;
//initializing
first['a']=10;
first['b']=20;
first['c']=30;
first['d']=40;
map<char, int>::iterator it;
for(it=first.begin(); it!=first.end(); ++it){
cout << it->first << " => " << it->second << '\n';
}
return 0;
}
```
输出:
```
a => 10
b => 20
c => 30
d => 40
```
## 创建地图对象
`map<string, int> myMap;`
## 插入
使用插入成员函数插入数据。
```c++
myMap.insert(make_pair("earth", 1));
myMap.insert(make_pair("moon", 2));
```
我们还可以使用operator \[\] ie在std :: map中插入数据
`myMap["sun"] = 3;`
## 访问地图元素
要访问地图元素,您必须为它创建迭代器。这是前面提到的一个例子。
```c++
map<char, int>::iterator it;
for(it=first.begin(); it!=first.end(); ++it){
cout << it->first << " => " << it->second << '\n';
}
```
在这里,您可以了解有关地图的更多信息: [cpluspluc\_map](http://www.cplusplus.com/reference/map/map/map/)
注意示例中的所有代码都是C ++ 11版本。您可以[在此处](http://en.cppreference.com/w/cpp/compiler_support)了解有关C ++版本的更多信息

View File

@@ -0,0 +1,78 @@
---
title: Object Oriented Programming using C++
localeTitle: 面向对象的C ++编程
---
## 面向对象的C ++编程
面向对象编程简称OOP旨在实现编程中的继承隐藏和多态等真实世界实体。 OOP的主要目的是将数据和对其进行操作的函数绑定在一起这样除了该函数之外其他任何代码都不能访问这些数据。
让我们了解面向对象编程语言的不同特征:
### 宾语:
对象是面向对象系统中的基本运行时实体,对象是类的实例,这些是定义的用户定义数据类型。
```cpp
class person
{
char name[20];
int id;
public:
void getdetails(){}
};
int main()
{
person p1; //p1 is an object
}
```
对象占用内存中的空间并具有相关的地址如pascal或结构中的记录或C中的并集。
执行程序时,对象通过相互发送消息进行交互。
每个对象都包含操作数据的数据和代码。对象可以在不必了解彼此数据或代码的详细信息的情况下进行交互。知道接受的消息类型和对象返回的响应类型就足够了。
### 类:
类是数据和函数或方法的蓝图。班级不占用任何空间。
```cpp
class class_name
{
private:
//data members and member functions declarations
public:
//data members and member functions declarations
protected:
//data members and member functions declarations
};
```
类是用户定义的数据类型如C中的结构和联合。
默认情况下,类变量是私有的,但在结构的情况下它是公共的。在上面的例子中,人是一个类。
### 封装和数据抽象:
将数据和函数包装(组合)到单个单元中称为封装。外部世界无法访问这些数据,只有那些包含在类中的函数才能访问它。这种数据与程序直接访问的隔离称为数据隐藏或信息隐藏。
数据抽象是指仅向外界提供所需信息并隐藏实现细节。例如将具有公共函数的类Complex视为getReal和getImag。我们可以将类实现为大小为2的数组或两个变量。抽象的优点是我们可以在任何时候改变实现Complex类的用户不会受到影响因为我们的方法接口保持不变。如果我们的实施是公开的我们就无法改变它。
### 遗产:
继承是一个类的对象获取另一个类的对象属性的过程。它支持层次分类的概念。继承提供可重用性。这意味着我们可以在不修改现有类的情况下向其添加其他功能。
### 多态性:
多态性意味着能够采用多种形式。操作可能在不同情况下表现出不同的行为。行为取决于操作中使用的数据类型。
C ++支持运算符重载和函数重载。运算符重载是使运算符在不同实例中展示不同行为的过程。函数重载使用单个函数名来执行不同类型的任务。多态性广泛用于实现继承。
### 动态绑定:
在动态绑定中,响应函数调用而执行的代码在运行时决定。 C ++具有支持此功能的虚拟功能。
### 消息传递:
对象通过彼此发送和接收信息来彼此通信。对象的消息是执行过程的请求,因此将调用接收对象中生成所需结果的函数。消息传递涉及指定对象的名称,函数的名称和要发送的信息。

View File

@@ -0,0 +1,102 @@
---
title: C++ Overloading
localeTitle: C ++重载
---
C ++允许您为同一范围内的函数名称或运算符指定多个定义,分别称为函数重载和运算符重载。
重载声明是声明,声明与同一作用域中先前声明的声明具有相同的名称,除了两个声明具有不同的参数和明显不同的定义(实现)。
当您调用重载函数或运算符时,编译器通过将用于调用函数或运算符的参数类型与定义中指定的参数类型进行比较,确定要使用的最合适的定义。选择最合适的重载函数或运算符的过程称为重载决策。
### C ++中的函数重载
您可以在同一范围内对同一函数名称具有多个定义。函数的定义必须通过参数列表中的参数的类型和/或数量彼此不同。您不能重载仅由返回类型不同的函数声明。
以下是使用相同函数print打印不同数据类型的示例 -
```cpp
#include <iostream>
#include <string>
using namespace std;
class printData {
public:
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(double f) {
cout << "Printing float: " << f << endl;
}
void print(const string& s) {
cout << "Printing string: " << s << endl;
}
};
int main() {
printData pd;
// Call print to print integer
pd.print(5);
// Call print to print float
pd.print(500.263);
// Call print to print string
pd.print("Hello C++");
return 0;
}
```
编译并执行上述代码时,会产生以下结果 -
```
Printing int: 5
Printing float: 500.263
Printing string: Hello C++
```
### C ++中的运算符重载
大多数内置运算符也可以在C ++中重载。这允许程序员根据参数为操作符分配不同的实现。这些重载的运算符可以用于用户定义的类。
```
#include<iostream>
using namespace std;
class Complex_Number{
private:
int real;
int imag;
public:
Complex_Number(int i = 0, int j =0)
{
real = i;
imag = j;
}
//Here the operator '+' is being overloaded
Complex_Number operator + (Complex_Number const &a)
{
Complex_Number x;
x.real = real + a.real;
x.imag = imag + a.imag;
return x;
}
void print()
{
cout<<real<<" + i"<<imag<<endl;
}
};
int main()
{
Complex_Number c1(3, 2), c2(1, 1);
//Here, the overloaded operator is called. The numbers get added according to the function defined
Complex_Number c3 = c1 + c2;
c3.print();
}
```
上述计划的输出
```
4 + i3
```

View File

@@ -0,0 +1,160 @@
---
title: Preprocessors
localeTitle: 预处理器
---
## C / CPP中的预处理器
顾名思义预处理器是在编译之前处理源代码的程序。在C / C ++中编写程序和执行程序之间涉及许多步骤。在我们真正开始学习预处理器之前,让我们先看看这些步骤。
![](https://i.imgur.com/Pb0aTkV.png)
您可以在上图中看到中间步骤。程序员编写的源代码存储在文件program.c中。然后由预处理器处理该文件并生成名为program的扩展源代码文件。此扩展文件由编译器编译生成名为program.obj的目标代码文件。最后链接器将此目标代码文件链接到库函数的目标代码以生成可执行文件program.exe。
预处理程序提供预处理程序指令,告诉编译器在编译之前预处理源代码。所有这些预处理程序指令都以`#` (哈希)符号开头。 C / C ++程序中语句开头的这个('')符号表示它是一个预处理器指令。我们可以将这些预处理器指令放在程序的任何位置。一些预处理程序指令的示例是: `#include` `#include` `#define` `#ifndef`等。
### 预处理程序指令的类型:
1.
2. 文件包含
3. 条件编译
4. 其他指令
### 宏:
宏是程序中的一段代码,它有一些名称。只要编译器遇到此名称,编译器就会将该名称替换为实际的代码段。 `#define`指令用于定义宏。
```cpp
#include<iostream>
#define LIMIT 3
int main()
{
for(int i=0; i < LIMIT; i++)
{
std::cout<<i<<" " ;
}
return 0;
}
```
输出:
`0 1 2`
在上面的程序中,当编译器执行单词`LIMIT`它将其替换为3.宏定义中的`LIMIT`字称为宏模板,'3'表示宏扩展。
在宏定义的末尾不应该有分号(';')。宏定义不需要使用分号结束。
### 文件包含:
这种类型的预处理程序指令告诉编译器在源代码程序中包含一个文件。用户可以在程序中包含两种类型的文件:
* ####头文件或标准文件 这些文件包含预定义函数的定义如printf... scanf等。必须包含这些文件才能使用这些函数。 ...在不同的头文件中声明了不同的功能。例如......标准I / O功能在'iostream'文件中,而执行字符串操作的功能在'string'文件中。
#### 句法:
`#include< file_name >` 其中file\_name是要包含的文件的名称。 `<``>`括号告诉编译器在标准目录中查找文件。
* ####用户定义的文件 当程序变得非常大时,最好将它分成较小的文件并在需要时包含。这些类型的文件是用户定义的文件。这些文件可以包含在: ... `#include"filename"`
### 条件编译:
条件编译指令是指令的类型,它有助于编译程序的特定部分或基于某些条件跳过程序的某些特定部分的编译。这可以通过两个预处理命令`ifdef``endif`
#### 句法:
```cpp
ifdef macro_name
statement1;
statement2;
statement3;
.
.
.
statementN;
endif
```
如果定义了名为'macroname'的宏,则语句块将正常执行,但如果未定义,则编译器将跳过此语句块。
### 其他指令:
除了上述指令外,还有两个不常用的指令。这些是:
1. \##### `#undef`指令: `#undef`指令用于`#undef`定义现有宏。该指令的作用如下:
##### 句法:
`#undef LIMIT` 使用此语句将取消定义现有的宏LIMIT。在此语句之后每个`#ifdef LIMIT`语句将评估为false。
2. \##### `#pragma`指令: 该指令是一个特殊用途指令,用于打开或关闭某些功能。这种类型的指令是特定于编译器的,即它们因编译器而异。下面讨论一些`#pragma`指令:
##### `#pragma startup`和`#pragma exit`
这些指令帮助我们指定在程序启动之前控件传递给main之前和程序退出之前在控件从main返回之前运行所需的函数。
```cpp
#include<stdio.h>
void func1();
void func2();
#pragma startup func1
#pragma exit func2
void func1()
{
printf("Inside func1() ");
}
void func2()
{
printf("Inside func2() ");
}
int main()
{
printf("Inside main() ");
return 0;
}
```
输出:
`Inside func1() Inside main() Inside func2()`
当在GCC编译器上运行时上面的代码将产生如下给出的输出
输出:
`Inside main()`
这是因为GCC不支持#pragma startup或exit。但是您可以在GCC编译器上使用以下代码获得类似的输出。
```cpp
#include<stdio.h>
void func1();
void func2();
void __attribute__((constructor)) func1();
void __attribute__((destructor)) func2();
void func1()
{
printf("Inside func1()\n");
}
void func2()
{
printf("Inside func2()\n");
}
int main()
{
printf("Inside main()\n");
return 0;
}
```
##### `#pragma warn`指令:
该指令用于隐藏编译期间显示的警告消息。 我们可以隐藏警告,如下所示:
##### `#pragma warn -rvl`
该指令隐藏了当应该返回值的函数未返回值时引发的警告。
##### `#pragma warn -par`
该指令隐藏了当函数不使用传递给它的参数时引发的警告。
##### `#pragma warn -rch`
该指令隐藏了代码无法访问时引发的警告。例如在函数中的return语句之后写入的任何代码都是不可访问的。

View File

@@ -0,0 +1,197 @@
---
title: queue
localeTitle: 队列
---
## 队列
`queue`是C ++中最常用的容器之一。容器是一种存储对象集合的数据结构,有些是有序的,有些则不是。所有容器都有一组不同的功能,允许您访问该集合中的对象。
`std::queue`是C ++标准库的一部分(因此前缀为`std::` 允许您以先入先出FIFO顺序存储数据。注意 **队列中的所有对象必须具有相同的数据类型**
存储在队列中的数据类型位于queue关键字旁边的尖括号内。例如如果要存储整数集合则队列将为`std::queue<int> queue_name`
### 队列LIFO解释
`queue`允许我们按特定顺序推送/排队和弹出/出列。 **推送**意味着在队列的前面插入一个对象。 **Pop**意味着从队列末尾拉出“最旧的”对象。所以当你推动它在前面时,当你弹出时,你提取最旧的元素。
![alt text](https://github.com/mohammadaziz313/helloworld/blob/master/Fifo_queue.png "FIFO队列入队和出队示例")
### 队列操作
队列容器支持以下操作:
* 推(入队)
* pop出队
*
* 尺寸
* 面前
* 背部
#### 推
允许您在队列末尾的当前最后一个元素之后插入一个新元素。
```cpp
//Push operation in Queue
#include <iostream> // std::cout
#include <queue> // std::queue
int main ()
{
std::queue<int> q;
q.push(1); //Pushing 1 at front of the queue
q.push(2); //Pushing 2 at front of the queue
return 0;
}
```
#### 面前
允许您访问队列中的下一个元素而不删除它。 下一个元素是队列中“最旧的”元素。
```cpp
//Front operation in Queue
#include <iostream> // std::cout
#include <queue> // std::queue
int main ()
{
std::queue<int> q;
q.push(1); //Pushing 1 at front of the queue
q.push(2); //Pushing 2 at front of the queue
std::cout<<q.front()<<'\n'; //Accessing the front of the queue
std::cout<<q.front()<<'\n'; //Accessing the front of the queue
return 0;
}
```
```
Output:
1
1
```
#### 流行的
允许您删除队列中的下一个元素,有效地将其大小减小一个。 删除的元素是“最旧的”元素。
```cpp
//Pop operation in Queue
#include <iostream> // std::cout
#include <queue> // std::queue
int main ()
{
std::queue<int> q;
q.push(1); //Pushing 1 at front of the queue
q.push(2); //Pushing 2 at front of the queue
std::cout<<q.front()<<'\n'; //Accessing the front of the queue
q.pop(); //Removing the oldest element
std::cout<<q.front()<<'\n'; //Accessing the front of the queue
q.pop(); //Removing the oldest element
return 0;
}
```
```
Output:
1
2
```
#### 尺寸
返回`queue`的元素数。
```cpp
//Size operation in Queue
#include <iostream> // std::cout
#include <queue> // std::queue
int main ()
{
std::queue<int> q;
q.push(1); //Pushing 1 at front of the queue
q.push(2); //Pushing 2 at front of the queue
std::cout<<q.size()<<'\n'; //Accessing the front of the queue
q.pop(); //Removing the oldest element
std::cout<<q.size()<<'\n'; //Accessing the front of the queue
q.pop(); //Removing the oldest element
std::cout<<q.size()<<'\n'; //Accessing the front of the queue
return 0;
}
```
```
Output:
2
1
0
```
#### 空
返回`queue`是否为空,即队列大小是否为零。 如果队列的大小为0则返回`true` ,否则返回`false`
\`\`\`cpp //队列中的空操作
# 包括 // std :: cout
# 包括 // std :: stack
int main { 的std ::队列 q;
q.push1; q.push2;
whileq.empty= true{ 的std :: COUT << q.front<< '\\ n'; q.pop; }
std :: cout <<“Out of loop”<<'\\ n'; 返回0; }
```
Output:
1
2
Out of loop
#### Back
Allows you to access the last element in the queue without removing it.
The next element is the "newest" element in the queue.
```
CPP //在队列中返回操作
# 包括 // std :: cout
# 包括 // std :: queue
int main { 的std ::队列 q;
q.push1; //在队列前面按1 q.push2; //在队列前面按2
的std :: COUT << q.back<< '\\ n'; //访问队列的后面 的std :: COUT << q.back<< '\\ n'; //访问队列的后面
返回0; } \`\`\`
```
Output:
2
2
```
### 更多资源:
http://www.cplusplus.com/reference/queue/queue/
### 引文:
图片礼貌https __ //en.wikipedia.org/wiki/FIFO _computing_ and\_electronics

View File

@@ -0,0 +1,88 @@
---
title: Random Functions
localeTitle: 随机函数
---
* 随机数是将随机化引入程序的便捷方式。例如,如果你想运行任何模拟或玩游戏,删除数组的随机索引等,那么随机数就是要走的路。
* 要包含在c ++中使用随机数的头文件是`cstdlib`
* _专业提示 -_您可以使用`cpp #include<bits/stdc++.h>`来包含所有头文件中的所有函数。
功能: - rand
\- 返回从0到RAND _MAX_的伪随机数整数 _。不接受任何争论。 - RAND_ MAX是允许的最大整数。它是编译器依赖通常是2147483647。
以下是一个例子: -
```cpp
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
cout << rand() << endl;
cout << rand() << endl;
cout << rand() << endl;
return 0;
}
/*
Output:- (Note that the output is random and will differ from what is mentioned here)
1804289383
846930886
1681692777
*/
```
现在,再次运行该程序。然后再次。然后再次。你看到了什么? 一次又一次地打印相同的输出。
让我们回到rand函数的定义 -
rand - _返回从0到RAND\_MAX的**伪随机**数整数。不接受任何争论。_
那么什么是伪随机数?
* 顾名思义,一个非真正随机的数字是伪随机数。
* 伪随机数不是加密安全的,容易受到攻击。
* 在C ++的上下文中数字看起来是随机的但不是真正随机的。该函数假定从0到RAND\_MAX的每个数字都具有相同的可能性并吐出一个数字。 (事实上​​, 事实并非如此但它很接近。例如数字5几乎用于所有地方。如果随机数吐出5您可能认为该数字不是随机的。
* 随机函数rand占用一个非常大的数字通过一个大的素数应用模数并对数字进行各种操作并返回一个值。无论多么复杂它仍有可能打破它。
我们如何在每个程序执行时获得一组独特的随机数?
我们使用`void srand(int seed)` ;
* “种子”是赋予一个数字的名称,使得随机序列生成器每次都在不同的起始点开始。这可确保随机函数在程序运行期间不会吐出相同的值。
* **重要的是只在程序开始时调用sCE调用ONCE。**
* 不需要重复调​​用来播种随机数生成器(实际上,它会使您的数字不均匀 分散式)。
* 一种常用的技术是使用时钟为随机数生成器播种,因为每次看时它都会为您提供不同的输出。因此,对于种子,您可以获取时间输出并将其插入随机数生成器。
* time函数将返回计算机的时间。这表示为数量 自1970年1月1日大纪元以来已经过去的秒数。
* 函数时间NULL将返回计算机时间内经过的秒数。
* 时间函数必须包含的头文件:\`ctime'。
代码段:
```cpp
#include <ctime>
srand(time(NULL));
cout << rand();
/*
Output: (Will differ from computer to computer, and because of the seed, will also differ from time to time, literally. :D)
1696269016
*/
```
每次运行程序时,这会产生不同的值。
额外奖励:根据您的方便调整兰特()。
* 由于rand返回一个从0到RAND\_MAX的随机数如果你想要一个0到8之间的数字那么 -rand9。 任何模9的数字都将返回0到8之间的值。
* 更正式的是如果你想要一个L包括和U包括之间的数字你必须这样做 `int num = L + rand()%(U-L+1).` 说明: - randUL + 1返回0和UL的随机伪随机别忘记数字。因此添加L确保我们得到L和U之间的值。
摘要:-
1. int rand返回0到RAND\_MAX之间的随机数。
2. void srandint seed用于为随机数生成器设定种子。仅调用此函数_一次_就足够了。
### 来源: - [随机数生成](http://www.math.uaa.alaska.edu/~afkjm/csce211/handouts/RandomFunctions)

View File

@@ -0,0 +1,30 @@
---
title: Range For Loop
localeTitle: 循环范围
---
## 基于范围的循环
基于范围的`for`循环允许在一系列元素(如容器中的元素)上轻松循环。
使用传统的`for`循环:
```cpp
std::vector<std::string> stringList {"one", "two", "three"};
for (size_t il; il < stringList.size(); il++
{
std::cout << stringList.at(il) << std::endl;
}
```
使用基于范围的`for`循环:
```cpp
std::vector<std::string> stringList {"one", "two", "three"};
for (auto& singleString : stringList)
{
std:cout << singleString << std::endl;
}
```

View File

@@ -0,0 +1,94 @@
---
title: Set
localeTitle: 组
---
c ++中的集合数据结构的定义方式与在数学上下文中定义集合的方式相同。
更正式地说,集合是一种关联容器,其中每个元素必须是唯一的。
* 输入元素后无法修改元素的值但允许删除元素并插入新元素就像我们在mathenatics中一样。
* 设置数据结构可用于建模,并设置自身。很容易找到交叉点,工会等。
* 与vector类似但只允许使用唯一值。
* 当您将元素插入集合时Set会按递增顺序排列元素。
使用set数据结构所需的头文件是'set'。即, `#include<set>`必须在您的代码中以便您使用set数据结构。
**专业提示** - 使用`#include<bits/stdc++.h>`包含所有C ++数据结构和函数,而不是逐个添加它们。
可以使用set执行的一些功能 -
1. begin - 返回集合中第一个元素的迭代器
2. end - 返回到集合中最后一个元素后面的理论元素的迭代器
3. size - 返回集合中的元素数
4. max\_size - 返回集合可容纳的最大元素数
5. empty - 返回集合是否为空
6. eraseconst g - 从集合中删除值“g”
7. clear - 从集合中删除所有元素
让我们看一个例子: -
```cpp
#include <iostream>
#include <set>
#include <iterator>
using namespace std;
int main()
{
set <int> myset; //an empty set container. Note that size of the set need not be declared, similar to vector.
// insert elements in random order
myset.insert(65);
myset.insert(30);
myset.insert(80);
myset.insert(20);
myset.insert(9);
myset.insert(9); // only one 9 will be added to the list.
// printing set myset
set <int> :: iterator itr; //an iterator is like a pointer.
cout << "\nThe contents of myset : ";
for (itr = myset.begin(); itr != myset.end(); ++itr)
{
cout << '\t' << *itr;
}
cout << endl;
// remove all elements up to 65 in myset from the beginning:-
cout << "\nContents of myset after removal of elements less than 30 : ";
myset.erase(myset.begin(), myset.find(30));
for (itr = myset.begin(); itr != myset.end(); ++itr)
{
cout << '\t' << *itr;
}
// remove element with value 50 in myset
int num = myset.erase(80); //returns true (and deletes) if 80 is there in the list else returns 0.
cout<<"\n\n After doing myset.erase(80), "<<num<<" element is removed\n\n";
cout<<"Contents of the modified set:\t";
for (itr = myset.begin(); itr != myset.end(); ++itr)
{
cout << '\t' << *itr;
}
cout << endl;
return 0;
}
```
\`\`\`CPP 输出: - myset的内容9 20 30 65 80
移除少于30:30的元素后的myset内容65 80
做myset.erase80删除1个元素
修改集的内容30 65 \`\`\`
###来源
1. [极客的极客](https://www.geeksforgeeks.org/set-in-cpp-stl/)

View File

@@ -0,0 +1,174 @@
---
title: stack
localeTitle: 堆
---
## 堆栈
`stack`是C ++中最常用的容器之一。容器是一种存储对象集合的数据结构,有些是有序的,有些则不是。所有容器都有一组不同的功能,允许您访问该集合中的对象。
`std::stack`是C ++标准库的一部分(因此前缀为`std::` 允许您以后进先出LIFO顺序存储数据。注意 **堆栈中的所有对象必须具有相同的数据类型**
存储在堆栈中的数据类型位于stack关键字旁边的尖括号内。例如如果要存储整数集合则堆栈将为`std::stack<int> stack_name`
### 堆栈LIFO说明
`stack`允许我们按特定顺序推送和弹出。 **推送**意味着将对象插入堆栈顶部。 **Pop**意味着从堆栈顶部拉出最后插入的对象。因此,当您按下它时,它位于顶部,当您弹出时,您将提取最后插入的元素。
![alt text](https://github.com/mohammadaziz313/helloworld/blob/master/Lifo_stack.png "LIFO Stack Push和Pop示例")
### 堆栈操作
堆栈容器支持以下操作:
*
* 流行的
*
* 尺寸
* 背部
#### 推
允许您在当前顶部元素上方的堆栈顶部插入新元素。
```cpp
//Push operation in Stack
#include <iostream> // std::cout
#include <stack> // std::stack
int main ()
{
std::stack<int> s;
s.push(1); //Pushing 1 at top of the stack
s.push(2); //Pushing 2 at top of the stack
return 0;
}
```
#### 最佳
允许您访问顶部元素而无需从堆栈中删除它。
```cpp
//Top operation in Stack
#include <iostream> // std::cout
#include <stack> // std::stack
int main ()
{
std::stack<int> s;
s.push(1); //Pushing 1 at top of the stack
s.push(2); //Pushing 2 at top of the stack
std::cout<<s.top()<<'\n'; //Accessing the top of the stack
std::cout<<s.top()<<'\n'; //Accessing the top of the stack
return 0;
}
```
```
Output:
2
2
```
#### 流行的
移除堆栈顶部的元素,有效地将堆栈的大小减少一个。
```cpp
//Pop operation in Stack
#include <iostream> // std::cout
#include <stack> // std::stack
int main ()
{
std::stack<int> s;
s.push(1); //Pushing 1 at top of the stack
s.push(2); //Pushing 2 at top of the stack
std::cout<<s.top()<<'\n'; //Accessing the top of the stack
s.pop(); //Removing element from the top of stack
std::cout<<s.top()<<'\n'; //Accessing the top of the stack
return 0;
}
```
```
Output:
2
1
```
#### 尺寸
返回`stack`的元素数。
```cpp
//Size operation in Stack
#include <iostream> // std::cout
#include <stack> // std::stack
int main ()
{
std::stack<int> s;
s.push(1); //Pushing 1 at top of the stack
s.push(2); //Pushing 2 at top of the stack
std::cout<<s.size()<<'\n'; //Showing the size of the stack
s.pop(); //Removing element from the top of stack
std::cout<<s.size()<<'\n'; //Showing the size of the stack
s.pop(); //Removing element from the top of stack
std::cout<<s.size()<<'\n'; //Showing the size of the stack
return 0;
}
```
```
Output:
2
1
0
```
#### 空
返回`stack`是否为空,即堆栈大小是否为零。 它返回`true` 如果堆栈的大小0否则返回`false`
```cpp
//Empty operation in Stack
#include <iostream> // std::cout
#include <stack> // std::stack
int main ()
{
std::stack<int> s;
s.push(1);
s.push(2);
while(s.empty() != false){
std::cout<<s.top()<<'\n';
s.pop();
}
std::cout<<"Out of loop"<<'\n';
return 0;
}
```
```
Output:
2
1
Out of loop
```

View File

@@ -0,0 +1,43 @@
---
title:Switch Statement
localeTitle: undefined
---
switch语句允许测试变量与值列表的相等性。每个值都称为一个案例并且针对每种情况检查要打开的变量。
句法: switch表达式{ case constant-expression 声明S; 打破; //可选的 case constant-expression 声明S; 打破; //可选的
//您可以拥有任意数量的案例陈述。 default//可选 声明S; }
以下规则适用于switch语句 -
switch语句中使用的表达式必须具有整数或枚举类型或者是类类型其中类具有单个转换函数为整数或枚举类型。
您可以在交换机中包含任意数量的case语句。每个案例后跟要与之比较的值和冒号。
case的constant-expression必须与switch中的变量具有相同的数据类型并且必须是常量或文字。
当接通的变量等于大小写时该大小写之后的语句将一直执行直到达到break语句。
当达到break语句时交换机终止控制流跳转到switch语句后面的下一行。
并非每个案例都需要包含休息时间。如果没有出现中断,则控制流将进入后续情况,直到达到中断。
switch语句可以有一个可选的默认大小写它必须出现在交换机的末尾。当没有任何情况为真时默认情况可用于执行任务。默认情况下不需要中断。
例: \`\`\`C ++
# 包括
使用命名空间std;
int main{ //局部变量声明: char grade ='D';
开关(等级){ 案例'A' cout <<“太棒了!” << endl; 打破; 案例'B' 案例'C' cout <<“做得好”<< endl; 打破; 案例'D' cout <<“你通过了”<< endl; 打破; 案例'F' cout <<“最好再试一次”<< endl; 打破; 默认值 cout <<“无效等级”<< endl; } cout <<“你的成绩是”<< grade << endl;
返回0; }\`\`\`
输出 你通过了 你的成绩是D
### 来源
https://www.tutorialspoint.com

View File

@@ -0,0 +1,142 @@
---
title: IDE and Printing different text
localeTitle: IDE和打印不同的文本
---
# IDE简介和打印不同的文本
* 在上一篇文章中有一些编程所需软件的下载链接。像这样的软件被称为IDE。 **IDE代表集成开发环境**
## IDE主要由3种软件组成
**1编辑器**略微修改的文本编辑器使编码变得简单。用于编码的编辑器的示例是Notepad ++。
**2调试器**帮助您在程序中查找错误并在执行前解决错误的软件。想象一下FaceBook在加载应用程序或游戏突然崩溃时崩溃。为了防止错误执行程序调试器是程序员最好的朋友。
**3编译器**编译器是计算机的一部分它将高级程序代码转换为简单的机器代码0s和1s;这样计算机就可以理解命令并执行它们。从现在开始,我们将经常使用**编译器**这个词。
_问尝试在Google上搜索IDE并在其上运行您的第一个程序。检查输出_
现在安装IDE并尝试在上一篇文章中更改程序中的文本。
### 在C ++上更改文本
* 要更改文本,请更改`cout<<`后面的`""`输入的内容
示例程序:
```cpp
#include <iostream>
using namespace std :
int main()
{
cout << "I Love freeCodeCamp ! ";
}
```
上面的代码返回一个错误因为在第2行我们使用了冒号:)而不是分号(; 那么,让我们调试错误:
```C++
#include <iostream>
using namespace std ;
int main()
{
cout << "I Love freeCodeCamp ! ";
return 0;
}
```
请注意,现在程序运行完美。 输出将是: `I Love freeCodeCamp!`
### 现在,让我们将文本更改为其他类似的内容:
```cpp
cout << "Hello World!\t I love freeCodeCamp!";
```
这次输出会有所不同:
```
Hello World! I love freeCodeCamp!
```
如果您意识到, `\t`命令在两个文本之间创建了一个_制表符空间_ 。这是C ++中的一种特殊命令。这些特殊命令称为_Escape Sequences_ 。 它们用于打印编译器无法显示的某些特殊字符。
#### 有用的转义序列:
* `\'`打印一个倒置的逗号
* `\"`打印双倒逗号
* `\n`在新行上打印
* `\t`为水平制表符
* `\f`为新页面
* `\\`反斜杠
* `\?`一个问号
##### 现在,让我们尝试使用一些转义序列打印数字和特殊字符:
```cpp
cout << "40158 \t 236708 ! \n \\ @ \?" << endl;
```
输出更改为:
```
40158 236708 !
\ @ ?
```
##### 让我们尝试一些其他打印方式:
```cpp
cout << "1+2" << endl;
cout << 1+2 << endl;
```
输出:
* 第一个输出语句是`1+2`
* 第二个输出语句是`3`
这是因为我们没有为第二个print语句添加引号因此编译器在打印之前添加了数字。
#### 注释:
* 注释是许多编程语言的重要特征。它们允许程序员记笔记以获得自助,并且不会影响程序的运行。
**注释的不同类型的注释和语法**
1 `//` 〜 _单行注释_ 这些注释的长度为1行键入的行。 2 `/* */` 〜 _多行注释_ 这些注释可占用_多行_的空格。
#### 使用评论的示例:
\`\`\`CPP cout <<“你好评论”<< endl; // cout <<“你好评论”<< endl; 单行评论
```
/* This is an example of a multi line comment. No output is generated for this .
I now end the comment. :) */
```
\`\`\`
输出将是
`Hello Comment`
您可能会注意到在程序执行期间忽略注释并且在检查程序输出时不会显示注释 应该注意的是虽然注释确实为一个代码添加了额外的可读性但是过于依赖注释来描述代码中的逻辑是一个坏习惯一般来说您的代码应该说明一切并反映程序员的意图
您可能会注意到在程序执行期间忽略注释并且在检查程序输出时不会显示注释
#### 运营商
* 运算符允许您比较两个或更多表达式
* `==`等于
* `!=`不等于
* `<`小于
* `>`大于
* `<=`小于或等于
* `>=`大于或等于
```cpp
(7==5);
```
评估为false
`cpp (7!=5);` 评估结果为true

View File

@@ -0,0 +1,74 @@
---
title: The Auto Feature
localeTitle: 自动功能
---
## 自动功能
`auto`是一种C ++ 11特性它允许编译器在定义中为您推断数据类型。这可以为您节省大量的打字特别是对于复杂的类型。
没有`auto`
```cpp
double x = 10.425;
double y = x * x;
```
有了`auto`
```cpp
double x = 10.425;
auto y = x * x;
```
虽然它看似微不足道,但当数据类型开始变得复杂时,它变得非常有用。例如,假设您要存储员工的[`vector`](https://guide.freecodecamp.org/cplusplus/vector) ,并且您只对他们的姓名和年龄感兴趣。一种方法来存储姓名和年龄可能是`pair`一个`string``unsigned int` 。这被声明为`std::vector<std::pair<std::string, unsigned int>> employees` 。现在假设您要访问最后添加的员工:
```cpp
std::vector<std::pair<std::string, unsigned int>> employees;
// without auto, you have to write:
std::pair<std::string, unsigned int>> last_employee = employees.back();
// with auto, you just have to write:
auto last_employee = employees.back();
```
一旦编译器确定了`=`右侧的类型,它就会用该类型替换`auto`
在C ++的现代版本中从C ++ 14开始 `auto`也可以在函数声明中用作返回类型。然后编译器将从函数内部的return语句中推断返回类型。以员工为例
```
std::vector<std::pair<std::string, unsigned int>> employees;
auto get_last_employee() {
return employees.back(); // Compiler knows the return type from this line.
}
```
编译器将从返回语句的行知道函数的返回类型应该是`std::vector<std::pair<std::string, unsigned int>>`
虽然非常技术性,但[auto上](http://en.cppreference.com/w/cpp/language/auto)的[cppreference页面](http://en.cppreference.com/w/cpp/language/auto)描述了更多的`auto`用法以及何时可以使用和不能使用的详细信息。
### 在C ++ 11之前的`auto`
在一些包含_非常_旧代码的旧教科书中关键字`auto`以非常不同的方式使用。
这个特定的`auto`是一个从C借用的关键字可能是有史以来用得最少的关键字。
在C ++中所有变量都有_自动持续时间_ ,也就是说,它们被定义,直到你离开它们定义的函数。
例如:
\`\`\`CPP
# 包括
int main { int a; a = 1; //有意义,因为它是在同一个函数中定义的
```
return 0;
```
} a = 2; //没有意义,因为这里没有定义 \`\`\`
这是在C ++中给出的,并且`auto`指定变量应该具有_自动持续时间_ ,因此缺乏使用。
## 进一步阅读:
* http://www.stroustrup.com/C++11FAQ.html#auto

View File

@@ -0,0 +1,95 @@
---
title: C++ If Statement
localeTitle: C ++ If Statement
---
# IF声明。
**if语句有什么作用**
* `if`语句计算括号内存在的测试表达式。
* `if`语句使用关系运算符和逻辑运算符来生成逻辑表达式。
* * *
`if`语句的一般形式:
```cpp
if (Test Expression / Condition)
{
// Block of statements if test expression is True
}
```
如果测试表达式的值为**true** ,则为块 执行if语句中的代码。
如果测试表达式的值为**false** ,则为块 跳过if语句中的代码并继续执行代码。
示例`if`语句:
```cpp
int a = 10;
// true statement
if (a < 20)
{
// execute this block of code
}
// false statement
if (a < 0)
{
// Skip this block of code.
}
```
示例在C ++中:
```cpp
// Program to check if number entered by the user is positive
// If negative, the block of code is skipped
#include <iostream>
using namespace std;
int main()
{
int no ;
cout << "Enter a number: ";
cin >> no;
// if statement to check if the number is positive
if ( no > 0)
{
cout << "You have entered a positive number: " << no << endl;
}
// If number is not positive, then if statement is skipped a program continues
cout << "This step is always printed" << endl;
return 0;
}
```
**输出:**
输出1
```
Enter a number: 5
You have entered a positive number: 5
This step is always printed
```
This is the output when the number entered is positive.
OUTPUT 2:
```
输入一个数字:-1 始终打印此步骤 \`\`\` 这是输入数字为负数时的输出。
[亲自尝试一下代码吧! :)](https://repl.it/Mg9X)
_祝贺。祝贺。这是关于IF声明的文章的结尾_
**祝大家好运**
**快乐的编码! :)**

View File

@@ -0,0 +1,186 @@
---
title: Operators
localeTitle: 运营商
---
# 经营者:
* 操作员允许您对数据执行操作。
* 正在操作的数据称为_操作数_ 。
* C ++中不同类型的运算符是:
* _OPERANDS_是操作员执行某些命令的数据。
* 运算符有3种类型一元适用于1个操作数二元适用于2个操作数三元适用于3个操作数
### 1 I / O操作符 -
* 这些运算符允许您直接输入和输出。
## 输入曝气器“>>”##
用于从标准输入读取数据“cin”语句
## 输出运算符“<<
用于在`cout`语句中发送输出。
### 2算术运算符 -
* 这些运算符允许您执行基本的算术运算。
1. `+`运算符_添加_两个操作数。
2. `-`运算符_减去_两个操作数。
3. `*`运算符将两个操作数_相乘_ 。
4. `/`运算符_除以_并给出两个操作数的_商_ 。
5. `%`运算符_除以_并给出两个操作数的_其余部分_ 。 (或者,对于更具数学倾向的读者, `a % b`基本上是“a mod b”的结果
### 使用算术运算符的示例:
\`\`\`CPP
# 包括
使用命名空间std;
int main { int a = 5; //第一个操作数 int b = 10; //第二个操作数
```
cout << "+ operator " << a+b << "\n"; //Add
cout << "- operator " << ab << "\n"; //Subtract
cout << "* operator " << a*b << "\n"; //Multiply
cout << "/ operator " << b/a << "\n"; //Find Quotient
cout << "modulus operator " << b%a << "\n"; //Find remainder
return 0;
```
} \`\`\`
输出:
```
+ operator 15
- operator -5
* operator 50
/ operator 2
modulus operator 0
```
[亲自尝试一下代码吧! :)](https://repl.it/Mge9)
### 增量运算符:
* `++`被称为增量运算符。它将整数变量的值增加1。
2种增量
* 预增量首先递增该值然后使用它。示例: `int a ; ++a;`
* 后增量首先使用变量然后递增它。示例: `int b; b++;`
### 减量运算符:
* `--`被称为减量运算符。它将整数变量的值减1。
减少的两种类型:
* 预递减首先递减该值然后使用它。示例: `int a ; --a;`
* 后递减首先使用变量然后递减它。示例: `int b; b--;`
递增和递减运算符的示例:
```cpp
#include <iostream>
using namespace std;
int main()
{
int a = 3 ,b = 4;
// INCREMENT
cout<< "Value of int a PRE INCREMENTED : " << ++a << "\n";
cout<< "Value of int b POST INCREMENTED : " << b++ << "\n";
cout<< "Value of b is changed after using once : " << b << "\n";
// DECREMENT
cout << "\n"; //go to next line
a = 10; //Assigning a new value to a
b = 10; //Assigning a new value to b
cout << "Value of int a PRE DECREMENTED : " << --a << "\n";
cout << "Value of int b POST DECREMENTED : " << b-- << "\n";
cout << "Value of b is changed after using once : " << b << "\n";
return 0;
}
```
输出:
```
Value of int a PRE INCREMENTED : 4
Value of int b POST INCREMENTED : 4
Value of b is changed after using once : 5
Value of int a PRE DECREMENTED : 9
Value of int b POST DECREMENTED : 10
Value of b is changed after using once : 9
```
[亲自尝试一下代码吧! :)](https://repl.it/Mgg4/2)
### 3关系运算符
* 这些运算符告诉我们两个操作数之间的关系并返回一个布尔值0或1。如果关系为`true`则结果为1。如果实现是假的则结果为0。
* 6个关系运算符是
1. 小于`<`
2. 大于`>`
3. 小于或等于`<=`
4. 大于或等于`>=`
5. 等于`==`
6. 不等于`!=`
### 4逻辑运算符
* 这些运算符组合了逻辑运算的表达式他们是
1. 逻辑AND `&&` 如果两个值都为真则求值为true。
2. 逻辑OR `||` 如果任何值为true则求值为true。
3. 逻辑不`!` 如果_表达式_为true则_表达式_为false。该运算符反转了真值是一元运算符。
### 5.三元运营商:
`?:`运算符是三元运算符或_条件运算符_ ,因为它可以用来替换`if else`语句,甚至是`if else if`语句。 语法:
`condition ? ValueIfTrue : ValueIfFalse` 。这扩展到:
```cpp
if(condition)
ValueIfTrue;
else ValueIfFalse;
```
调用`ValueIfTrue`值有点错误,因为它不必是数字。像这样的东西:
`condition ? FirstLevelTrueValue : ConditionIfFalse ? SecondLevelTrueValue : SecondLevelFalseValue`也有效,并且`if else if`语句被解释为以下`if else if`语句:
```cpp
if(condition)
FirstLevelTrueValue;
else if(ConditionIfFalse)
SecondLevelTrueValue;
else SecondLevelFalseValue;
```
类似地,嵌套的`if`语句也可以使用三元运算符。
_Camper你现在知道什么是令牌了。下一篇文章将是关于_ _恭喜_
**祝大家好运**
**快乐的编码! :)**

View File

@@ -0,0 +1,208 @@
---
title: Tokens Part 1
localeTitle: 代币第1部分
---
### 什么是令牌?
标记是程序的最小单元,对编译器很重要。有不同种类的令牌:
* 关键词
* 运营商
* 标点符号
* 字面
* 身份标识
* **令牌的组合形成表达**
### 什么是变量?
* 教科书定义:变量是指定数据可以更改的内存位置。
* 但我希望你把变量想象成一个类似于盒子的东西,如下所示: ![](https://i.imgur.com/YdbgWHL.png)
所以,例如: 我正在转移到一个新的地方,我需要把我的东西安排在盒子里。因此,我想到了两件事情, **盒子里会存储什么样的东西,所以盒子的大小是已知的(数据类型)** **我如何识别盒子?(命名变量)**
因此我们知道C ++中的变量需要_名称_和_数据类型_并且可以更改存储在其中的值。
### C ++中的数据类型:
在c ++中声明变量时,它们必须具有您稍后将要使用的名称,值(常量或非常量)和类型。 该类型将告诉编译器变量可以使用的值可能的操作并将在memmory中保存一定的空间。 在c ++中有两种类型的数据:
* 简单的类型
* 结构类型
### 简单的数据类型
* 布尔 - 布尔 像开关一样工作,可以打开或关闭。
* 人物 - 炭火 存储单个字符。
* 整数 - 整数 存储[整数](https://en.wikipedia.org/wiki/Integer) 。
* 浮点 - 浮点数 他们可以使用小数。
* 双浮点 - 双 浮点型的双精度。
在这里你可以看到一些例子:
```cpp
bool GameRunning = true;
char a;
int x = 2;
```
#### 这些类型也可以使用修饰符进行修改,例如:
签 无符号 短 长
### 结构数据类型
#### 身份标识。
* 标识符是赋予变量或类或函数或任何用户定义函数的名称。
## 命名变量的规则:
* 用AZ或az中的字母开始命名。
* 数字可以跟随您的第一个字母,但我们无法开始用数字命名。
* 不允许使用空格或特殊字符而是使用UNDERSCORE \_。
#### 声明变量:
语法如下 < _数据类型_ > < _变量名称_ >; 要么 < _data type_ > < _variable name_ > = < _value_ >;如果我们也想初始化变量。
例如 `cpp int a ; //declaring a variable named 'a' of type integer. a=4; //initializing a variable int b = 5 ; //declaring and initializing a variable 'b' of type integer.`
**声明变量的示例:**
```cpp
int a9;
char A;
double area_circle;
long l;
```
**声明变量的错误方法** -
```cpp
int 9a;
char -a;
double area of circle;
long l!!;
```
* 变量名称不能以数字开头
* 不允许有特殊字符
* 不允许有空格
您可以想象不同尺寸的不同盒子,并将不同的东西存储为不同的变量。
**注意:**
1. **C ++编译器忽略空格,它们通常用于美化代码,以便任何程序员调试或理解代码。**
2. **如果未初始化变量,则它包含垃圾值。让我举个例子:**
### 变量范围
所有变量都具有其功能区域,并且在该边界之外它们不保持其值,该边界称为变量的范围。对于大多数情况,它在花括号之间,其中变量被声明为存在变量,而不是在变量之外。我们稍后会研究存储类,但到目前为止,我们可以将变量大致分为两种主要类型,
\*全球变量。
\*局部变量。
#### 全局变量
全局变量是那些一旦声明并且可以在程序的整个生命周期中由任何类或任何函数使用。它们必须在main函数之外声明。如果仅声明则可以在程序生命周期的不同时间为它们分配不同的值。但即使它们在main函数之外同时声明和初始化也可以在程序的任何一点为它们分配任何值。
示例:仅声明,未初始化。
```cpp
#include <iostream>
using namespace std;
int x; // Global variable declared
int main()
{
x=10; // Initialized once
cout <<"first value of x = "<< x;
x=20; // Initialized again
cout <<"Initialized again with value = "<< x;
}
```
#### 局部变量
局部变量是仅存在于其声明的花括号之间的变量。在外面它们不可用并导致编译时错误。
示例:
```cpp
#include <iostream>
using namespace std;
int main()
{
int i=10;
if(i<20) // if condition scope starts
{
int n=100; // Local variable declared and initialized
} // if condition scope ends
cout << n; // Compile time error, n not available here
}
```
### 常数变量
常量变量是无法更改的变量。例如如果您的代码中需要“pi”则不希望在初始化后更改它。
示例:
```cpp
#include <iostream>
using namespace std;
const double PI = 3.14159253;
int main()
{
//Calculating the area of a circle, using user provided radius
double radius;
//input and output explained in other guide
cin>>radius;
//pi*r^2
double area = PI*radius*radius;
cout<<area<<endl;
}
```
### 变量中的垃圾值
如果未初始化变量,则它包含垃圾值。例如:
所以就盒子而言,你可以把它想象成 -
![](https://i.imgur.com/YdbgWHL.png)
\`\`\`CPP #包括 使用命名空间std; int main { int a; cout <<“垃圾值中的一个:”<< a << endl; //声明类型为integer的名为'a'的变量 一个= 5; //初始化变量 cout <<“一个新的价值”<< a << endl;
} \`\`\`
### 输出是:
```
Garbage value in a : 0
New value in a : 5
```
正如您所看到的在我们给它一个值这里它是0之前已经有一个值存储在'a'这应该保留在每个程序员的脑海中这样当使用变量时它们不会创建逻辑错误并打印垃圾值
[亲自尝试一下代码吧! :)](https://repl.it/Mg7j)
#### 关键字:
_关键字是为编译器传达特殊含义的保留字。它们**不能**用于在c ++中命名。_ 关键字示例 内联运算符私有intdoublevoidchar模板使用虚拟休息案例开关朋友等
**这些关键字中的每一个都用于C ++中的特殊功能。**
_令牌第1部分结束了。在Tokens的[第2部分](https://guide.freecodecamp.org/cplusplus/tokens-part-II)见到你的露营者:)_
**祝大家好运**
**快乐的编码! :)**

View File

@@ -0,0 +1,120 @@
---
title: Variables
localeTitle: 变量
---
让我们讨论一些被称为变量的东西。变量就像一个桶。你可以把东西放进去然后改变它 之后需要的时候。 在C ++中,有许多类型的变量,如整数,字符串,布尔值等等。 让我们看一个使用整数变量的简单程序。整数存储正数负数或零的整数。整数不是分数例如1 / 2,1 / 4和1/5。让我们看一个使用整数的简单程序 变量。
```cpp
#include <iostream>
using namespace std ;
int main()
{
int a; // Declare an integer variable a
a = 5; // Assign value of 5 to variable a
cout << a; // Display the value of variable a which contains 5
return 0;
}
```
执行此程序时屏幕上会显示5
* 请注意,在上面的程序中//放在行之后。符号“//”用于注释我们的代码。符号后面的代码 “//”不会在其放置的行中被执行。
* 在第5行声明了一个简单的整数变量。
* 在第6行值5被赋给变量a。现在每当我们在程序中使用变量a时它的值将为5 除非我们改变它。
* 在第7行我们显示变量a的值并在屏幕上打印5。
### 变量范围
所有变量都具有其功能区域,并且在该边界之外它们不保持其值,该边界称为变量的范围。对于大多数情况,它在花括号之间,其中变量被声明为存在变量,而不是在变量之外。我们稍后会研究存储类,但到目前为止,我们可以将变量大致分为两种主要类型,
\*全球变量。
\*局部变量。
#### 全局变量
全局变量是那些一旦声明并且可以在程序的整个生命周期中由任何类或任何函数使用。它们必须在main函数之外声明。如果仅声明则可以在程序生命周期的不同时间为它们分配不同的值。但即使它们在main函数之外同时声明和初始化也可以在程序的任何一点为它们分配任何值。
示例:仅声明,未初始化。
```cpp
include <iostream>
using namespace std;
int x; // Global variable declared
int main()
{
x=10; // Initialized once
cout <<"first value of x = "<< x;
x=20; // Initialized again
cout <<"Initialized again with value = "<< x;`
}
```
#### 局部变量
局部变量是仅存在于其声明的花括号之间的变量。在外面它们不可用并导致编译时错误。
示例:
```cpp
include <iostream>
using namespace std;
int main()
{
int i=10;
if(i<20) // if condition scope starts
{
int n=100; // Local variable declared and initialized
} // if condition scope ends
cout << n; // Compile time error, n not available here
}
```
现在让我们来看看一种新型的变量 -
#### 静态变量
静态变量当一个变量声明为static时它的空间将被分配给程序的生命周期。即使多次调用该函数静态变量的空间也只分配一次前一次调用中的变量值通过下一个函数调用传递。这对于在C / C ++或需要存储先前函数状态的任何其他应用程序中实现协同程序非常有用。 通俗地说,这意味着当超出范围时,正常变量会失去它的标识(值),但静态变量具有全局范围并保留其值直到程序结束,但与全局变量不同,没有必要声明它在计划开始时。
#### 额外-
Static是C ++中的一个关键字,用于为元素赋予特殊的特征。静态元素在静态存储区域的程序生命周期中仅分配存储一次。他们有一个范围,直到程序生命周期。
#### 码-
```
#include <iostream>
#include <string>
using namespace std;
void howstaticworks()
{
static int count = 0; // static variable
cout << count << " ";
/* value is updated and
will be carried to next
function calls*/
count++;
}
int main()
{
for (int i=0; i<5; i++)
howstaticworks();
return 0;
}
```
#### 试试自己
只需复制代码并将其粘贴到给定的链接中即可。 在IDE上运行 - https://ideone.com/
输出: 0 1 2 3 4
您可以在上面的程序中看到变量count被声明为static。因此它的值通过函数调用来传递。每次调用函数时变量计数都不会被初始化。
让我们通过删除“static”关键字来尝试相同的代码并猜测输出并将其与IDE上的一个进行比较。 静态现在转换为正常变量

View File

@@ -0,0 +1,211 @@
---
title: Vectors
localeTitle: 矢量
---
## 矢量
C ++ `vector`是C ++中最常用的容器之一。容器是一种数据结构,它存储一组对象,这些对象可以从有序(如`vector` !)到无序(如`set` 变化。所有C ++容器都有一组不同的函数,允许您访问该集合中的对象,修改和循环该数据结构中的元素。
向量类似于Java中的ArrayLists因为您不必指定容器的长度。与必须定义大小的数组相比它的大小取决于其内容。
`std::vector`是C ++标准库的一部分(因此前缀为`std::` ,允许您存储相同数据类型的连续数据。注意: **向量中的所有对象必须具有相同的数据类型**
存储在矢量中的数据类型位于vector关键字旁边的尖括号内。例如如果要存储字符串集合则向量将为`std::vector<std::string> vector_name`
_注意_ :每当使用向量时,您必须包含向量库!
`#include <vector>`
### 矢量建筑
有许多方法可以构建一个向量。
使用初始化列表 - 对象列在一组大括号中: `{ }`
```cpp
std::vector<int> a{1, 2, 3, 4, 5}; // a is a vector of 5 ints: 1, 2, 3, 4 and 5
std::vector<std::string> b{"hello", "world"}; // b is a vector of 2 strings: "hello" and "world"
std::vector<bool> v; // v is an empty vector
```
从另一个向量构造它(这被称为复制构造)
```cpp
std::vector<double> a{1.0, 2.0, 3.0};
std::vector<double> b(a); // b is a vector of 3 doubles: 1.0, 2.0 and 3.0
```
使用相同的元素初始化它:
```cpp
std::vector<int> a(100, -1); // a is a vector of 100 elements all set to -1
```
### 矢量迭代器
迭代器可以被认为是专门用于导航容器的指针 (如矢量)。最重要的迭代器是`begin()``end()``begin()`返回指向向量中第一个项的指针,而`end()`指向 到向量中最后一项之后的一个位置。因此循环通过 矢量可以做到:
```cpp
std::vector<int> vec{1, 2, 3};
for(auto vec_it = vec.begin(); vec_it != vec.end(); it++){
// since vec_it is a pointer and points to the memory address of the item
// inside the vector, vec_it must be dereferenced using '*'
std::cout << *it << '\n';
}
/* Output
1
2
3
*/
```
### 修改矢量
将项目推送到矢量:
```cpp
std::vector<int> vec; // constructs an empty vector
for (int i = 0; i < 10; i = i + 2){
vec.push_back(i);
}
// vec now holds [0, 2, 4, 6, 8]
```
将项目插入特定位置略有不同。 C ++向量插入 函数适用于迭代器。它会在给定项之前将给定项插入一个位置 迭代器。
```cpp
std::vector<unsigned int> vec{3, 400, 12, 45};
auto iter = vec.begin() + 2; // iter now points to '12'
vec.insert(iter, 38); // inserts '38' before '12'
// vec: [3, 400, 38, 12, 45]
```
### 元素访问
标准库提供了用于访问向量中特定元素的不同函数。
```cpp
std::vector<std::string> a{"test", "element", "access"};
std::string first_item = a.front(); // gets the first item of the vector ("test")
std::string last_item = a.back(); // gets the last item in the vector ("access")
// To get an element at a specific index (remember: vector indices start at 0)
std::string second_item = a.at(2); // gets "element"
// OR
std::string second_item = a[2]; // gets "element"
```
### 循环在`vector`元素
循环使用C ++ `std::vector`中的元素与在JavaScript或Ruby中的向量中循环元素完全不同。由于C ++是C的精简抽象因此您只能使用称为迭代器的这些漂亮的小变量来遍历元素以访问每个元素。 迭代器通常以指针的形式出现,指针是存储另一个变量的内存地址的变量。您可以[在此处](https://www.tutorialspoint.com/cplusplus/cpp_pointers.htm)了解有关指针的更多信息。 但是,因为迭代器充当指针(反之亦然),为了查看它们指向的内容,您需要将其取消引用为适当类型的变量。 我们如何做到这一点? 这里。我们。走!
```cpp
std::vector<std::string> a{"test", "element", "access"};
for(auto it = v.begin(); it != v.end(); it++) { //notice use of auto keyword
cout<<*it<<endl; //Will print out string that the iterator is currently ppointing to
}
```
从这里开始,你可以做各种很酷的事情,比如操纵矢量或者随便点一下它的顺序!
### 一些有用的成员函数
标准模板库STL还为您提供了不同的_方法_
```cpp
std::vector.size(); // returns the size of the vector (the number of positions in the vector)
std::vector.begin(); // returns an iterator which is a pointer to the beginning of the vector
std::vector.end(); // returns an iterator which is a pointer to the end of the vector
std::vector.empty(); // returns true if the vector is empty, otherwise returns false.
std::vector.front(); // returns the first element of the vector.
std::vector.back(); // returns the last element of the vector.
std::vector.push_back(n); // inserts the element "n" to the end of the vector.
std::vector.pop_back(n); // removes the last element of the vector
```
### 矢量迭代器
迭代器提供了另一种访问向量中元素的方法。
迭代器声明。
```cpp
std::vector<int> v;
//Iterator delcaration for the above vector will correspond to
std::vector<int>::iterator it;
```
使用迭代器使用for循环打印向量的元素
```cpp
for(it=v.begin(); it!=v.end(); ++it)
//std::vector::begin and std::vector::end return iterator pointing to first and last element of the vector respectively.
cout<<*it;
```
### 通过向量迭代
有不同的方法来迭代向量并访问其内容。以下形式是等效的第一个涉及使用基于范围的表达式自C ++ 11以来第二个使用迭代器最后一个是基于索引的迭代
\`\`\`cpp
# 包括
# 包括
//首先声明向量 的std ::矢量 myVector {1,2,3,4,5}; // a是5个整数的向量1,2,3,4和5
//使用基于范围的循环自C ++ 11起 forint elementmyVector{//读取类似于“myVector中的每个元素” std :: cout <<“元素是”<< element << std :: endl; }
//使用迭代器 的std ::矢量 :: iterator它; //声明迭代器 forit = myVector.begin; it= myVector.end; ++ it{ std :: cout <<“元素是”<< \* it << std :: endl; //取消引用迭代器以访问其数据 }
//使用索引 用于标准::矢量 :: size\_type i = 0; i= myVector.size;++{ std :: cout <<“元素是”<< myVector \[i\] << std :: endl; //取消引用迭代器以访问其数据 }
```
### Sorting A Vector In Ascending Order
Sorting a vector based on ascending order can be done with the help of Sort() in C++.
```
CPP
# 包括
# 包括
# 包括
使用命名空间std;
int main{
向量 v {10,5,82,69,64,70,3,42,28,0}; sortv.beginv.end;
cout <<“向量内容按升序排序\\ n”; forint ev{ cout << e <<“”; }
返回0; }
```
### Sorting Vector In Descending Order
Sorting Vector in descending order can be done with the help of third argument namely greater<int>() in Sort() in C++.
```
CPP
# 包括
# 包括
# 包括
使用命名空间std;
int main{
向量 v {10,5,82,69,64,70,3,42,28,0}; sortv.beginv.end更大 ;
cout <<“向量内容按升序排序\\ n”; forint ev{ cout << e <<“”; }
返回0; } \`\`\`

View File

@@ -0,0 +1,37 @@
---
title:While-loop
localeTitle: undefined
---
只要给定条件为真while循环语句就会重复执行目标语句。
句法: whilecondition{ 声明S; }
while循环的一个关键点是循环可能永远不会运行。 当测试条件并且结果为假时将跳过循环体并且将执行while循环之后的第一个语句。
例:
```C++
#include <iostream>
using namespace std;
int main () {
// Local variable declaration:
int a = 10;
// while loop execution
while( a < 20 ) {
cout << "value of a: " << a << endl;
a++;
}
return 0;
}
```
输出:
价值10 价值11 价值12 价值a13 a的值14 价值a15 价值16 价值17 价值18 价值19
### 来源
www.tutorialspoint.com