文章目录
- 1.1大框架
- 1.2基本函数:
- 2.成员函数
-
- 2.0构造函数
- 2.05析构函数
- 2.09拷贝构造函数
-
- 2.1增加字符(push_back,append,s+=)
-
- 2. 删除字符(erase)
- 2. 查找字符(find)
- 2. 插入字符(insert)
- 2. 截取字符(substr)
- 2. 调整字符串的长度(resize)
- 2. 比大小(运算符重载)
-
- 2.流插入提取(cout,cin)
-
- 2.赋值运算符重载
- 2.迭代器
1.1大框架
namespace simulation {
class string {
public:
private:
size_t _size;
size_t _capacity;
char* _str;
const static size_t npos;
};
const size_t string::npos=-1;
}
1.2基本函数:
const char* c_str()const {
return _str;
}
size_t size() {
return _size;
}
char& operator[](size_t pos) {
assert(pos < _size);
return _str[pos];
}
const char& operator[](size_t pos) const{
assert(pos < _size);
return _str[pos];
}
2.成员函数
2.0构造函数
string()
:_size(0)
, _capacity(0)
, _str(new char[1])
{
_str[0] = '\0';
}
string(const char* str)
:_size(strlen(str))
, _capacity(_size)
, _str(new char[_capacity + 1])
{
memcpy(_str, str, _size + 1);
}
string(const char* str = "") {
_size = strlen(str);
_capacity = _size;
_str = new char[_capacity + 1];
memcpy(_str, str, _size + 1);
}
2.05析构函数
~string() {
delete[] _str;
_size = _capacity = 0;
_str = nullptr;
}
2.09拷贝构造函数
string(const string& s) {
_str = new char[s._capacity + 1];
memcpy(_str, s._str, s._size + 1);
_size = s._size;
_capacity = s._capacity;
}
补充:预留存储空间(reserve)
void reserve(size_t n) {
if (n > _capacity) {
char* tmp = new char[n + 1];
memcpy(tmp, _str, _size + 1);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
2.1增加字符(push_back,append,s+=)
push_back
void push_back(char ch) {
if (_size == _capacity) {
reserve(_capacity == 0 ? 4 : 2 * _capacity);
}
_str[_size] = ch;
++_size;
_str[_size] = '\0';
}
append
void append(const char* str) {
size_t len = strlen(str);
if (len + _size > _capacity) {
reserve(len + _size);
}
memcpy(_str + _size, str, len + 1);
_size += len;
}
s+=
string& operator+=(const char* str) {
append(str);
return *this;
}
string& operator+=(char ch) {
push_back(ch);
return *this;
}
2. 删除字符(erase)
void erase(size_t pos, size_t len = npos) {
assert(pos <= _size);
if (len == npos || len + pos >= _size) {
_size = pos;
_str[_size] = '\0';
}
else {
size_t end = pos + len;
while (end<=_size) {
_str[pos++] = _str[end++];
}
_size -= len;
}
}
2. 查找字符(find)
size_t find(char ch, size_t pos=0) {
assert(pos < _size);
for (size_t i = 0; i < _size; i++) {
if (_str[i] == ch) {
return i;
}
}
return npos;
}
size_t find(const char* str, size_t pos = 0) {
assert(pos < _size);
const char* ptr = strstr(_str, str);
if (ptr) {
return ptr - _str;
}
return npos;
}
2. 插入字符(insert)
void insert(size_t pos, size_t n, char ch) {
assert(pos <= _size);
if (n + _size > _capacity) {
reserve(n + _size);
}
size_t end = _size;
while (end >= pos && end != npos) {
_str[end + n] = _str[end];
end--;
}
for (size_t i = 0; i < n; i++) {
_str[pos + i] = ch;
}
_size += n;
}
void insert(size_t pos, const char* str) {
assert(pos <= _size);
size_t len = strlen(str);
if (len + _size > _capacity) {
reserve(len + _size);
}
size_t end = _size;
while (end >= pos&&end!=npos) {
_str[len + end] = _str[end];
end--;
}
for (size_t i = 0; i < len; i++) {
_str[pos + i] = str[i];
}
_size += len;
}
2. 截取字符(substr)
string substr(size_t pos=0, size_t len = npos) {
assert(pos < _size);
size_t n = len;
if (len == npos || len + pos > _size) {
n = _size - pos;
}
string tmp;
tmp.reserve(n);
for (size_t i = pos; i < pos + n; i++)
{
tmp += _str[i];
}
return tmp;
}
2. 调整字符串的长度(resize)
void resize(size_t n,char ch='\0') {
if (n < _size) {
_str[n] = '\0';
_size = n;
}
else {
reserve(n);
for (size_t i = _size; i < n; i++) {
_str[i] = ch;
}
_size = n;
_str[_size] = '\0';
}
}
2. 比大小(运算符重载)
<
bool operator<(const string& s)const {
int i1 = 0;
int i2 = 0;
while (i1 < _size && i2 < s._size) {
if (_str[i1] < s._str[i2]) {
return true;
}
else if (_str[i1] > s._str[i2]) {
return false;
}
else {
i1++;
i2++;
}
}
return _size < s._size;
}
bool operator<(const string& s)const {
int ret = memcmp(_str, s._str, _size < s._size ? _size : s._size);
return ret == 0 ? _size < s._size : ret < 0;
}
复用
bool operator==(const string& s)const {
return _size == s._size &&
memcmp(_str, s._str, _size) == 0;
}
bool operator<=(const string& s)const {
return *this < s || *this == s;
}
bool operator>(const string& s)const {
return !(*this <= s);
}
bool operator>=(const string& s)const {
return !(*this < s);
}
2.流插入提取(cout,cin)
cout
ostream& operator<<(ostream& out, const simulation::string& s)
{
for (auto ch : s)
{
out << ch;
}
return out;
}
cin
void clear() {
_str[0] = '\0';
_size = 0;
}
``istream& operator>>(istream& in, simulation:: string& s) {
s.clear();
char ch = in.get();
while (ch == ' ' || ch== '\n') {
ch = in.get();
}
char buff[128];
int i = 0;
while (ch != ' ' && ch != '\n') {
buff[i++] = ch;
if (i == 127) {
buff[i] = '\0';
s += buff;
i = 0;
}
ch = in.get();
}
if (i != 0) {
buff[i] = '\0';
s += buff;
}
return in;
}
2.赋值运算符重载
void swap( string& s) {
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
std::swap(_str, s._str);
}
string& operator=(string tmp) {
swap(tmp);
return *this;
}
2.迭代器
typedef char* iterator;
typedef const char* const_iterator;
iterator begin() {
return _str;
}
const_iterator begin()const {
return _str;
}
iterator end() {
return _str + _size;
}
const_iterator end()const {
return _str + _size;
}