-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7fd73d9
commit 7f5ebf1
Showing
12 changed files
with
1,391 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
docs/faq/range_iterator_and_algorithm/forward_list_iterator.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_; | ||
}; |
21 changes: 21 additions & 0 deletions
21
docs/faq/range_iterator_and_algorithm/forward_list_iterator_simplified.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
struct Node { | ||
public: | ||
int value; | ||
Node* next; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_; | ||
}; |
Oops, something went wrong.