「C++」string类模拟实现


前言

string 是C++早期实现的一个内置容器,那时STL还没有出现,所以string有不少地方会让人感到诧异与不成熟,但即使如此,现在的string也已经成了C++不可却少的一部分。


一、string类的介绍

string类是用来表示字符串的对象,提供了一系列用于操作字符串的方法,例如查找子串、连接字符串、比较字符串等。

string的特性

  • string类是基于模板类 base_string 的 cha 类型的实例化。
  • 支持STL的基本接口,可以和STL算法一起使用,如sort、reverse等。
  • 使用动态分配内存,在创造string时,会根据使用的字符串大小自动分配内存
  • string内部使用用零终止符’\0’来表示字符串的终结。

string的主要接口

构造函数

成员函数功能
string()构造空的字符串
string(const char* s)用C风格字符串构造string对象
string(size_t n, char c)构造大小为n的字符串,内容全为c
string(const string& s)拷贝构造函数

容量操作

成员函数功能说明
size ()返回字符串长度
capacity ()返回空间总大小
reserve (size_ n)为字符预留空间
resize (size_t n, char c)将字符大小设为n,多出的空间用c填充

访问与遍历操作

成员函数功能说明
operator[ ]返回pos位置的字符
begin + endbegin返回第一个字符的迭代器,end返回最后一个字符的下一个位置的迭代器
rbegin+ rend和begin和end相反

修改操作

成员函数功能说明
push_back (char c)在字符串后插入字符
append (const char* str)在字符串后追加字符串
c_str ( )返回C风格
find (size_t pos, char c)从字符串pos位置寻找字符c,返回该字符的位置
substr (size_t pos, size_t n)从pos位置开始,选取n个字符将其返回
npossize_t 的最大值(-1)

二、代码实现

类的声明

class string 
{
private:
    size_t _size;		// 声明顺序影响初始化顺序
    size_t _capacity;
    char* _str;
public:		//部分成员函数
    typedef char* iterator;		//迭代器
    typedef const char* const_iterator;	
    const static size_t npos;	
    
   	string(const char* str = "");		//构造函数
   	string(const string& s);
 	~string() { delete[] _str; _str = nullptr; };
	string& operator=(const string& s);
   	
   	void resize(size_t n, char c);		//容量操作
   	void reserve(size_t n);
   	void swap(string& s);
   	
   	string& insert(size_t pos, const char* str)	//插入删除
   	string& erase(size_t pos, size_t len = npos);
};

构造函数

构造函数主要的难点就是深拷贝浅拷贝的问题, 如果没有显式定义赋值函数、拷贝函数,编译器会自动生成默认的拷贝/赋值函数,它们只会做浅拷贝,也就是只会把另一个对象的成员变量直接传值拷贝,但string每个对象内部都使用动态内存开辟字符串的空间,所以要给每个对象都独立分配空间。

string::string(const char* str) 	// 注意:缺省值只要在头文件或cpp文件声明一次即可
	:_size(strlen(str))
	,_capacity(_size)
	,_str(new char[_capacity+1])	//留一个字节给字符串末尾的'\0'
{
	assert(str);
	strcpy(_str, str);
}

//现代写法
string::string(const string& s) : _str(nullptr)
{
	string tem(s._str);	//创建用s的字符串临时变量
	swap(tem);		//交换*this和tem的内容
}

//传统写法
//string::string(const string& s)
//	:_size(s._size)
//	,_capacity(s._capacity)
//	,_str(new char[_capacity+1])
//{
//	strcpy(_str, s._str);	//深拷贝
//}

string& string::operator=(const string& s)
{
	if(this != &s)
	{
		string tem(s);
		swap(tem);
	}
	
	return *this;
}

string::swap(string& s)
{
	std::swap(_str, s._str);
	std::swap(_capacity, s._capacity);
	std::swap(_size, s._size);
}

容量操作

resize是把数据中的有效字符数改为n个,reserve是为字符串预留空间,在频繁需要扩容的场景可以增加效率。

void string::resize(size_t n, char ch)
{
	if(n < _size)	
	{
		_str[n] = '\0';	
		_size = n;
	}
	else 
	{
		reserve(n);
		while(_size < n)	//多余的空间设为ch
		{
			_str[_size++] = ch;
		}
		_str[_size] = '\0';
	}
}

void string::reserve(size_t n)
{
	if (_capacity >= n)	
		return;
		
	_capacity = n;
	char* s = new char[n];
	strcpy(s, _str);	
	delete[] _str;	//复制完原有数据再删除
	_str = s;		//数据复位
}

void string::swap(string& s)	//string内置的swap
{
	std::swap(_str, s._str);	//交换数据
	std::swap(_size, s._size);
	std::swap(_capacity, s._capacity);
}

修改操作

扩展内存空间时,需要注意指针的初始位置与结束位置。

string& string::insert(size_t pos, char c)		//插入字符
{
	assert(pos <= _size);
	
    if(_size >= _capacity)
        reserve(_capacity * 2);	
        
    size_t end = _size + 1;	//设为_size+1避免当pos为0时,end为-1陷入死循环
    while(end > pos)
    {
        _str[end] = _str[end-1];
        end--;
    }

    _str[pos] = c;
    _size++;

    return *this;
}

 
 string& string::erase(size_t pos, size_t len)	//删除指定长度的子串
{
    assert(pos < _size);

    if(len == npos || pos + len >= _size)	
    {
        _str[pos] = '\0';
        _size = pos;   
    }
    else 
    {
        size_t begin = pos + len;
        while (begin <= _size)
        {
            _str[begin -len] = _str[begin++];
        } 
        _size -= len;
    }

    return *this;
 }
 
string string::substr(size_t pos, size_t len)	//提取子串
{
    string tem;
    size_t end = len + pos;
    if(len == npos || pos + len >= _size)	//当截取大小大于剩余空间
    {
        len = _size - pos;
        end = _size;
    }

    tem.reserve(len);
    for(int i = pos; i < end; ++i)
    {
        tem += _str[i];
    }

    return tem;
}

总结

string类存储字符串和处理字符串函数的集合,使用它能够大幅减少重复造轮子的痛苦。

完整代码:github

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值