Skip to content

Commit

Permalink
docs: 优化范围、迭代器、算法的阅读体验
Browse files Browse the repository at this point in the history
  • Loading branch information
FeignClaims committed Sep 23, 2024
1 parent e1bc3af commit 07a9f72
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 17 deletions.
11 changes: 5 additions & 6 deletions faq/range_iterator_and_algorithm/input_iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ class Iter {
Iter(int const* pointer) : pointer_(pointer) {}
// Iter(Iter const&); // 隐式生成的拷贝函数即可满足需要

friend bool operator==(Iter const& lhs,
Iter const& rhs) { // iter == end
friend bool operator==(Iter const& lhs, Iter const& rhs) {
return lhs.pointer_ == rhs.pointer_;
}
friend bool operator!=(Iter const& lhs,
Iter const& rhs) { // iter != end

friend bool operator!=(Iter const& lhs, Iter const& rhs) {
return !(lhs == rhs);
}

int const& operator*() const { // *iter
int const& operator*() const {
return *pointer_;
}

Iter& operator++() { // ++iter
Iter& operator++() {
++pointer_;
return *this;
}
Expand Down
70 changes: 59 additions & 11 deletions faq/range_iterator_and_algorithm/main.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,51 @@

- 通过自增操作 :cpp:`++iter`, 我们获得下一个元素的指针.

那么, 如果我们自定义一个类型, 它支持所有这些操作, 将会怎么样?
那么, 如果我们自定义一个类型, 它包装一个指针从而支持所有这些操作, 将会怎么样?

首先, 我们让这个自定义类型 :cpp:`Iter` 存储一个指针:

.. literalinclude:: input_iterator.cpp
:lines: 1-3, 22-25
:dedent:
:language: cpp
:linenos:

然后, 我们使用运算符重载让 :cpp:`Iter` 支持上面所说的所有操作.

支持拷贝
.. literalinclude:: input_iterator.cpp
:lines: 4
:dedent:
:language: cpp
:linenos:

支持相等性比较
.. literalinclude:: input_iterator.cpp
:lines: 6-12
:dedent:
:language: cpp
:linenos:

支持解引用
.. literalinclude:: input_iterator.cpp
:lines: 14-16
:dedent:
:language: cpp
:linenos:


支持自增操作
.. literalinclude:: input_iterator.cpp
:lines: 18-21
:dedent:
:language: cpp
:linenos:

故迭代器 :cpp:`Iter` 的定义如下:

.. literalinclude:: input_iterator.cpp
:dedent:
:language: cpp
:linenos:

Expand Down Expand Up @@ -386,19 +428,25 @@

.. code-block:: cpp
:linenos:
:caption: 读入范围中下一个元素
template <typename T>
void f() {
while (true) {
T value;
cin >> value; // 读入范围中下一个元素
if (cin.fail()) { // 判断是否已经遍历到范围外
break;
}
use(value); // 使用元素
}
T value;
cin >> value;
.. code-block:: cpp
:linenos:
:caption: 判断是否已经遍历到范围外
if (cin.fail()) {
break;
}
.. code-block:: cpp
:linenos:
:caption: 使用元素
auto result = process_value(value);
注意到,

- :cpp:`cin >> value` 可以对应于迭代器自增操作.
Expand Down

0 comments on commit 07a9f72

Please sign in to comment.