Skip to content

Commit

Permalink
docs: 补充 ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
FeignClaims committed Nov 10, 2024
1 parent 925da82 commit dbb8cc4
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions faq/range_iterator_and_algorithm/main.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,61 @@

—— Alexander Stepanov `《Efficient Programming with Components》`_

========================================================================================================================
C++20 ranges
========================================================================================================================

在 C++20, 我们有了 ranges, 将能更简单地使用范围、迭代器、算法:


.. tabs::

.. tab:: 计数

:godbolt:`nEM8981ro`

.. code-block:: cpp
:linenos:
// 计数有多少个 3
int count = std::ranges::count(range, 3);
// 计数有多少平方为 4 的数
int count = std::ranges::count(range, 4, [](int value) { return value * value; } );
// 计数有多少个偶数
int count = std::ranges::count_if(range, [](int value) { return value % 2 == 0; });
.. tab:: 遍历所有偶数

:godbolt:`PndcMsE19`

.. code-block:: cpp
:linenos:
for (auto value :
range | std::views::filter([](int value) { return value % 2 == 0; })) {
/* 使用 value */
}
.. tab:: 输出前 3 个正数

:godbolt:`Efz3Pxn6M`

.. code-block:: cpp
:linenos:
// C++20
std::cout << std::format("{}\n",
range
| std::views::filter([](int value) { return value > 0; })
| std::views::take(3));
// C++23
std::println("{}\n", range
| std::views::filter([](int value) { return value > 0; })
| std::views::take(3));
========================================================================================================================
涉及的标准库内容
========================================================================================================================
Expand Down

0 comments on commit dbb8cc4

Please sign in to comment.