C++中的string类是要比STL出现的早的,但是string类的接口和常规容器的接口基本相同,
其中包括 构造,拷贝构造,赋值,这些涉及到的一些深浅拷贝,迭代器、增删改查、扩容、还有比较、[ ] 、<< 、 >> 一些操作符的重载,
其实对于string类没有特别要注意的点,那我就浅浅的模拟实现一下吧,有什么问题欢迎大家指出来。
#include <iostream>
#include <stdlib.h>
using namespace std;
namespace ltx
{
class string
{
friend ostream& operator<<(ostream& _cout, const ltx::string& s);
friend istream& operator>>(istream& _cin, ltx::string& s);
public:
typedef char* iterator;
string(const char* str = "") {
_size = strlen(str);
_capacity = _size;
_str = new char[_size + 1];
//strncpy(_str, str, _size - 1);
strcpy(_str, str);
_str[_size] = '\0';
}
string(const string& s) {
string temp(s._str);
swap(temp);
}
string& operator=(const string& s) {
string temp(s._str);
swap(temp);
return *this;
}
~string() {
delete[] _str;
_str = nullptr;
_size = 0;
_capacity = 0;
}
//
// iterator
iterator begin() {
return _str;
}
iterator end() {
return _str + _size - 1;
}
/
void Expansion() {
while (_size >= _capacity) {
_capacity *= 2;
char* temp = new char[_capacity];
strcpy(temp, _str);
delete[] _str;
_str = temp;
}
}
// modify
void push_back(char c) {
Expansion();
_str[_size++] = c;
_str[_size] = '\0';
}
string& operator+=(char c) {
push_back(c);
return *this;
}
void append(const char* str) {
int size = _size;
_size += strlen(str);
Expansion();
strcpy(_str + size, str);
_str[_size] = '\0';
}
string& operator+=(const char* str) {
append(str);
return *this;
}
void clear() {
_size = 0;
_str[0] = '\0';
}
void swap(string& temp) {
::swap(_size, temp._size);
::swap(_capacity, temp._capacity);
::swap(_str, temp._str);
}
const char* c_str()const {
return _str;
}
/
// capacity
size_t size()const {
return _size;
}
size_t capacity()const {
return _capacity;
}
bool empty()const {
return _size == 0;
}
void resize(size_t n, char c = '\0') {
if (n > _size) {
int size = _size;
_size += n;
Expansion();
while (size < _size) {
_str[size++] = c;
}
_str[_size] = '\0';
}
else if (n >= 0){
_size = n;
_str[_size] = '\0';
}
}
void reserve(size_t n) {
if (_capacity < n) {
_capacity = n;
char* temp = new char[_capacity];
strcpy(temp, _str);
delete[] _str;
_str = temp;
_str[_size] = '\0';
}
}
/
// access
char& operator[](size_t index) {
return _str[index];
}
const char& operator[](size_t index)const {
return _str[index];
}
/
//relational operators
bool operator<(const string& s)const {
int size = _size < s._size ? _size : s._size;
for (int i = 0; i < size; i++) {
if (_str[i] <= s._str[i]) continue;
return false;
}
return _size < s._size;
}
bool operator<=(const string& s)const {
return !(*this > s);
}
bool operator>(const string& s)const {
int size = _size < s._size ? _size : s._size;
for (int i = 0; i < size; i++) {
if (_str[i] >= s._str[i]) continue;
return false;
}
return _size > s._size;
}
bool operator>=(const string& s)const {
return !(*this < s);
}
bool operator==(const string& s)const {
if (_size != s._size) return false;
for (int i = 0; i < _size; i++) {
if (_str[i] != s._str[i]) return false;
}
return true;
}
bool operator!=(const string& s)const {
return !(*this == s);
}
// 返回c在string中第一次出现的位置
size_t find(char c, size_t pos = 0) const {
for (; pos < _size; pos++) {
if (_str[pos] == c) return pos;
}
return -1;
}
// 返回子串s在string中第一次出现的位置
size_t find(const char* s, size_t pos = 0) const {
int i = pos;
int j = 0;
int jmax = strlen(s);
while (pos < _size) {
i = pos;
j = 0;
while (i < _size && j < jmax && _str[i++] == s[j++]) {
if (_str[i] != s[j]) break;
}
if (j == jmax) return pos;
pos++;
}
return -1;
}
// 在pos位置上插入字符c/字符串str,并返回该字符的位置
size_t insert(size_t pos, char c) {
if (pos <= _size) {
_size++;
int size = _size;
Expansion();
while (size > pos) {
:: swap(_str[size], _str[size - 1]);
size--;
}
_str[pos] = c;
}
return pos;
}
size_t insert(size_t pos, const char* str) {
if (pos <= _size) {
int Ssize = strlen(str);
_size += Ssize;
int cur = _size;
Expansion();
while (cur > pos) {
::swap(_str[cur], _str[cur - 1]);
cur--;
}
//_str[pos] = c;
strncpy(_str + pos, str, Ssize);
}
return pos;
}
// 删除pos位置上的元素,并返回该元素的下一个位置
size_t erase(size_t pos, size_t len) {
if (pos < _size) {
if (pos + len < _size) {
while (pos + len <= _size) {
_str[pos] = _str[pos + len];
pos++;
}
_size -= len;
}
else {
_str[pos] = '\0';
_size = pos;
}
}
return pos;
}
private:
char* _str;
size_t _capacity;
size_t _size;
};
ostream& operator<<(ostream& _cout, const ltx::string& s) {
_cout << s.c_str();
return _cout;
}
istream& operator>>(istream& _cin, ltx::string& s) {
s.clear();
char get = _cin.get();
char temp[33] = { 0 };
int i = 0;
while (get != '\n') {
if (i == 32) {
s += temp;
i = 0;
}
temp[i++] = get;
get = _cin.get();
}
if (i != 0) {
temp[i] = '\0';
s += temp;
}
return _cin;
}
};