1
+ #include < cstring>
2
+ #include < cstdlib>
3
+ #include < string>
4
+ #include < iostream>
5
+ using namespace std ;
6
+
7
+ // 在此处补充你的代码
8
+ class MyString : public string
9
+ {
10
+ public:
11
+ MyString () :string() {}
12
+ MyString (char *str) :string(str) {}
13
+ MyString (string str) :string(str) {}
14
+ MyString (MyString &mystr) :string(mystr) {}
15
+ MyString operator ()(int start, int length)
16
+ {
17
+ return this ->substr (start, length);
18
+ }
19
+ };
20
+
21
+ int CompareString (const void * e1 , const void * e2 )
22
+ {
23
+ MyString * s1 = (MyString *)e1 ;
24
+ MyString * s2 = (MyString *)e2 ;
25
+ if (*s1 < *s2)
26
+ return -1 ;
27
+ else if (*s1 == *s2)
28
+ return 0 ;
29
+ else if (*s1 > *s2)
30
+ return 1 ;
31
+ }
32
+
33
+ int main ()
34
+ {
35
+ MyString s1 (" abcd-" ), s2, s3 (" efgh-" ), s4 (s1);
36
+ MyString SArray[4 ] = { " big" ," me" ," about" ," take" };
37
+ cout << " 1. " << s1 << s2 << s3 << s4 << endl;
38
+ s4 = s3;
39
+ s3 = s1 + s3;
40
+ cout << " 2. " << s1 << endl;
41
+ cout << " 3. " << s2 << endl;
42
+ cout << " 4. " << s3 << endl;
43
+ cout << " 5. " << s4 << endl;
44
+ cout << " 6. " << s1[2 ] << endl;
45
+ s2 = s1;
46
+ s1 = " ijkl-" ;
47
+ s1[2 ] = ' A' ;
48
+ cout << " 7. " << s2 << endl;
49
+ cout << " 8. " << s1 << endl;
50
+ s1 += " mnop" ;
51
+ cout << " 9. " << s1 << endl;
52
+ s4 = " qrst-" + s2;
53
+ cout << " 10. " << s4 << endl;
54
+ s1 = s2 + s4 + " uvw " + " xyz" ;
55
+ cout << " 11. " << s1 << endl;
56
+ qsort (SArray, 4 , sizeof (MyString), CompareString);
57
+ for (int i = 0 ; i < 4 ; i++)
58
+ cout << SArray[i] << endl;
59
+ // s1的从下标0开始长度为4的子串
60
+ cout << s1 (0 , 4 ) << endl;
61
+ // s1的从下标5开始长度为10的子串
62
+ cout << s1 (5 , 10 ) << endl;
63
+ return 0 ;
64
+ }
0 commit comments