目录
构造函数
c_str
operator[]
const版本
迭代器
push_back
append
reserve
operator+=
插入函数
删除函数
find函数:
resize
流插入(非友元版本)
友元版本:
流提取:
高效率流提取:
clear
深拷贝:
赋值重载:
析构函数:
swap
赋值重载(现代写法)
string(const char*str=" ")
{
_size = strlen(str);
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str, str);
}
const char*c_str()
{
return _str;
}
char& operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];
}
const char& operator[](size_t pos) const
{
assert(pos < _size);
return _str[pos];
}
typedef char* iterator;
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
实现了迭代器也就支持了范围for。
范围for本质是迭代器的替换,他的底层实现就是迭代器,所以实现了迭代器就实现了范围for。
void push_back(char ch)
{
if (_size == _capacity)
{
size_t NewCapacity = _capacity == 0 ? 4 : _capacity * 2;
reserve(NewCapacity);
}
_str[_size] = ch;
++_size;
_str[_size] = '\0';
}
void append(const char*str)
{
size_t len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
strcpy(_str, str);
_size += len;
}
void reserve(size_t n)
{
char*tmp = new char[n+1];
strcpy(tmp, _str);
delete _str;
_str = tmp;
_capacity = n;
}
string& operator+=(char ch)
{
push_back(ch);
return *this;
}
string& operator+=(const char* str)
{
append(str);
return *this;
}
string&insert(size_t pos, char ch)
{
assert(pos <= _size);
if (_size == _capacity)
{
size_t NewCapacity = _capacity == 0 ? 4 : _capacity * 2;
reserve(NewCapacity);
}
int end = _size;
while ((int)pos <= end)
{
_str[end + 1] = _str[end];
--end;
}
_str[pos] = ch;
++_size;
return *this;
}
string&insert(size_t pos, const char*str)
{
assert(pos <= _size);
if (_size == _capacity)
{
size_t NewCapacity = _capacity == 0 ? 4 : _capacity * 2;
reserve(NewCapacity);
}
int len = strlen(str);
int end = _size;
while ((int)pos <= end)
{
_str[end + len] = _str[end];
--end;
}
strncpy(_str + pos, str,len);
_size += len;
return *this;
}
我们需要先实现npos:
npos是静态成员变量,被所有类对象共享,所以只能在类外面初始化,但是const修饰的整型静态成员变量是可以在类里面初始化的。
string&erase(size_t pos = 0, size_t len = npos)
{
assert(pos < _size);
if (len == npos || len >= _size - pos)
{
_str[pos] = '\0';
_size -= len;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
return *this;
}
size_t find(char ch, size_t pos = 0)
{
assert(pos < _size);
while (pos < _size)
{
if (_str[pos] == ch)
{
return pos;
}
pos++;
}
return npos;
}
size_t find(const char*str, size_t pos = 0)
{
assert(pos < _size);
const char*ptr=strstr(_str+pos, str);
if (ptr == nullptr)
{
return npos;
}
else
{
return ptr-_str;
}
}
void resize(size_t n, char val = '\0')
{
if (n > _size)
{
reserve(n);
for (int i = _size; i
流插入(非友元版本)
ostream&operator<<(ostream&out, const string&s)
{
int i = 0;
for (i = 0; i < s.size(); i++)
{
out << s[i];
}
return out;
}
友元版本:
ostream&operator<<(ostream&out, const string&s)
{
out << s._str << endl;
return out;
}
我们需要在类内写友元声明:
friend ostream&operator<<(ostream&out, const string&s);
流提取:
istream&operator>>(istream&in,string&s)
{
char ch =in.get();
while (ch != ' ' && ch != '\n')
{
s += ch;
ch = in.get();
}
return in;
}
高效率流提取:
istream&operator>>(istream&in,string&s)
{
s.clear();
char buff[128] = {'\0'};
char ch;
int i = 0;
ch = in.get();
while (ch != ' '&&ch != '\n')
{
if (i == 127)
{
s += buff;
i = 0;
}
buff[i++] = ch;
ch = in.get();
}
if (i >= 0)
{
buff[i] = '\0';
s += buff;
return in;
}
}
clear
void clear()
{
_size = 0;
_str[0] = '\0';
}
深拷贝:
string(const string&s)
{
_str = new char[s._capacity + 1];
_capacity = s._capacity;
_size = s._size;
strcpy(_str, s._str);
}
赋值重载:
string&operator=(const string&s)
{
if (_str != s._str)
{
char*tmp = new char[s._capacity + 1];
strcpy(tmp, s._str);
delete _str;
_str = tmp;
_capacity = s._capacity;
_size = s._size;
}
return *this;
}
析构函数:
~string()
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
swap
void swap(string&s)
{
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
赋值重载(现代写法)
string&operator=(string s)
{
swap(s);
return *this;
}
你可能感兴趣的:(开发语言)