C++primer 14.3.2节练习

练习14.18

  1 class String
  2 {
  3     friend bool operator<(const String &s1, const String &s2);
  4     friend bool operator<=(const String &s1, const String &s2);
  5     friend bool operator>(const String &s1, const String &s2);
  6     friend bool operator>=(const String &s1, const String &s2);
  7     //其他成员
  8 };
  9 
 10 friend bool operator<(const String &s1, const String &s2)
 11 {
 12     return strcmp(s1.str, s2.str) < 0;
 13 }
 14 friend bool operator<=(const String &s1, const String &s2)
 15 {
 16     return strcmp(s1.str, s2.str) <= 0;
 17 }
 18 friend bool operator>(const String &s1, const String &s2)
 19 {
 20     return strcmp(s1.str, s2.str) > 0;
 21 }
 22 friend bool operator>=(const String &s1, const String &s2)
 23 {
 24     return strcmp(s1.str, s2.str) >= 0;
 25 }
 26 
 27 class StrBlob
 28 {
 29     friend bool operator<(const StrBlob &s1, const StrBlob &s2);
 30     friend bool operator<=(const StrBlob &s1, const StrBlob &s2);
 31     friend bool operator>(const StrBlob &s1, const StrBlob &s2);
 32     friend bool operator>=(const StrBlob &s1, const StrBlob &s2);
 33 };
 34 bool operator<(const StrBlob &s1, const StrBlob &s2)
 35 {
 36     return *s1.data < *s2.data;
 37 }
 38 bool operator<=(const StrBlob &s1, const StrBlob &s2)
 39 {
 40     return *s1.data <= *s2.data;
 41 }
 42 bool operator>(const StrBlob &s1, const StrBlob &s2)
 43 {
 44     return *s1.data > *s2.data;
 45 }
 46 bool operator>=(const StrBlob &s1, const StrBlob &s2)
 47 {
 48     return *s1.data >= *s2.data;
 49 }
 50 
 51 class StrBlobPtr
 52 {
 53     friend operator<(const StrBlobPtr &s1, const StrBlobPtr &s2);
 54     friend operator<=(const StrBlobPtr &s1, const StrBlobPtr &s2);
 55     friend operator>(const StrBlobPtr &s1, const StrBlobPtr &s2);
 56     friend operator>=(const StrBlobPtr &s1, const StrBlobPtr &s2);
 57 };
 58 bool operator<(const StrBlobPtr &s1, const StrBlobPtr &s2)
 59 {
 60     auto l = s1.wptr.lock(), r = s2.wptr.lock();
 61     if (l == r)
 62     {
 63         if (!r)
 64             return false;
 65         return (s1.curr < s2.curr);
 66     }
 67     else
 68         return false;
 69 }
 70 bool operator<=(const StrBlobPtr &s1, const StrBlobPtr &s2)
 71 {
 72     auto l = s1.wptr.lock(), r = s2.wptr.lock();
 73     if (l == r)
 74         return (!r || s1.curr <= s2.curr);
 75     else
 76         return false;
 77 }
 78 bool operator>(const StrBlobPtr &s1, const StrBlobPtr &s2)
 79 {
 80     auto l = s1.wptr.lock(), r = s2.wptr.lock();
 81     if (l == r)
 82     {
 83         if (!r)
 84             return false;
 85         return (s1.curr > s2.curr);
 86     }
 87     else
 88         return false;
 89 }
 90 bool operator>=(const StrBlobPtr &s1, const StrBlobPtr &s2)
 91 {
 92     auto l = s1.wptr.lock(), r = s2.wptr.lock();
 93     if (l == r)
 94         return (!r || s1.curr >= s2.curr);
 95     else
 96         return false;
 97 }
 98 
 99 class StrVec
100 {
101     friend operator<(const StrVec &s1, const StrVec &s2);
102     friend operator<=(const StrVec &s1, const StrVec &s2);
103     friend operator>(const StrVec &s1, const StrVec &s2);
104     friend operator>=(const StrVec &s1, const StrVec &s2);
105     //其他成员
106 };
107 bool operator<(const StrVec &s1, const StrVec &s2)
108 {
109     for (auto p1 = s1.begin(), p2 = s2.begin(); p1 != s1.end(), p2 != s2.end(); ++p1, ++p2)
110     {
111         if (*p1 < *p2)
112             return true;
113         else if (*p1 > *p2)
114             return false;
115     }
116     if (p1 == s1.end() && p2 != s2.end())
117         return true;
118     return false;
119 }
120 bool operator<=(const StrVec &s1, const StrVec &s2)
121 {
122     for (auto p1 = s1.begin(), p2 = s2.begin(); p1 != s1.end(), p2 != s2.end(); ++p1, ++p2)
123     {
124         if (*p1 < *p2)
125             return true;
126         else if (*p1 > *p2)
127             return false;
128     }
129     if (p1 == s1.end())
130         return true;
131     return false;
132 }
133 bool operator>(const StrVec &s1, const StrVec &s2)
134 {
135     for (auto p1 = s1.begin(), p2 = s2.begin(); p1 != s1.end(), p2 != s2.end(); ++p1, ++p2)
136     {
137         if (*p1 < *p2)
138             return false;
139         else if (*p1 > *p2)
140             return true;
141     }
142     if (p1 == s1.end() && p2 != s2.end())
143         return true;
144     return false;
145 }
146 bool operator>=(const StrVec &s1, const StrVec &s2)
147 {
148     for (auto p1 = s1.begin(), p2 = s2.begin(); p1 != s1.end(), p2 != s2.end(); ++p1, ++p2)
149     {
150         if (*p1 < *p2)
151             return false;
152         else if (*p1 > *p2)
153             return true;
154     }
155     if (p2 == s2.end())
156         return true;
157     return false;
158 }

 

posted @ 2017-09-10 20:30  五月份小姐  阅读(262)  评论(0编辑  收藏  举报