深浅拷贝现代写法

插入

insert

插入字符串

	string& insert(size_t pos, char* str)
	{
		assert(pos < _size);
		int len = strlen(str);
		if (len + _size > _capacity)
		{
			reserve(len + _size);
		}
		int end=_size;
		while (end >= pos)
		{
			_str[end + pos] = _str[end];
			--end;
		}
		strncpy(_str + pos, str, len);
		_size += len;
		return *this;
	}

在第0个位置插入时会发生错误。

pos 无符号

end 有符号

为了防止截断有符号会转为无符号,所以应在pos前加(int)pos

插入字符

		void insert(size_t pos, char ch)
		{
			assert(pos < _size);
			if (_size == _capacity)
			{
				size_t newcapacity = _capacity == 0 ? 2 : _capacity * 2;
				reserve(newcapacity);
			}
			int end = _size;
			while (end >= pos)
			{
				_str[end+1] = _str[end];
				--end;
			}
			_str[pos] = ch;
			++_size;
		}

拷贝

strncpy

与strcpy相比可以指定拷贝的长度。

erase

string& erase(size_t pos, size_t len = string::npos)
{
	if (len >= _size - pos)
	{
		_str[pos] = 0;
	}
	else
	{
		int i = pos + len;
		while (i <= _size)
		{
			_str[i - len] = _str[i];
			i++;
		}
		_size -= len;
		//_str[_size] = 0;---> i < _size
	}
	return *this;
}

resize

resize应分三种情况,考虑n与_size的情况

void resize(int n, char ch = '\0')
{
	if (n < _size)
	{
		_str[n] = '\0';
		_size = n;
	}
	else
	{
		if (n > _capacity)
			reserve(n);
		for (int i = _size; i < n; i++)
			_str[i] = ch;
		_size = n;
		_str[_size] = 0;
	}
}

查找

字符

size_t find(char ch, size_t pos = 0)
{
	for (int i = pos; i < _size; ++i)
	{
		if (ch == _str[i])
			return i;
	}
	return string::npos;
}

字符串

size_t find(const char* str, size_t pos = 0)
{
	char*p=strstr(_str, str);
	if (p == nullptr) return string::npos;
	else return p - _str;
}

 重载operator>>

istream& operator>>(istream& in, string& s)
{
	int i;
	for (; ;)
	{
		char ch;
		//in >> ch;
		ch = in.get();
		if (ch==' '||ch == '\n')
			break;
		else
			s += ch;
	}
	return in;
}

深浅拷贝现代写法

string(const string& s)
	:_str(nullptr)
{
	string tmp(s);
	swap(_str, tmp._str);
}

_str应置空,随机值析构时会报错。

operator=

string& operator=(const string& s)
{
	if (this != &s)
	{
		string tmp(s);
		swap(_str, tmp._str);
	}
	return *this;
}

tmp是临时对象出了作用域会调析构函数。赋值不存在_str为空的情况。

string& operator=(string& s)
{
	swap(_str, s._str);
	return *this;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值