fix: converted single to triple backticks5 (#36232)

This commit is contained in:
Randell Dawson
2019-06-20 14:14:23 -07:00
committed by Tom
parent 63a0fa745b
commit fce8901c56
75 changed files with 1299 additions and 1139 deletions

View File

@@ -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 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());
`
```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` عدة مرات على نفس الحاوية يولد الكثير من النفقات العامة من تحريك العناصر.
@@ -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
*/
```
### مصادر