目录
string及其模拟实现::
1.构造函数
2.拷贝构造函数
3.赋值运算符重载
4.析构函数
5.string中的小接口
6.operator[]的运算符重载
7.迭代器
8.reserve和resize
9.push_back、append和+=的重载
10.insert、erase和find
11.<<和>>的运算符重载
12.模拟实现string的整体代码
string及其模拟实现::
1.构造函数
//注意: '\0' "\0" ""的区别
string(const char* str = "")
{
//对空指针不能strlen
_size = strlen(str);
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str, str);
}
2.拷贝构造函数
传统写法:
//拷贝构造传统写法
//s2(s1)
string(const string& s)
{
_str = new char[s._capacity + 1];
_capacity = s._capacity;
_size = s._size;
strcpy(_str, s._str);
}
现代写法:
void swap(string& s)
{
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
string(const string& s)
:_str(nullptr)
,_size(0)
,_capacity(0)
{
string tmp(s._str);//构造函数
//交换s2和tmp
//this->swap(tmp);
swap(tmp);
}
3.赋值运算符重载
传统写法:
//赋值重载传统写法:
//s1=s3
string& operator=(const string& s)
{
if (this != &s)
{
char* tmp = new char[s._capacity + 1];
strcpy(tmp, s._str);
//释放s1的旧空间
delete[] _str;
_str = tmp;
_size = s._size;
_capacity = s._capacity;
}
return *this;
}
现代写法:
//赋值重载现代写法:
//s1=s3
//string& operator=(const string& s)
//{
// //string tmp(s._str);
// string tmp(s);
// swap(tmp);
// return *this;
//}
//s1=s3
string& operator=(string s)
{
//不用传引用传参 就用传值传参 s是s3的拷贝构造
swap(s);
return *this;
}
4.析构函数
~string()
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
5.string中的小接口
const char* c_str() const
{
return _str;
}
size_t size() const
{
return _size;
}
size_t capacity() const
{
return _capacity;
}
void clear()
{
_str[0] = '\0';
_size = 0;
}
6.operator[]的运算符重载
//普通对象可读可写
char& operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];
}
//const对象:只读
const char& operator[](size_t pos) const
{
assert(pos < _size);
return _str[pos];
}
7.迭代器
typedef char* iterator;
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
8.reserve和resize
void reserve(size_t n)
{
//保证只扩容不缩容
if (n > _capacity)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
void resize(size_t n, char ch = '\0')
{
if (n > _size)
{
reserve(n);
for (size_t i = _size; i < n; ++i)
{
_str[i] = ch;
}
_size = n;
_str[_size] = '\0';
}
else
{
_str[n] = '\0';
_size = n;
}
}
9.push_back、append和+=的重载
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 + _size, str);
_size += len;
}
string& operator+=(char ch)
{
push_back(ch);
return *this;
}
string& operator+=(const char* str)
{
append(str);
return *this;
}
10.insert、erase和find
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;
//操作符两边的操作数类型不一样时会发生隐式类型的提升 解决办法1:将pos强转为int
//注意: >= 一定要强转 如果想用无符号 判断部分就不能是>=
/*while (end >= (int)pos)
{
_str[end + 1] = _str[end];
--end;
}*/
//解决方法2:将_str[end-1]赋值给_str[end]
size_t end = _size + 1;
while (end > pos)
{
_str[end] = _str[end - 1];
--end;
}
_str[pos] = ch;
++_size;
return *this;
}
string& insert(size_t pos, const char* str)
{
size_t len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
/*int end = _size;
while (end >= (int)pos)
{
_str[end + len] = _str[end];
--end;
}*/
size_t end = _size + len;
//问题:当end < len _str[end - len]存在越界
//while (end > pos)
while (end >= pos + len)
{
_str[end] = _str[end - len];
--end;
}
//使用strcpy(_str + pos,str)会拷贝\0使字符串提前结束
strncpy(_str + pos, str, len);
_size += len;
return *this;
}
string& erase(size_t pos, size_t len = npos)
{
assert(pos < _size);
if (len == npos || pos + len >= _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
return *this;
}
size_t find(char ch, size_t pos = 0) const
{
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) const
{
assert(pos < _size);
char* ptr = strstr(_str + pos, str);
if (ptr == nullptr)
{
return npos;
}
else
{
return ptr - _str;
}
}
文章来源:https://www.toymoban.com/news/detail-470622.html
11.<<和>>的运算符重载
ostream& operator<<(ostream& out, const string& s)
{
for (size_t i = 0; i < s.size(); ++i)
{
out << s[i];
}
return out;
}
istream& operator>>(istream& in, string& s)
{
//该写法不需要频繁扩容
s.clear();
char buff[128] = { '\0' };
size_t i = 0;
char 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;
}
文章来源地址https://www.toymoban.com/news/detail-470622.html
12.模拟实现string的整体代码
#include<iostream>
#include<assert.h>
using namespace std;
namespace wjq
{
class string
{
public:
typedef char* iterator;
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
//注意: '\0' "\0" ""的区别
string(const char* str = "")
{
//对空指针不能strlen
_size = strlen(str);
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str, str);
}
void swap(string& s)
{
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
/*string()
{
_str = new char[1];
_str[0] = '\0';
_capacity = _size = 0;
}*/
//拷贝构造传统写法
//s2(s1)
string(const string& s)
{
_str = new char[s._capacity + 1];
_capacity = s._capacity;
_size = s._size;
strcpy(_str, s._str);
}
//拷贝构造现代写法
//s2(s1)
string(const string& s)
:_str(nullptr)
,_size(0)
,_capacity(0)
{
string tmp(s._str);//构造函数
//交换s2和tmp
//this->swap(tmp);
swap(tmp);
}
//赋值重载传统写法:
//s1=s3
string& operator=(const string& s)
{
if (this != &s)
{
char* tmp = new char[s._capacity + 1];
strcpy(tmp, s._str);
//释放s1的旧空间
delete[] _str;
_str = tmp;
_size = s._size;
_capacity = s._capacity;
}
return *this;
}
//赋值重载现代写法:
//s1=s3
//string& operator=(const string& s)
//{
// //string tmp(s._str);
// string tmp(s);
// swap(tmp);
// return *this;
//}
//s1=s3
string& operator=(string s)
{
//不用传引用传参 就用传值传参 s是s3的拷贝构造
swap(s);
return *this;
}
~string()
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
const char* c_str() const
{
return _str;
}
size_t size() const
{
return _size;
}
size_t capacity() const
{
return _capacity;
}
//普通对象可读可写
char& operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];
}
//const对象:只读
const char& operator[](size_t pos) const
{
assert(pos < _size);
return _str[pos];
}
void reserve(size_t n)
{
//保证只扩容不缩容
if (n > _capacity)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
void resize(size_t n, char ch = '\0')
{
if (n > _size)
{
reserve(n);
for (size_t i = _size; i < n; ++i)
{
_str[i] = ch;
}
_size = n;
_str[_size] = '\0';
}
else
{
_str[n] = '\0';
_size = n;
}
}
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 + _size, str);
_size += len;
}
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;
//操作符两边的操作数类型不一样时会发生隐式类型的提升 解决办法1:将pos强转为int
//注意: >= 一定要强转 如果想用无符号 判断部分就不能是>=
/*while (end >= (int)pos)
{
_str[end + 1] = _str[end];
--end;
}*/
//解决方法2:将_str[end-1]赋值给_str[end]
size_t end = _size + 1;
while (end > pos)
{
_str[end] = _str[end - 1];
--end;
}
_str[pos] = ch;
++_size;
return *this;
}
string& insert(size_t pos, const char* str)
{
size_t len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
/*int end = _size;
while (end >= (int)pos)
{
_str[end + len] = _str[end];
--end;
}*/
size_t end = _size + len;
//问题:当end < len _str[end - len]存在越界
//while (end > pos)
while(end >= pos + len)
{
_str[end] = _str[end - len];
--end;
}
//使用strcpy(_str + pos,str)会拷贝\0使字符串提前结束
strncpy(_str + pos, str, len);
_size += len;
return *this;
}
string& erase(size_t pos, size_t len = npos)
{
assert(pos < _size);
if (len == npos || pos + len >= _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
return *this;
}
size_t find(char ch, size_t pos = 0) const
{
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) const
{
assert(pos < _size);
char* ptr = strstr(_str + pos, str);
if (ptr == nullptr)
{
return npos;
}
else
{
return ptr - _str;
}
}
void clear()
{
_size = 0;
_str[0] = '\0';
}
private:
char* _str;
size_t _size;
size_t _capacity;
//const的静态整型变量可以在类中进行初始化
const static size_t npos = -1;
};
ostream& operator<<(ostream& out, const string& s)
{
for (size_t i = 0; i < s.size(); ++i)
{
out << s[i];
}
return out;
}
istream& operator>>(istream& in, string& s)
{
/*char ch;
in >> ch;
while (ch != ' ' && ch != '\n')
{
s += ch;
in >> ch;
}*/
/*char ch = in.get();
while (ch != ' ' && ch != '\n')
{
s += ch;
ch = in.get();
}*/
//该写法不需要频繁扩容
s.clear();
char buff[128] = { '\0' };
size_t i = 0;
char 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;
}
}
到了这里,关于[C++]string及其模拟实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!