fix: converted single to triple backticks5 (#36232)
This commit is contained in:
@@ -38,49 +38,50 @@ localeTitle: صب
|
||||
|
||||
## أمثلة
|
||||
|
||||
`#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;
|
||||
}
|
||||
`
|
||||
```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;
|
||||
}
|
||||
```
|
||||
@@ -18,12 +18,13 @@ localeTitle: تعليمات كود نظيفة
|
||||
|
||||
بدلاً من استخدام المتغيرات العامة ، يمكنك استخدام المتغيرات التي تم الإعلان عنها في الوظائف والتي يمكن أن تساعدك في تحديد القيم التي يتم تمريرها وتحديد الأخطاء بشكل أسرع.
|
||||
|
||||
`#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
// Global variables are declared outside functions
|
||||
int cucumber; // global variable "cucumber"
|
||||
`
|
||||
```cpp
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
// Global variables are declared outside functions
|
||||
int cucumber; // global variable "cucumber"
|
||||
```
|
||||
|
||||
## استخدام الانتقال والمتابعة وما إلى ذلك.
|
||||
|
||||
@@ -39,27 +40,29 @@ localeTitle: تعليمات كود نظيفة
|
||||
|
||||
عادة ما تكون هناك أعمال حول هذا تبدو أكثر وضوحا وأقل إرباكا ، على سبيل المثال. بينما الحلقات. فعل:
|
||||
|
||||
`int i=1;
|
||||
while (i <= 5)
|
||||
{
|
||||
if (i == 2)
|
||||
i = 4;
|
||||
|
||||
++i;
|
||||
}
|
||||
`
|
||||
```cpp
|
||||
int i=1;
|
||||
while (i <= 5)
|
||||
{
|
||||
if (i == 2)
|
||||
i = 4;
|
||||
|
||||
++i;
|
||||
}
|
||||
```
|
||||
|
||||
بدلا من:
|
||||
|
||||
`for (int i = 1; i <= 5; i++)
|
||||
{
|
||||
if (i == 2)
|
||||
{
|
||||
i = 4;
|
||||
}
|
||||
// Do work
|
||||
}
|
||||
`
|
||||
```cpp
|
||||
for (int i = 1; i <= 5; i++)
|
||||
{
|
||||
if (i == 2)
|
||||
{
|
||||
i = 4;
|
||||
}
|
||||
// Do work
|
||||
}
|
||||
```
|
||||
|
||||
## أعلن الثوابت وأنواع في الجزء العلوي
|
||||
|
||||
@@ -75,17 +78,19 @@ localeTitle: تعليمات كود نظيفة
|
||||
|
||||
بدلا من:
|
||||
|
||||
`for (int i = 1; i <= 5; i++)
|
||||
//CODE
|
||||
`
|
||||
```cpp
|
||||
for (int i = 1; i <= 5; i++)
|
||||
//CODE
|
||||
```
|
||||
|
||||
فعل:
|
||||
|
||||
`for (int i = 1; i <= 5; i++)
|
||||
{
|
||||
//CODE
|
||||
}
|
||||
`
|
||||
```cpp
|
||||
for (int i = 1; i <= 5; i++)
|
||||
{
|
||||
//CODE
|
||||
}
|
||||
```
|
||||
|
||||
## توصيات أخرى
|
||||
|
||||
@@ -96,7 +101,8 @@ localeTitle: تعليمات كود نظيفة
|
||||
* \#### كتابة const في أحرف استهلالية ، أنواع البيانات تبدأ بـ T والمتغيرات في الحالة الصغيرة.
|
||||
|
||||
|
||||
`const int MAX= 100; //Constant
|
||||
typedef int TVector[MAX]; //Data type
|
||||
TVector vector; //Vector
|
||||
`
|
||||
```cpp
|
||||
const int MAX= 100; //Constant
|
||||
typedef int TVector[MAX]; //Data type
|
||||
TVector vector; //Vector
|
||||
```
|
||||
@@ -13,26 +13,28 @@ localeTitle: مشغل شرطي
|
||||
|
||||
هنا ، يتم تقييم expression-1 عندما يكون الشرط صحيحًا ويتم تقييم expression-2 عندما تكون condtion خاطئة. عبارة if-else مشابهة ستكون:
|
||||
|
||||
`if(condition)
|
||||
{
|
||||
expression-1;
|
||||
}
|
||||
else
|
||||
{
|
||||
expression-2;
|
||||
}
|
||||
`
|
||||
```cpp
|
||||
if(condition)
|
||||
{
|
||||
expression-1;
|
||||
}
|
||||
else
|
||||
{
|
||||
expression-2;
|
||||
}
|
||||
```
|
||||
|
||||
ومن ثم فإن العامل الشرطي سهل للغاية عندما تحتاج إلى كتابة عبارة if-else بسيطة. ويمكن أيضا أن تستخدم في # تعريف preprocessor عندما يتم استخدام حالة مماثلة في أماكن متعددة.
|
||||
|
||||
على سبيل المثال ، يمكن استخدام الحد الأقصى لعدد المشغلين الشرطيين على النحو التالي:
|
||||
|
||||
`#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
|
||||
`
|
||||
```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
|
||||
```
|
||||
|
||||
**حظا سعيدا لكم جميعا**
|
||||
|
||||
|
||||
@@ -8,24 +8,25 @@ localeTitle: مسح-إزالة لغة
|
||||
|
||||
### مقارنة
|
||||
|
||||
`// 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 erase–remove 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());
|
||||
`
|
||||
```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 erase–remove 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` عدة مرات على نفس الحاوية يولد الكثير من النفقات العامة من تحريك العناصر.
|
||||
|
||||
@@ -33,51 +34,52 @@ localeTitle: مسح-إزالة لغة
|
||||
|
||||
### مثال
|
||||
|
||||
`#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
|
||||
*/
|
||||
`
|
||||
```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
|
||||
*/
|
||||
```
|
||||
|
||||
### مصادر
|
||||
|
||||
|
||||
@@ -10,18 +10,20 @@ A For Loop هو عبارة تكرارية تستخدم للتحقق من وجو
|
||||
|
||||
## بناء الجملة
|
||||
|
||||
`for ( init; condition; increment ) {
|
||||
statement(s);
|
||||
}
|
||||
`
|
||||
```
|
||||
for ( init; condition; increment ) {
|
||||
statement(s);
|
||||
}
|
||||
```
|
||||
|
||||
من المسموح به لوضع الزيادة insie للحلقة مثل في حلقة في حين. بمعنى أن بناء الجملة مثل هذا يمكن أن يعمل أيضًا.
|
||||
|
||||
`for ( init; condition;) {
|
||||
statement(s);
|
||||
increment;
|
||||
}
|
||||
`
|
||||
```
|
||||
for ( init; condition;) {
|
||||
statement(s);
|
||||
increment;
|
||||
}
|
||||
```
|
||||
|
||||
### فيه
|
||||
|
||||
|
||||
@@ -10,11 +10,12 @@ localeTitle: وظائف في C ++
|
||||
|
||||
## الشكل العام لتعريف الدالة C ++:
|
||||
|
||||
`return_type function_name( parameter list )
|
||||
{
|
||||
body of the function
|
||||
}
|
||||
`
|
||||
```cpp
|
||||
return_type function_name( parameter list )
|
||||
{
|
||||
body of the function
|
||||
}
|
||||
```
|
||||
|
||||
### نوع الإرجاع:
|
||||
|
||||
@@ -34,19 +35,20 @@ localeTitle: وظائف في C ++
|
||||
|
||||
## مثال:
|
||||
|
||||
`int max(int num1, int num2)
|
||||
{
|
||||
// local variable declaration
|
||||
int result;
|
||||
|
||||
if (num1 > num2)
|
||||
result = num1;
|
||||
else
|
||||
result = num2;
|
||||
|
||||
return result;
|
||||
}
|
||||
`
|
||||
```cpp
|
||||
int max(int num1, int num2)
|
||||
{
|
||||
// local variable declaration
|
||||
int result;
|
||||
|
||||
if (num1 > num2)
|
||||
result = num1;
|
||||
else
|
||||
result = num2;
|
||||
|
||||
return result;
|
||||
}
|
||||
```
|
||||
|
||||
## لماذا تعتبر الوظائف مهمة؟
|
||||
|
||||
|
||||
@@ -24,12 +24,13 @@ localeTitle: وظيفة مضمنة
|
||||
|
||||
}
|
||||
|
||||
`## When to use Inline function
|
||||
|
||||
* When the function performs small tasks and is called very often.
|
||||
* When performance is important.
|
||||
* Instead of a macro.
|
||||
`
|
||||
```
|
||||
## When to use Inline function
|
||||
|
||||
* When the function performs small tasks and is called very often.
|
||||
* When performance is important.
|
||||
* Instead of a macro.
|
||||
```
|
||||
|
||||
ج ++
|
||||
|
||||
|
||||
@@ -8,11 +8,12 @@ localeTitle: وظائف مضمنة في C ++
|
||||
|
||||
يوفر C ++ وظائف مضمّنة لتقليل الحمل المكالمة الدالة. دالة مضمنة هي دالة يتم توسيعها في السطر عندما يتم استدعاؤها. عندما يتم استدعاء الدالة المضمنة يتم إدخال رمز كامل للدالة المضمنة أو استبدالها في نقطة استدعاء دالة مضمنة. يتم تنفيذ هذا الاستبدال بواسطة برنامج التحويل البرمجي C ++ في وقت التحويل البرمجي. وظيفة مضمنة قد تزيد من الكفاءة إذا كانت صغيرة. إن صيغة تعريف الدالة المضمنة هي:
|
||||
|
||||
`inline return-type function-name(parameters)
|
||||
{
|
||||
// function code
|
||||
}
|
||||
`
|
||||
```cpp
|
||||
inline return-type function-name(parameters)
|
||||
{
|
||||
// function code
|
||||
}
|
||||
```
|
||||
|
||||
تذكر أن التضمين هو مجرد طلب إلى المحول البرمجي وليس أمرًا. يمكن تجاهل المحول البرمجي طلب forlining. قد لا يؤدّي المحول البرمجي inlining في حالات مثل:
|
||||
|
||||
@@ -107,10 +108,11 @@ localeTitle: وظائف مضمنة في C ++
|
||||
|
||||
انتاج:
|
||||
|
||||
`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
|
||||
`
|
||||
```
|
||||
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
|
||||
```
|
||||
@@ -10,32 +10,35 @@ localeTitle: الحلقات
|
||||
|
||||
\`\` \`ج + + 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:
|
||||
`
|
||||
```
|
||||
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:
|
||||
```
|
||||
|
||||
ج ++ بينما (الشرط) { // القانون الذي سيتم تنفيذه في حين الشرط هو الصحيح } فعل { // سيتم التنفيذ مرة واحدة وحتى تصبح الحالة خاطئة } في حين (شرط) ؛
|
||||
|
||||
`### 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:
|
||||
`
|
||||
```
|
||||
### 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:
|
||||
```
|
||||
|
||||
ج ++ لـ (تهيئة متغير ؛ تحقق من شرط ؛ زيادة المتغير الذي تم تهيئته) { // رمز التنفيذ }
|
||||
|
||||
`Lets write a program which will print numbers from 0 to 1000 including 1000 on the screen using a for loop.
|
||||
`
|
||||
```
|
||||
Lets write a program which will print numbers from 0 to 1000 including 1000 on the screen using a for loop.
|
||||
```
|
||||
|
||||
ج ++ لـ (int i = 0؛ i <= 1000؛ i ++) { cout << i << endl؛ }
|
||||
|
||||
|
||||
@@ -41,11 +41,12 @@ localeTitle: خريطة
|
||||
|
||||
انتاج:
|
||||
|
||||
`a => 10
|
||||
b => 20
|
||||
c => 30
|
||||
d => 40
|
||||
`
|
||||
```
|
||||
a => 10
|
||||
b => 20
|
||||
c => 30
|
||||
d => 40
|
||||
```
|
||||
|
||||
## خلق كائن الخريطة
|
||||
|
||||
|
||||
@@ -12,19 +12,20 @@ localeTitle: البرمجة الشيئية باستخدام C ++
|
||||
|
||||
الكائنات هي كيانات وقت التشغيل الأساسية في نظام موجه للكائنات ، والكائنات هي مثيلات للفئة يتم تعريف هذه أنواع البيانات المعرفة من قبل المستخدم.
|
||||
|
||||
`class person
|
||||
{
|
||||
char name[20];
|
||||
int id;
|
||||
public:
|
||||
void getdetails(){}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
person p1; //p1 is an object
|
||||
}
|
||||
`
|
||||
```cpp
|
||||
class person
|
||||
{
|
||||
char name[20];
|
||||
int id;
|
||||
public:
|
||||
void getdetails(){}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
person p1; //p1 is an object
|
||||
}
|
||||
```
|
||||
|
||||
تشغل الكائنات مساحة في الذاكرة ويكون لها عنوان مرتبط مثل سجل في الباسكال أو البنية أو الاتحاد في C.
|
||||
|
||||
@@ -36,16 +37,17 @@ localeTitle: البرمجة الشيئية باستخدام C ++
|
||||
|
||||
الصف هو مخطط للبيانات والوظائف أو الطرق. الطبقة لا تأخذ أي مساحة.
|
||||
|
||||
`class class_name
|
||||
{
|
||||
private:
|
||||
//data members and member functions declarations
|
||||
public:
|
||||
//data members and member functions declarations
|
||||
protected:
|
||||
//data members and member functions declarations
|
||||
};
|
||||
`
|
||||
```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
|
||||
};
|
||||
```
|
||||
|
||||
Class هو نوع بيانات محدد بواسطة المستخدم مثل بنيات ونقابات في C.
|
||||
|
||||
|
||||
@@ -49,10 +49,11 @@ localeTitle: C ++ التحميل الزائد
|
||||
|
||||
عندما يتم تجميع التعليمات البرمجية المذكورة أعلاه وتنفيذها ، فإنها تنتج النتيجة التالية -
|
||||
|
||||
`Printing int: 5
|
||||
Printing float: 500.263
|
||||
Printing string: Hello C++
|
||||
`
|
||||
```
|
||||
Printing int: 5
|
||||
Printing float: 500.263
|
||||
Printing string: Hello C++
|
||||
```
|
||||
|
||||
### مشغل الحمولة الزائدة في C ++
|
||||
|
||||
@@ -96,5 +97,6 @@ localeTitle: C ++ التحميل الزائد
|
||||
|
||||
الناتج عن البرنامج أعلاه
|
||||
|
||||
`4 + i3
|
||||
`
|
||||
```
|
||||
4 + i3
|
||||
```
|
||||
@@ -57,16 +57,17 @@ rand (): - _إرجاع رقم **pseudo-random** (عدد صحيح) من 0 إلى
|
||||
|
||||
مقتطف الشفرة:
|
||||
|
||||
`#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
|
||||
*/
|
||||
`
|
||||
```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
|
||||
*/
|
||||
```
|
||||
|
||||
هذا ينتج قيم مختلفة في كل مرة يتم تشغيل البرنامج.
|
||||
|
||||
|
||||
@@ -8,20 +8,22 @@ localeTitle: نطاق للحلقة
|
||||
|
||||
مع التقليدية `for` حلقة:
|
||||
|
||||
`std::vector<std::string> stringList {"one", "two", "three"};
|
||||
|
||||
for (size_t il; il < stringList.size(); il++
|
||||
{
|
||||
std::cout << stringList.at(il) << std::endl;
|
||||
}
|
||||
`
|
||||
```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` حلقة:
|
||||
|
||||
`std::vector<std::string> stringList {"one", "two", "three"};
|
||||
|
||||
for (auto& singleString : stringList)
|
||||
{
|
||||
std:cout << singleString << std::endl;
|
||||
}
|
||||
`
|
||||
```cpp
|
||||
std::vector<std::string> stringList {"one", "two", "three"};
|
||||
|
||||
for (auto& singleString : stringList)
|
||||
{
|
||||
std:cout << singleString << std::endl;
|
||||
}
|
||||
```
|
||||
@@ -8,26 +8,29 @@ localeTitle: الميزة التلقائية
|
||||
|
||||
بدون `auto` :
|
||||
|
||||
`double x = 10.425;
|
||||
double y = x * x;
|
||||
`
|
||||
```cpp
|
||||
double x = 10.425;
|
||||
double y = x * x;
|
||||
```
|
||||
|
||||
مع `auto` :
|
||||
|
||||
`double x = 10.425;
|
||||
auto y = x * x;
|
||||
`
|
||||
```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` كـ `std::vector<std::pair<std::string, unsigned int>> employees` . الآن لنفترض أنك تريد الوصول إلى آخر موظف تمت إضافته:
|
||||
|
||||
`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();
|
||||
`
|
||||
```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` بهذا النوع.
|
||||
|
||||
|
||||
@@ -4,16 +4,17 @@ localeTitle: المتغيرات
|
||||
---
|
||||
دعونا نناقش شيء يعرف كمتغيرات. المتغيرات هي بمثابة دلو. يمكنك وضع شيء فيه ثم تغييره بعد ذلك عند الحاجة. في C ++ ، هناك العديد من أنواع المتغيرات مثل الأعداد الصحيحة ، السلاسل ، Booleans وغيرها الكثير. دعونا ننظر إلى برنامج بسيط باستخدام المتغيرات الصحيحة. تخزن أعداد صحيحة الأعداد الصحيحة التي تكون موجبة أو سالبة أو صفر. الأرقام الكاملة ليست أرقام كسور على سبيل المثال 1/2 و 1/4 و 1/5. دعونا ننظر إلى برنامج بسيط يستخدم عددًا صحيحًا متغير.
|
||||
|
||||
`#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;
|
||||
}
|
||||
`
|
||||
```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 معروضة على الشاشة
|
||||
|
||||
|
||||
@@ -22,86 +22,94 @@ _ملاحظة_ : يجب عليك تضمين مكتبة المتجه عند اس
|
||||
|
||||
استخدام قائمة intializer - حيث يتم إدراج الكائنات داخل مجموعة من الأقواس: `{ }`
|
||||
|
||||
`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<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
|
||||
```
|
||||
|
||||
بناءه من متجه آخر (يعرف هذا باسم نسخة البناء)
|
||||
|
||||
`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<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
|
||||
```
|
||||
|
||||
استهلاله بنفس العنصر:
|
||||
|
||||
`std::vector<int> a(100, -1); // a is a vector of 100 elements all set to -1
|
||||
`
|
||||
```cpp
|
||||
std::vector<int> a(100, -1); // a is a vector of 100 elements all set to -1
|
||||
```
|
||||
|
||||
### المتجهات المتجهات
|
||||
|
||||
يمكن اعتبار المتكررات كمؤشرات تستخدم على وجه التحديد للتنقل في الحاويات (مثل المتجهات). تبدأ أهم التكرارات `begin()` `end()` . `begin()` بإرجاع مؤشر إلى العنصر الأول في متجه بينما تشير نقاط `end()` إلى موضع واحد بعد العنصر الأخير في متجه. على هذا النحو من خلال حلقات ناقلات يمكن أن يتم على النحو التالي:
|
||||
|
||||
`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{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
|
||||
*/
|
||||
```
|
||||
|
||||
### تعديل ناقل
|
||||
|
||||
دفع العناصر إلى متجه:
|
||||
|
||||
`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]
|
||||
`
|
||||
```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 ++ تعمل وظيفة على المكرر. وسوف تدرج البند المعطى موضع واحد قبل المعطى مكرر.
|
||||
|
||||
`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<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]
|
||||
```
|
||||
|
||||
### الوصول للعنصر
|
||||
|
||||
توفر المكتبة القياسية وظائف مختلفة للوصول إلى عناصر معينة في متجهك.
|
||||
|
||||
`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"
|
||||
`
|
||||
```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) . ومع ذلك ، لأن التكرارات تعمل كمؤشرات (أو العكس) ، من أجل معرفة ما يشيرون إليه ، تحتاج إلى إدخاله في متغير من نوع appropirate. كيف نفعل ذلك؟ هنا. نحن. اذهب!
|
||||
|
||||
`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
|
||||
}
|
||||
`
|
||||
```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
|
||||
}
|
||||
```
|
||||
|
||||
من هنا ، يمكنك القيام بكل أنواع الأشياء الرائعة ، مثل التلاعب بالمتجه أو العبث مع أمره كما تشاء!
|
||||
|
||||
@@ -109,15 +117,16 @@ _ملاحظة_ : يجب عليك تضمين مكتبة المتجه عند اس
|
||||
|
||||
توفر أيضًا مكتبة القوالب القياسية (STL) _طرقًا_ مختلفة لك:
|
||||
|
||||
`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.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
|
||||
```
|
||||
|
||||
### المتجهات المحاكية
|
||||
|
||||
@@ -125,17 +134,19 @@ _ملاحظة_ : يجب عليك تضمين مكتبة المتجه عند اس
|
||||
|
||||
إعلان Iterator.
|
||||
|
||||
`std::vector<int> v;
|
||||
//Iterator delcaration for the above vector will correspond to
|
||||
std::vector<int>::iterator it;
|
||||
`
|
||||
```cpp
|
||||
std::vector<int> v;
|
||||
//Iterator delcaration for the above vector will correspond to
|
||||
std::vector<int>::iterator it;
|
||||
```
|
||||
|
||||
استخدام المكرر لطباعة عناصر المتجه باستخدام الحلقة
|
||||
|
||||
`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;
|
||||
`
|
||||
```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;
|
||||
```
|
||||
|
||||
### متكررة من خلال ناقل
|
||||
|
||||
@@ -155,9 +166,10 @@ _ملاحظة_ : يجب عليك تضمين مكتبة المتجه عند اس
|
||||
|
||||
// باستخدام المؤشرات ل(الأمراض المنقولة جنسيا :: ناقلات :: size\_type i = 0؛ i! = myVector.size ()؛ ط ++) { std :: cout << "العنصر هو" << myVector \[i\] << std :: endl؛ // Dereference the iterator to access data }
|
||||
|
||||
`### Sorting A Vector In Ascending Order
|
||||
Sorting a vector based on ascending order can be done with the help of Sort() in C++.
|
||||
`
|
||||
```
|
||||
### Sorting A Vector In Ascending Order
|
||||
Sorting a vector based on ascending order can be done with the help of Sort() in C++.
|
||||
```
|
||||
|
||||
حزب الشعب الكمبودي
|
||||
|
||||
@@ -177,9 +189,10 @@ cout << "Vector Contents Sorted In Ascending Order: \\ n"؛ for (int e: v) { cou
|
||||
|
||||
العودة 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++.
|
||||
`
|
||||
```
|
||||
### 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++.
|
||||
```
|
||||
|
||||
حزب الشعب الكمبودي
|
||||
|
||||
|
||||
Reference in New Issue
Block a user