|
| 1 | +#include <iostream> |
| 2 | +#include <cstdlib> |
| 3 | +#include <cstring> |
| 4 | + |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +#include <string> |
| 8 | +#include <vector> |
| 9 | +#include <algorithm> |
| 10 | +#define qsort(a,b,c,d) myqsort(a,b,c,d) |
| 11 | + |
| 12 | +class MyString : public string |
| 13 | +{ |
| 14 | +public: |
| 15 | + MyString() {}; |
| 16 | + //1.0继承类继承父类所有的成员变量和成员函数,但不继承构造函数和析构函数 |
| 17 | + //1.1继承类的无参构造函数,会隐式调用父类的无参构造函数 |
| 18 | + MyString(const char * st) :string(st) {}; |
| 19 | + //1.2继承类的有参构造函数,如果父类也有有参构造函数,则必须显示调用它 |
| 20 | + //2.0这里的参数根据reference有两种选择,此处必须用const char*,"xxx"的类型是const char* |
| 21 | + MyString(const MyString& s):string(s){} |
| 22 | + //1.3继承类的复制构造函数必须要显示的调用父类的复制构造函数,不然就会默认调用父类的无参构造函数 |
| 23 | + MyString operator +(MyString & op2) |
| 24 | + { |
| 25 | + string s1 = *this; |
| 26 | + string s2 = op2; |
| 27 | + string s = s1 + s2; |
| 28 | + return *new MyString(s.c_str()); |
| 29 | + } |
| 30 | + MyString & operator +(const char * cs2) |
| 31 | + { |
| 32 | + string str1 = *this; |
| 33 | + string s = str1 + cs2; |
| 34 | + return *new MyString(s.c_str()); |
| 35 | + } |
| 36 | + |
| 37 | + MyString & operator()(int s, int l) |
| 38 | + { |
| 39 | + string str = substr(s, l); |
| 40 | + return *new MyString(str.c_str()); |
| 41 | + } |
| 42 | +}; |
| 43 | + |
| 44 | +void print_test(MyString s) |
| 45 | +{ |
| 46 | + cout << s << endl; |
| 47 | +} |
| 48 | + |
| 49 | +int myqsort(MyString * ms,int size,int length,int (*f)(const void *,const void *)) |
| 50 | +{ |
| 51 | + vector<MyString> vec; |
| 52 | + |
| 53 | + for (int i=0; i< size; i++) |
| 54 | + vec.push_back(ms[i]); |
| 55 | + |
| 56 | + sort(vec.begin(), vec.end()); |
| 57 | + |
| 58 | + for (int i=0; i<size; i++) |
| 59 | + //cout << vec[i] << endl; |
| 60 | + ms[i]=vec[i]; |
| 61 | + return 0; |
| 62 | + |
| 63 | +} |
| 64 | + |
| 65 | +MyString operator+(const char * op1, MyString & op2) |
| 66 | +{ |
| 67 | + string st2 = op2; |
| 68 | + string s = op1 + st2; |
| 69 | + return *new MyString(s.c_str()); |
| 70 | +} |
| 71 | + |
| 72 | +int CompareString(const void * e1, const void * e2) |
| 73 | +{ |
| 74 | + MyString * s1 = (MyString *)e1; |
| 75 | + MyString * s2 = (MyString *)e2; |
| 76 | + if (*s1 < *s2) return -1; |
| 77 | + else if (*s1 == *s2) return 0; |
| 78 | + else if (*s1 > *s2) return 1; |
| 79 | +} |
| 80 | + |
| 81 | +int main() |
| 82 | +{ |
| 83 | + MyString s1("abcd-"), s2, s3("efgh-"); |
| 84 | + MyString s4(s1); |
| 85 | + MyString SArray[4] = { "big","me","about","take" }; |
| 86 | + //这里等号右边的赋值操作相当于调用了MyString的转换构造函数,其实就是单一非const classname&参数的构造函数可以直接接受参数类型的变量 |
| 87 | + cout << "1. " << s1 << s2 << s3 << s4 << endl; |
| 88 | + s4 = s3; |
| 89 | + //3.0 operator=可以直接用string类里面的 |
| 90 | + s3 = s1 + s3; |
| 91 | + s1+s3; |
| 92 | + cout << "2. " << s1 << endl; |
| 93 | + cout << "3. " << s2 << endl; |
| 94 | + cout << "4. " << s3 << endl; |
| 95 | + cout << "5. " << s4 << endl; |
| 96 | + cout << "6. " << s1[2] << endl; |
| 97 | + s2 = s1; |
| 98 | + s1 = "ijkl-"; |
| 99 | + s1[2] = 'A'; |
| 100 | + cout << "7. " << s2 << endl; |
| 101 | + cout << "8. " << s1 << endl; |
| 102 | + s1 += "mnop"; |
| 103 | + cout << "9. " << s1 << endl; |
| 104 | + s4 = "qrst-" + s2; |
| 105 | + cout << "10. " << s4 << endl; |
| 106 | + s1 = s2 + s4 + " uvw " + "xyz"; |
| 107 | + cout << "11. " << s1 << endl; |
| 108 | + |
| 109 | + qsort(SArray, 4, sizeof(MyString), CompareString); |
| 110 | + //myqsort(SArray, 4, sizeof(MyString), CompareString); |
| 111 | + |
| 112 | + for (int i = 0; i < 4; ++i) |
| 113 | + cout << SArray[i] << endl; |
| 114 | + cout << s1(0, 4) << endl; |
| 115 | + cout << s1(5, 10) << endl; |
| 116 | + return 0; |
| 117 | +} |
0 commit comments