<String_>

ContractedBlock.gif ExpandedBlockStart.gif string_
 
   
1 /*
2 boring
3 eth0
4 */
5 #ifndef SSTRING_H
6 #define SSTRING_H
7
8
9 #include < iostream >
10 #include < cstring >
11 #include < algorithm >
12 using std::istream;
13 using std::ostream;
14
15 class sstring
16 {
17 private :
18 friend istream & operator >> (istream & ,sstring & );
19 friend ostream & operator << (ostream & , const sstring & );
20 friend bool operator == ( const sstring & a, const sstring & b);
21 friend bool operator != ( const sstring & a, const sstring & b);
22 friend bool operator < ( const sstring & a, const sstring & b);
23 friend bool operator > ( const sstring & a, const sstring & b);
24 friend bool operator <= ( const sstring & a, const sstring & b);
25 friend bool operator >= ( const sstring & a, const sstring & b);
26 public :
27 sstring( const char * str = "" );
28 sstring( const sstring & );
29 ~ sstring();
30 int length() const ;
31 sstring & operator = ( const sstring & );
32 sstring & operator += ( const sstring & );
33 char & operator []( int );
34 const char & operator []( int ) const ;
35 sstring operator ()( int , int ) const ;
36 private :
37 char * s;
38 };
39 /* *************************************************** */
40 sstring::sstring( const char * str)
41 {
42 if (str == 0 )
43 {
44 s = new char [ 1 ];
45 s[ 0 ] = 0 ;
46 }
47 else
48 {
49 s = new char [strlen(str) + 1 ];
50 strcpy(s,str);
51 }
52 }
53 sstring::sstring( const sstring & other)
54 {
55 s = new char [other.length() + 1 ];
56 strcpy(s,other.s);
57 }
58 sstring:: ~ sstring()
59 {
60 delete []s;
61 }
62 int sstring::length() const
63 {
64 return strlen(s);
65 }
66 sstring & sstring:: operator = ( const sstring & other)
67 {
68 if ( this !=& other)
69 {
70 delete []s;
71 s = new char [other.length() + 1 ];
72 strcpy(s,other.s);
73 }
74 return * this ;
75 }
76 sstring & sstring:: operator += ( const sstring & other)
77 {
78 char * tem = new char [length() + 1 ];
79 strcpy(tem,s);
80 delete []s;
81 s = new char [length() + other.length() + 2 ];
82 strcpy(s,tem);
83 strcat(s,other.s);
84 delete []tem;
85 return * this ;
86 }
87 char & sstring:: operator []( int i)
88 {
89 if (i >= length() || i < 0 )
90 exit( 0 );
91 return s[i];
92 }
93 const char & sstring:: operator []( int i) const
94 {
95 if (i >= length() || i < 0 )
96 exit( 0 );
97 return s[i];
98 }
99 sstring sstring:: operator ()( int index, int sublen) const
100 {
101 if (index < 0 || index >= length() || sublen <= 0 ) return "" ;
102 int len = sublen + index > length() ? length() - index:sublen;
103 char * tem = new char [len + 1 ];
104 strncpy(tem,s + index,len);
105 tem[len] = 0 ;
106 sstring t(tem);
107 delete tem;
108 return t;
109 }
110 bool operator == ( const sstring & a, const sstring & other)
111 {
112 return strcmp(a.s,other.s) == 0 ;
113 }
114 bool operator < ( const sstring & a, const sstring & b)
115 {
116 return strcmp(a.s,b.s) < 0 ;
117 }
118 bool operator > ( const sstring & a, const sstring & b)
119 {
120 return b < a;
121 }
122 bool operator <= ( const sstring & a, const sstring & b)
123 {
124 return ! (b < a);
125 }
126 bool operator >= ( const sstring & a, const sstring & b)
127 {
128 return ! (a < b);
129 }
130 bool operator != ( const sstring & a, const sstring & b)
131 {
132 return ! (a == b);
133 }
134 istream & operator >> (istream & in ,sstring & other)
135 {
136 char tem[ 100 ] = { 0 };
137 in >> tem;
138 other = tem;
139 return in ;
140 }
141 ostream & operator << (ostream & on, const sstring & other)
142 {
143
144 return on << other.s;
145 }
146
147
148 /* ************************************************ */
149 #endif // SSTRING_H

转载于:https://www.cnblogs.com/eth0/archive/2011/05/04/2036834.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值