Loading docs/lang/op-overload.md +50 −56 Original line number Diff line number Diff line Loading @@ -33,25 +33,20 @@ C++ 自带的运算符,最初只定义了一些基本类型的运算规则。 #include <iostream> #include <queue> using namespace std; struct student { struct student { string name; int score; }; struct cmp { bool operator()(const student&a,const student&b)const { struct cmp { bool operator()(const student& a, const student& b) const { return a.score < b.score || (a.score == b.score && a.name > b.name); } }; priority_queue<student, vector<student>, cmp> pq; int main() { int main() { int n; cin >> n; for(int i=1;i<=n;i++) { for (int i = 1; i <= n; i++) { string name; int score; cin >> name >> score; Loading Loading @@ -86,24 +81,20 @@ int main() #include <iostream> #include <queue> using namespace std; struct student { struct student { string name; int score; bool operator<(const student&a)const { bool operator<(const student& a) const { return score < a.score || (score == a.score && name > a.name); // 上面省略了 this 指针,完整表达式如下: // this->score<a.score||(this->score==a.score&&this->name>a.name); } }; priority_queue<student> pq; int main() { int main() { int n; cin >> n; for(int i=1;i<=n;i++) { for (int i = 1; i <= n; i++) { string name; int score; cin >> name >> score; Loading @@ -118,11 +109,14 @@ int main() 事实上,只要有了 `<` 运算符,则其他五个比较运算符的重载也可以很容易实现。 ```cpp bool operator< (const T& lhs, const T& rhs){ /* 这里重载小于运算符 */ } bool operator<(const T& lhs, const T& rhs) { /* 这里重载小于运算符 */ } bool operator>(const T& lhs, const T& rhs) { return rhs < lhs; } bool operator<=(const T& lhs, const T& rhs) { return !(lhs > rhs); } bool operator>=(const T& lhs, const T& rhs) { return !(lhs < rhs); } bool operator==(const T& lhs, const T& rhs){ return !(lhs < rhs)&&!(lhs > rhs); } bool operator==(const T& lhs, const T& rhs) { return !(lhs < rhs) && !(lhs > rhs); } bool operator!=(const T& lhs, const T& rhs) { return !(lhs == rhs); } ``` Loading Loading
docs/lang/op-overload.md +50 −56 Original line number Diff line number Diff line Loading @@ -33,25 +33,20 @@ C++ 自带的运算符,最初只定义了一些基本类型的运算规则。 #include <iostream> #include <queue> using namespace std; struct student { struct student { string name; int score; }; struct cmp { bool operator()(const student&a,const student&b)const { struct cmp { bool operator()(const student& a, const student& b) const { return a.score < b.score || (a.score == b.score && a.name > b.name); } }; priority_queue<student, vector<student>, cmp> pq; int main() { int main() { int n; cin >> n; for(int i=1;i<=n;i++) { for (int i = 1; i <= n; i++) { string name; int score; cin >> name >> score; Loading Loading @@ -86,24 +81,20 @@ int main() #include <iostream> #include <queue> using namespace std; struct student { struct student { string name; int score; bool operator<(const student&a)const { bool operator<(const student& a) const { return score < a.score || (score == a.score && name > a.name); // 上面省略了 this 指针,完整表达式如下: // this->score<a.score||(this->score==a.score&&this->name>a.name); } }; priority_queue<student> pq; int main() { int main() { int n; cin >> n; for(int i=1;i<=n;i++) { for (int i = 1; i <= n; i++) { string name; int score; cin >> name >> score; Loading @@ -118,11 +109,14 @@ int main() 事实上,只要有了 `<` 运算符,则其他五个比较运算符的重载也可以很容易实现。 ```cpp bool operator< (const T& lhs, const T& rhs){ /* 这里重载小于运算符 */ } bool operator<(const T& lhs, const T& rhs) { /* 这里重载小于运算符 */ } bool operator>(const T& lhs, const T& rhs) { return rhs < lhs; } bool operator<=(const T& lhs, const T& rhs) { return !(lhs > rhs); } bool operator>=(const T& lhs, const T& rhs) { return !(lhs < rhs); } bool operator==(const T& lhs, const T& rhs){ return !(lhs < rhs)&&!(lhs > rhs); } bool operator==(const T& lhs, const T& rhs) { return !(lhs < rhs) && !(lhs > rhs); } bool operator!=(const T& lhs, const T& rhs) { return !(lhs == rhs); } ``` Loading