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
| #include <iostream> #include <assert.h> class String { public: friend std::istream& operator>>(std::istream& _cin, String& s); friend std::ostream& operator<<(std::ostream& _cout, const String& s); typedef char* Iterator; typedef const char* const_Iterator; String(const String& s); String(const char* str = ""); String(String&& s); ~String(); String& operator=(String s); Iterator begin(); const_Iterator begin() const; Iterator end(); const_Iterator end() const; size_t size() const; void resize(size_t n, char c = '\0'); size_t capacity() const; void reserve(size_t n); void clear(); bool empty() const; char& operator[](size_t pos); const char& operator[](size_t pos) const; String& operator+=(const String& s); String& operator+=(const char* str); String& operator+=(char c); String& insert(size_t pos, const char* str); String& insert(size_t pos, size_t n, const char c); String& erase(size_t pos, size_t n); void swap(String& s); char* c_str() const; size_t find(const char* str, size_t pos = 0) const; size_t find_first_of(char c, size_t pos = 0) const; static size_t npos; private: size_t _size; size_t _capacity; char* _str; };
#include "string.h"
std::istream& operator>>(std::istream& _cin, String& s) { for (char c = _cin.get(); !isspace(c); _cin.get(c)) { s += c; } return _cin; }
std::ostream& operator<<(std::ostream& _cout, const String& s) { _cout << s._str; return _cout; }
String::String(const String& s) : _size(0) , _capacity(0) , _str(nullptr) { String tmp(s._str); swap(tmp); }
String::String(const char* str) : _size(strlen(str)) , _capacity(_size) , _str(new char[_capacity + 1]) { strcpy(_str, str); }
String::String(String&& s) : String() { swap(s); }
String::~String() { if (_str) { delete[] _str; _str = nullptr; _size = 0; _capacity = 0; } }
String& String::operator=(String s) { swap(s); return *this; }
String::Iterator String::begin() { return _str; }
String::const_Iterator String::begin() const { return _str; }
String::Iterator String::end() { return _str + _size; }
String::const_Iterator String::end() const { return _str + _size; }
size_t String::size() const { return _size; }
void String::resize(size_t n, char c) { if (n < _size) { _size = n; _str[_size] = '\0'; } else { if (n > _capacity) { reserve(n); } for (size_t i = _size; i < n; i++) { _str[i] = c; } _size = n; _str[_size] = '\0'; } }
size_t String::capacity() const { return _capacity; }
void String::reserve(size_t n) { if (n > _capacity) { char* tmp = new char[n + 1]; if (_str) { strcpy(tmp, _str); delete[] _str; } _str = tmp; _capacity = n; } }
void String::clear() { _size = 0; _str[_size] = '\0'; }
bool String::empty() const { return _size == 0; }
char& String::operator[](size_t pos) { assert(pos <= _size); return _str[pos]; }
const char& String::operator[](size_t pos) const { assert(pos <= _size); return _str[pos]; }
String& String::operator+=(const String& s) { insert(_size, s._str); return *this; }
String& String::operator+=(const char* str) { insert(_size, str); return *this; }
String& String::operator+=(char c) { insert(_size, 1, c); return *this; }
String& String::insert(size_t pos, const char* str) { assert(pos <= _size); int len = strlen(str); if (_size + len > _capacity) { reserve(_size + len); } size_t end = _size + len; while (end > pos + len - 1) { _str[end] = _str[end - len]; --end; } while (*str) { _str[pos++] = *str++; } _size += len; return *this; }
String& String::insert(size_t pos, size_t n, const char c) { assert(pos <= _size); if (_size + n > _capacity) { reserve(_size + n); } size_t end = _size + n; while (end > pos + n - 1) { _str[end] = _str[end - n]; --end; } for (size_t i = 0; i < n; ++i) { _str[pos++] = c; } _size += n; return *this; }
String& String::erase(size_t pos, size_t n) { assert(pos < _size); if (n >= _size - pos) { _size = pos; _str[_size] = '\0'; } else { size_t begin = pos + n; while (begin <= _size) { _str[pos++] = _str[begin]; ++begin; } _size -= n; } return *this; }
void String::swap(String& s) { std::swap(_str, s._str); std::swap(_size, s._size); std::swap(_capacity, s._capacity); }
char* String::c_str() const { return _str; }
size_t String::find(const char* str, size_t pos) const { char* ret = strstr(_str + pos, str); if (ret != NULL) { return ret - _str; } else { return npos; } }
size_t String::find_first_of(char c, size_t pos) const { for (size_t i = pos; i < _size; i++) { if (_str[i] == c) { return i; } } return npos; }
size_t String::npos = -1;
|