всё код готов и пашет гуд, если кому надо вот
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
| #include <iostream>
#include <utility>
#include <cstring>
using std::rel_ops::operator!=;
using std::rel_ops::operator>;
using std::rel_ops::operator<=;
using std::rel_ops::operator>=;
using namespace std;
class mstring
{
public:
typedef size_t size_type;
typedef char value_type;
static const size_type npos = -1;
mstring();
mstring( const mstring& );
mstring( const char* );
mstring( const char* str, size_type length );
~mstring();
friend bool operator== ( const mstring&, const mstring& );
friend bool operator< ( const mstring&, const mstring& );
char& operator[] ( size_type );
const char& operator[] ( size_type ) const;
mstring& operator= ( const mstring& );
mstring& operator= ( const char* );
friend std::ostream& operator<< ( std::ostream&, const mstring& );
friend std::istream& operator>> ( std::istream&, mstring& );
mstring& operator+= ( const mstring& );
mstring& operator+= ( const char* );
friend mstring operator+ ( const mstring&, const mstring& );
friend mstring operator+ ( const char*, const mstring& );
friend mstring operator+ ( const mstring&, const char* );
void insert( char*, size_t, char );
void insert( char*, const char*, const char* );
mstring& insert( size_type, const char*, size_type );
size_type find( const mstring&, size_type index = 0 ) const;
size_type find( const char*, size_type index = 0 ) const;
size_type find( const char*, size_type, size_type ) const;
size_type find( char, size_type index = 0 ) const;
void resize( size_type );
mstring substr( size_type index = 0, size_type length = npos ) const;
void swap( mstring& );
size_type size() const;
size_type capacity() const;
private:
size_type m_len;
size_type m_capacity;
char *m_arr;
};
mstring::mstring():
m_len( 0 ), m_capacity( 2*m_len+1 ), m_arr( new char[m_capacity]() )
{
*m_arr = '\0';
}
mstring::mstring( const mstring& s ):
m_len( s.m_len ), m_capacity( 2*m_len+1 ), m_arr( new char[m_capacity]() )
{
strcpy( m_arr, s.m_arr );
}
mstring::mstring( const char* str ):
m_len( strlen( str ) ), m_capacity( 2*m_len+1 ), m_arr( new char[m_capacity]() )
{
strcpy( m_arr, str );
}
mstring::mstring( const char* str, size_type length ):
m_len( std::min( length, strlen( str ) ) ),
m_capacity( 2*m_len+1 ), m_arr( new char[m_capacity]() )
{
strncpy( m_arr, str, std::min( length, strlen( str ) ) );
}
mstring::~mstring()
{
delete [] m_arr;
}
bool operator== ( const mstring& c1, const mstring& c2 )
{
return strcmp( c1.m_arr, c2.m_arr ) == 0;
}
bool operator< ( const mstring& c1, const mstring& c2 )
{
return strcmp( c1.m_arr, c2.m_arr ) < 0;
}
char& mstring::operator[] ( size_type index )
{
return m_arr[index];
}
const char& mstring::operator[]( size_type index ) const
{
return m_arr[index];
}
mstring& mstring::operator= ( const mstring& s )
{
if ( this != &s )
mstring( s ).swap( *this );
return *this;
}
mstring& mstring::operator= ( const char* s )
{
mstring( s ).swap( *this );
return *this;
}
std::ostream& operator<< ( std::ostream& out, const mstring& s )
{
out << s.m_arr;
return out;
}
std::istream& operator>> ( std::istream& in, mstring& s )
{
char tmp[4096];
in >> tmp;
s = in ? tmp : mstring();
return in;
}
mstring& mstring::operator+= ( const mstring& append )
{
resize( m_len + append.m_len + 1 );
strcat( m_arr, append.m_arr );
return *this;
}
mstring& mstring::operator+= ( const char* append )
{
resize( m_len + strlen( append ) + 1 );
strcat( m_arr, append );
return *this;
}
mstring operator+ ( const mstring& lhs, const mstring& rhs )
{
mstring ret( lhs );
ret += rhs;
return ret;
}
mstring operator+ ( const char* lhs, const mstring& rhs )
{
mstring ret( lhs );
ret += rhs;
return ret;
}
mstring operator+ ( const mstring& lhs, const char* rhs )
{
mstring ret( lhs );
ret += rhs;
return ret;
}
void mstring::insert( char* p, size_t n, char c )
{
size_type tmp = p - m_arr;
if ( m_len + n > m_capacity )
resize( m_len + n );
else
m_len += n;
p = m_arr + tmp;
for ( char * i = m_arr + m_len; i != p + n - 1; --i )
*i = *( i - n );
for ( char * i = p; i != p + n; ++i )
*i = c;
}
void mstring::insert( char* p, const char* first, const char* last )
{
size_type tmp = p - m_arr;
size_type n = last - first;
if ( m_len + n > m_capacity )
resize( m_len + n );
else
m_len += n;
p = m_arr + tmp;
for ( char * i = m_arr + m_len; i != p + n - 1; --i )
*i = *( i - n );
for ( char * i = p; i != p + n && first != last; ++i )
*i = *first++;
}
mstring& mstring::insert ( size_t pos1, const char* s, size_t n)
{
insert( m_arr + pos1, s, s + n );
return *this;
}
mstring::size_type mstring::find( const mstring& str, size_type index ) const
{
char * ptr = strstr( m_arr + index, str.m_arr );
if (ptr != nullptr)
return ptr - m_arr;
else
return npos;
}
mstring::size_type mstring::find( const char* str, size_type index ) const
{
char *ptr = strstr( m_arr + index, str );
if (ptr != nullptr)
return ptr - m_arr;
else
return npos;
}
mstring::size_type mstring::find( char ch, size_type index ) const
{
for ( size_type i = index; i != m_len; ++i )
{
if ( m_arr[i] == ch )
return i;
}
return npos;
}
mstring mstring::substr( size_type index, size_type length ) const
{
mstring res( this->m_arr + index, length );
return res;
}
void mstring::resize( size_type new_len )
{
m_capacity = 2 * new_len + 1;
char *new_arr = new char[m_capacity]();
strcpy( new_arr, m_arr );
delete [] m_arr;
m_len = new_len;
m_arr = new_arr;
}
void mstring::swap( mstring& c )
{
if ( this != &c )
{
std::swap( m_len, c.m_len );
std::swap( m_capacity, c.m_capacity );
std::swap( m_arr, c.m_arr );
}
}
mstring::size_type mstring::size() const
{
return m_len;
}
mstring::size_type mstring::capacity() const
{
return m_capacity;
}
int main(void)
{
mstring str1, str2;
string str3="Class";
char p,s;
size_t sz;
std::cout << "enter str1: ";
std::cin >> str1;
std::cout << "enter str2: ";
std::cin >> str2;
cout<<"Size str1: ";
cout<<str1.size()<<endl;
cout<<"Size str2: ";
cout<<str2.size()<<endl;
str1+=str2;
cout<<"str1+str2 "<<str1<<endl;
cout<<str1.find(str2)<<endl;
str1.swap(str2);
cout<<"swap: "<<str1<<endl;
sz=str1.size();
str1.resize(14);
cout <<"resize: "<<str1<< endl;
str1.insert (3," to be",18);
cout<<"insert: "<<str1<<endl;
cout<<"capacity: "<<str2.capacity()<<endl;
cout<<"substr: "<<str2.substr()<<endl;
system("pause");
return 0;
} |
|
всем спс за помощ и за сайты, особено тебе, David Sylva
Добавлено через 11 минут
немного переделал, так что ясно было что в класе стринг делаетса(вивод в мейн)
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
| #include <iostream>
#include <utility>
#include <cstring>
using std::rel_ops::operator!=;
using std::rel_ops::operator>;
using std::rel_ops::operator<=;
using std::rel_ops::operator>=;
using namespace std;
class mstring
{
public:
typedef size_t size_type;
typedef char value_type;
static const size_type npos = -1;
mstring();
mstring( const mstring& );
mstring( const char* );
mstring( const char* str, size_type length );
~mstring();
friend bool operator== ( const mstring&, const mstring& );
friend bool operator< ( const mstring&, const mstring& );
char& operator[] ( size_type );
const char& operator[] ( size_type ) const;
mstring& operator= ( const mstring& );
mstring& operator= ( const char* );
friend std::ostream& operator<< ( std::ostream&, const mstring& );
friend std::istream& operator>> ( std::istream&, mstring& );
mstring& operator+= ( const mstring& );
mstring& operator+= ( const char* );
friend mstring operator+ ( const mstring&, const mstring& );
friend mstring operator+ ( const char*, const mstring& );
friend mstring operator+ ( const mstring&, const char* );
void insert( char*, size_t, char );
void insert( char*, const char*, const char* );
mstring& insert( size_type, const char*, size_type );
size_type find( const mstring&, size_type index = 0 ) const;
size_type find( const char*, size_type index = 0 ) const;
size_type find( const char*, size_type, size_type ) const;
size_type find( char, size_type index = 0 ) const;
void resize( size_type );
mstring substr( size_type index = 0, size_type length = npos ) const;
void swap( mstring& );
size_type size() const;
size_type capacity() const;
private:
size_type m_len;
size_type m_capacity;
char *m_arr;
};
mstring::mstring():
m_len( 0 ), m_capacity( 2*m_len+1 ), m_arr( new char[m_capacity]() )
{
*m_arr = '\0';
}
mstring::mstring( const mstring& s ):
m_len( s.m_len ), m_capacity( 2*m_len+1 ), m_arr( new char[m_capacity]() )
{
strcpy( m_arr, s.m_arr );
}
mstring::mstring( const char* str ):
m_len( strlen( str ) ), m_capacity( 2*m_len+1 ), m_arr( new char[m_capacity]() )
{
strcpy( m_arr, str );
}
mstring::mstring( const char* str, size_type length ):
m_len( std::min( length, strlen( str ) ) ),
m_capacity( 2*m_len+1 ), m_arr( new char[m_capacity]() )
{
strncpy( m_arr, str, std::min( length, strlen( str ) ) );
}
mstring::~mstring()
{
delete [] m_arr;
}
bool operator== ( const mstring& c1, const mstring& c2 )
{
return strcmp( c1.m_arr, c2.m_arr ) == 0;
}
bool operator< ( const mstring& c1, const mstring& c2 )
{
return strcmp( c1.m_arr, c2.m_arr ) < 0;
}
char& mstring::operator[] ( size_type index )
{
return m_arr[index];
}
const char& mstring::operator[]( size_type index ) const
{
return m_arr[index];
}
mstring& mstring::operator= ( const mstring& s )
{
if ( this != &s )
mstring( s ).swap( *this );
return *this;
}
mstring& mstring::operator= ( const char* s )
{
mstring( s ).swap( *this );
return *this;
}
std::ostream& operator<< ( std::ostream& out, const mstring& s )
{
out << s.m_arr;
return out;
}
std::istream& operator>> ( std::istream& in, mstring& s )
{
char tmp[4096];
in >> tmp;
s = in ? tmp : mstring();
return in;
}
mstring& mstring::operator+= ( const mstring& append )
{
resize( m_len + append.m_len + 1 );
strcat( m_arr, append.m_arr );
return *this;
}
mstring& mstring::operator+= ( const char* append )
{
resize( m_len + strlen( append ) + 1 );
strcat( m_arr, append );
return *this;
}
mstring operator+ ( const mstring& lhs, const mstring& rhs )
{
mstring ret( lhs );
ret += rhs;
return ret;
}
mstring operator+ ( const char* lhs, const mstring& rhs )
{
mstring ret( lhs );
ret += rhs;
return ret;
}
mstring operator+ ( const mstring& lhs, const char* rhs )
{
mstring ret( lhs );
ret += rhs;
return ret;
}
void mstring::insert( char* p, size_t n, char c )
{
size_type tmp = p - m_arr;
if ( m_len + n > m_capacity )
resize( m_len + n );
else
m_len += n;
p = m_arr + tmp;
for ( char * i = m_arr + m_len; i != p + n - 1; --i )
*i = *( i - n );
for ( char * i = p; i != p + n; ++i )
*i = c;
}
void mstring::insert( char* p, const char* first, const char* last )
{
size_type tmp = p - m_arr;
size_type n = last - first;
if ( m_len + n > m_capacity )
resize( m_len + n );
else
m_len += n;
p = m_arr + tmp;
for ( char * i = m_arr + m_len; i != p + n - 1; --i )
*i = *( i - n );
for ( char * i = p; i != p + n && first != last; ++i )
*i = *first++;
}
mstring& mstring::insert ( size_t pos1, const char* s, size_t n)
{
insert( m_arr + pos1, s, s + n );
return *this;
}
mstring::size_type mstring::find( const mstring& str, size_type index ) const
{
char * ptr = strstr( m_arr + index, str.m_arr );
if (ptr != nullptr)
return ptr - m_arr;
else
return npos;
}
mstring::size_type mstring::find( const char* str, size_type index ) const
{
char *ptr = strstr( m_arr + index, str );
if (ptr != nullptr)
return ptr - m_arr;
else
return npos;
}
mstring::size_type mstring::find( char ch, size_type index ) const
{
for ( size_type i = index; i != m_len; ++i )
{
if ( m_arr[i] == ch )
return i;
}
return npos;
}
mstring mstring::substr( size_type index, size_type length ) const
{
mstring res( this->m_arr + index, length );
return res;
}
void mstring::resize( size_type new_len )
{
m_capacity = 2 * new_len + 1;
char *new_arr = new char[m_capacity]();
strcpy( new_arr, m_arr );
delete [] m_arr;
m_len = new_len;
m_arr = new_arr;
}
void mstring::swap( mstring& c )
{
if ( this != &c )
{
std::swap( m_len, c.m_len );
std::swap( m_capacity, c.m_capacity );
std::swap( m_arr, c.m_arr );
}
}
mstring::size_type mstring::size() const
{
return m_len;
}
mstring::size_type mstring::capacity() const
{
return m_capacity;
}
int main(void)
{
mstring str1, str2;
string str3="Class";
char p,s;
size_t sz;
std::cout << "enter str1: ";
std::cin >> str1;
std::cout << "enter str2: ";
std::cin >> str2;
cout<<"Size str1: ";
cout<<str1.size()<<endl;
cout<<"Size str2: ";
cout<<str2.size()<<endl;
str1+=str2;
cout<<"str1+str2 "<<str1<<endl;
cout<<"find(str1 in str2): "<<str1.find(str2)<<endl;
str2.swap(str1);
cout<<"swap: "<<str2<<endl;
sz=str1.size();
str1.resize(14);
cout <<"resize (14): "<<str1<< endl;
str1.insert (3," to be",18);
cout<<"insert (to be): "<<str1<<endl;
cout<<"capacity: "<<str2.capacity()<<endl;
cout<<"substr: "<<str2.substr()<<endl;
system("pause");
return 0;
} |
|
1
|