1 // File Name: Istring.h
2
3 #pragma once
4
5 #include<iostream>
6 using namespace std;
7
8 class Istring{
9 public:
10
11 Istring( const char * = "\0" );
12 Istring( const Istring & );
13 ~Istring();
14 Istring & operator = ( const Istring & );
15
16 Istring & operator + ( const Istring & );
17 Istring & operator += ( const Istring & );
18
19 bool operator == ( const Istring & rhs ) const;
20 bool operator != ( const Istring & rhs ) const { return !( *this == rhs ); }
21 bool operator < ( const Istring & rhs ) const;
22 bool operator > ( const Istring & rhs ) const { return rhs < *this; }
23 bool operator >= ( const Istring & rhs ) const { return !( *this < rhs ); }
24 bool operator <= ( const Istring & rhs ) const { return *this < rhs || *this == rhs; }
25
26 char & operator [] ( unsigned );
27 char operator [] ( unsigned ) const;
28
29 size_t length( );
30 bool empty( );
31
32 char * c_str( );
33
34 void swap( Istring & other )
35 {
36 using std::swap;
37 swap( pstr, other.pstr );
38 }
39
40 friend ostream & operator << ( ostream &, const Istring & );
41 friend istream & operator >> ( istream &, Istring & );
42
43 private:
44
45 char * pstr;
46
47 };
1 //File Name: Istring.cc
2
3 #include"istring.h"
4 #include<cstring>
5 #include<cassert>
6 #include<iostream>
7 using namespace std;
8
9 Istring:: Istring( const char *s )
10 {
11 if( s == NULL )
12 {
13 pstr = new char[1];
14 *pstr = '\0';
15 }
16 else
17 {
18 pstr = new char[ sizeof(s) ];
19 strcpy( pstr, s );
20 }
21 }
22
23 Istring:: Istring( const Istring & s )
24 {
25 assert( s.pstr != NULL );
26 pstr = new char[ strlen( s.pstr )+1 ];
27 strcpy( pstr, s.pstr );
28 }
29
30 Istring:: ~Istring()
31 {
32 delete [] pstr;
33 }
34
35
36 Istring& Istring:: operator = ( const Istring & s )
37 {
38 if( this == &s ) return *this;
39 char * pOrig = pstr;
40 pstr = new char [ strlen( s.pstr )+1 ];
41 strcpy( pstr, s.pstr );
42 delete [] pOrig;
43 return *this;
44 }
45
46
47
48 Istring & Istring:: operator + ( const Istring & s )
49 {
50 char * pOrig = pstr;
51 pstr = new char[ strlen(pstr) + strlen(s.pstr) + 1 ];
52 strcpy( pstr, pOrig );
53 strcpy( pstr + strlen( pOrig ), s.pstr );
54 delete [] pOrig;
55 return *this;
56 }
57
58 Istring & Istring:: operator += ( const Istring & s )
59 {
60 return *this = *this + s;
61 }
62
63 bool Istring:: operator == ( const Istring & s ) const
64 {
65 return strcmp( pstr, s.pstr ) == 0 ;
66 }
67
68
69 bool Istring:: operator < ( const Istring & s ) const
70 {
71 return strcmp( pstr, s.pstr ) < 0 ;
72 }
73
74
75 char & Istring:: operator [] ( unsigned n )
76 {
77 if ( n >= strlen( pstr ) )
78 {
79 throw " out of rang";
82 }
83 return pstr[n];
84 }
85
86 char Istring:: operator [] ( unsigned n ) const
87 {
88
89 if ( n >= strlen( pstr ) )
90 {
91
92 throw " out of rang" ;
94 }
95 return pstr[n];
96 }
97
98 size_t Istring:: length( )
99 {
100 return strlen( pstr );
101 }
102
103 bool Istring:: empty( )
104 {
105 return length() == 0;
106 }
107
108 char * Istring:: c_str( )
109 {
110 return pstr ;
111 }
112
113
114 ostream & operator << ( ostream & os, const Istring & s )
115 {
116 os<<s.pstr;
117 return os;
118 }
119
120 istream & operator >> ( istream & is, Istring & s )
121 {
122 char temp[100];
123 is>>temp;
124 s = temp;
125 return is;
126 }
127
128
129
130 namespace std {
131
132 template<>
133 void swap<Istring> ( Istring & a, Istring & b )
134 {
135 a.swap(b);
136 }
137
138 }