#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
class mystr {
public:
mystr(const char* str)
: s(0), len(strlen(str))
{
if(len != 0)
newAndCopy(s, str, len + 1);
}
mystr(const mystr& as)
: s(0), len(as.len)
{
if(len != 0)
newAndCopy(s, as.s, len + 1);
}
bool operator < (const mystr& rhs)
{
return strcmp( s, rhs.s ) < 0;
}
bool operator > (const mystr& rhs)
{
return strcmp( s, rhs.s ) > 0;
}
bool operator == (const mystr& rhs)
{
return strcmp( s, rhs.s ) == 0;
}
mystr& operator = (const mystr& as)
{
if(&as == this)
return *this;
if(len > 0)
delete [] s;
len = as.len;
s = 0;
if(len != 0)
newAndCopy(s, as.s, len + 1);
}
friend ostream& operator << ( ostream& os, const mystr& rhs )
{
return os << rhs.s;
}
~mystr() { if(s) delete [] s; }
const char* c_str() const { return s; }
size_t size() const { return len; }
private:
static void newAndCopy(char*& dest, const char* src, int len)
{
dest = new char[len];
strcpy(dest, src);
}
char* s;
size_t len;
};
#define CMP( s1, s2, op ) cout << setw(20) << s1 << #op"\t" << setw(20) << s2 << ":\t" << (s1 op s2) << endl;
int main()
{
mystr s1("oh, at last");
mystr s2("frostmoan hungers");
mystr s3("speak fool!");
mystr s4(s2);
cout.setf(ios::boolalpha | ios::left);
CMP( s1, s2, > );
CMP( s2, s3, < );
CMP( s1, s3, == );
CMP( s2, s4, == );
return 0;
}
专家你好!你帮我看一下这个程序,第八行最开始那个冒号是什么意思哦?O(∩_∩)O谢谢你~