Skip to content

Commit

Permalink
[faq] 解释范围、迭代器和算法
Browse files Browse the repository at this point in the history
  • Loading branch information
FeignClaims committed Feb 5, 2024
1 parent 7fd73d9 commit 7f5ebf1
Show file tree
Hide file tree
Showing 12 changed files with 1,391 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/_static/links.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
.. _`cppreference: string`: https://zh.cppreference.com/w/cpp/string/basic_string#.E6.88.90.E5.91.98.E5.87.BD.E6.95.B0
.. _`cppreference: vector`: https://zh.cppreference.com/w/cpp/container/vector#.E6.88.90.E5.91.98.E5.87.BD.E6.95.B0
.. _`cppreference: array`: https://zh.cppreference.com/w/cpp/container/array#.E6.88.90.E5.91.98.E5.87.BD.E6.95.B0
.. _`cppreference: algorithm`: https://zh.cppreference.com/w/cpp/header/algorithm

.. _`《人的正确思想是从哪里来的?》`: https://zhuanlan.zhihu.com/p/467228101
.. _`《实践论》`: https://www.marxists.org/chinese/maozedong/marxist.org-chinese-mao-193707.htm
Expand Down
31 changes: 31 additions & 0 deletions docs/faq/range_iterator_and_algorithm/cin_iterator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>
using namespace std;

template <typename T>
class Cin_iterator {
public:
Cin_iterator(bool is_end) : value_() {
if (!is_end) {
cin >> value_;
}
}

friend bool operator==(Cin_iterator const&, Cin_iterator const&) {
return cin.fail();
}
friend bool operator!=(Cin_iterator const& lhs, Cin_iterator const& rhs) {
return !(lhs == rhs);
}

T const& operator*() const {
return value_;
}

Cin_iterator& operator++() {
cin >> value_;
return *this;
}

private:
T value_;
};
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions docs/faq/range_iterator_and_algorithm/forward_list_iterator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Forward_list_iterator {
public:
Forward_list_iterator(Node const* node) : node_(node) {}

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

int const& operator*() const {
return node_->value;
}

Forward_list_iterator& operator++() {
node_ = node_->next;
return *this;
}

private:
Node const* node_;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Forward_list_iterator {
public:
Forward_list_iterator(Node const* node);

friend bool operator==(Forward_list_iterator const& lhs,
Forward_list_iterator const& rhs);
friend bool operator!=(Forward_list_iterator const& lhs,
Forward_list_iterator const& rhs);

int const& operator*() const {
return node_->value;
}

Forward_list_iterator& operator++() {
node_ = node_->next;
return *this;
}

private:
Node const* node_;
};
5 changes: 5 additions & 0 deletions docs/faq/range_iterator_and_algorithm/forward_list_node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
struct Node {
public:
int value;
Node* next;
};
26 changes: 26 additions & 0 deletions docs/faq/range_iterator_and_algorithm/input_iterator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Iter {
public:
Iter(int const* pointer) : pointer_(pointer) {}
// Iter(Iter const&); // 隐式生成的拷贝函数即可满足需要

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

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

Iter& operator++() { // ++iter
++pointer_;
return *this;
}

private:
int const* pointer_;
};
Loading

0 comments on commit 7f5ebf1

Please sign in to comment.