string的主要接口介绍和简单实现

1.标准库的string的使用


1.1 string类

string类的文档介绍
在使用string类时,必须包含#include头文件以及using namespace std;

1.2 auto和范围for

  • 在早期C/C++中auto的含义是:使用auto修饰的变量,是具有自动存储器的局部变量,后来这个不重要了。C++11中,标准委员会变废为宝赋予了auto全新的含义即:auto不再是一个存储类型
    指示符,而是作为一个新的类型指示符来指示编译器,auto声明的变量必须由编译器在编译时期
    推导而得。
  • 用auto声明指针类型时,用auto和auto*没有任何区别,但用auto声明引用类型时则必须加&
  • 当在同一行声明多个变量时,这些变量必须是相同的类型,否则编译器将会报错,因为编译器实际
    只对第一个类型进行推导,然后用推导出来的类型定义其他变量。
  • auto不能作为函数的参数,可以做返回值,但是建议谨慎使用
  • auto不能直接用来声明数组

2. string类的简单实现和常用接口简单介绍


2.1 string默认成员函数实现

1. 传统实现方法

//默认构造函数
string(const char* str = "")
{
	int len = strlen(str);
	_str = new char[len+1];
	strcpy(_str, str);
	//容量啥的都不算\0
	_size =_capacity= len;
}
//拷贝构造
string(const string& s)
{
//都是深拷贝如果不是会造成
//1.会进行两次析构
//2. 修改其中一个对象另一个也会变
	_str = new char[s._size + 1];
	strcpy(_str, s._str);
	_size = s._size;
	_capacity = s._capacity;
}
string& operator=(const string& s)
{
	//自己与自己进行赋值
	//会造成我们把s中的str给this时进行字符串拷贝
	//地址会是不确定的
	if (this != &s)
	{
		delete[] _str;
		_str = new char[s._size + 1];
		strcpy(_str, s._str);
		_size = s._size;
		_capacity = s._capacity;
	}
	return *this;
}
~string()
{
	delete[] _str;
	_str = nullptr;
	_capacity = _size = 0;
}

2. 现代版写法的实现

//默认构造函数
string(const char* str = "")
:_str(nullptr)
{
	int len = strlen(str);
	_str = new char[len+1];
	strcpy(_str, str);
	//容量啥的都不算\0
	_size =_capacity= len;
	
}
//拷贝构造
string(const string& s)
{
	string tem = s._str;
swap(tem);
}
string& operator=(string& s)
{
	swap(s);
	return *this;
}
~string()
{
	delete[] _str;
	_str = nullptr;
	_capacity = _size = 0;
}

上面两个版本有什么区别?

  • 其实重点在于赋值运算符的重载
    不仅拿到了我们想要的还把我们原来的资源给释放了
  • 注意:
    我们在构造函数最好给_str空指针防止交换后调用析构函数有风险因为对空指针free是没有问题的

上面的swap函数是我们自己实现的如下:

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

我们知道在算法库中有swap函数如下
在这里插入图片描述

如果我们直接调用该模版函数要先拷贝构造还有两次赋值运算法重载效率太低
如果自己实现交换的是内置类型会提升效率。当然我们能想到,那么库里面的只会比我们好,因此他们在全局实现了一个swap函数如下:

在这里插入图片描述
在这里插入图片描述

里面具体的实现和我们的差不多当这个函数和算法库的那个函数模版同时存在会优先调用改swap因此我们直接swap也可以

2.2 迭代器的实现

string::iterator string::begin()
{
	return this->_str;
}
string::iterator string::end()
{
	return this->_str+this->_size;
}

直接返回首地址就行

2.3 modify等接口实现


const char* string::c_str()const
{
	return this->_str;
}
void string::clear()
{
	//内容清楚空间不变
	this->_str[0] = '\0';
	this->_size = 0;
}



void string::swap(string& s)
{
	std::swap(this->_str, s._str);
	std::swap(this->_size, s._size);
	std::swap(this->_capacity, s._capacity);
}
void string::push_back(char c)
{
	//检查是否要扩容
	if (_capacity == _size)
	{
		reserve(_capacity == 0 ? 4 : 2 * _capacity);
	}
	_str[_size++] = c;
	_str[_size] = '\0';
	}
string& string::operator+=(char c)
{
	this->push_back(c);
	return *this;
}
string& string::append(const char* str)
{
	int len = strlen(str);
	for (int i = 0; i < len; i++)
	{
		push_back(str[i]);
	}
	return *this;
}
string& string::operator+=(const char* str)
{
	int len = strlen(str);
	reserve(_size + len);
	for (int i = 0; i < len; i++)
	{
		push_back(str[i]);
	}
	return *this;
}

2.4 access的实现

operator[]函数实现

char& string::operator[](size_t index)
{
	assert(index >= 0 && index < this->_size);
	return _str[index];
}
const char& string::operator[](size_t index)const
{
	assert(index >= 0 && index < this->_size);
	return _str[index];
}

2.5 relational operators等接口的实现

bool string::operator<(const string& s)
{
	return	strcmp(_str, s._str)<0;
}

bool string::operator<=(const string& s)
{
	return	strcmp(_str, s._str) <= 0;
}

bool string::operator>(const string& s)
{
	return	strcmp(_str, s._str) > 0;
}

bool string::operator>=(const string& s)
{
	return	strcmp(_str, s._str) >= 0;
}

bool string::operator==(const string& s)
{
	return	strcmp(_str, s._str) == 0;
}

bool string::operator!=(const string& s)
{
	return	strcmp(_str, s._str) != 0;
}

2.6 查找插入删除接口的实现

// 返回c在string中第一次出现的位置

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

// 返回子串s在string中第一次出现的位置

size_t string::find(const char* s, size_t pos ) const
{
	int ip = 0;
	int len = strlen(s);
	for (int i = pos; i < _size; i++)
	{
		ip = i;
		int count = 1;
		for (int j = 0; j < len; j++)
		{
			if (_str[ip] != s[j])
			{
				count = 0;
				break;
			}
			ip++;
		} 
		if (count == 1) return i-pos;
	}
	return std::string::npos;
}

 //在pos位置上插入字符c/字符串str,并返回该字符的位置

string& string::insert(size_t pos, char c)
{
	assert(pos <= _size);
	if (_size == _capacity)
	{
		reserve(_capacity == 0 ? 4 : 2 * _capacity);
	}
	//注意这里与pos比较时注意-1与pos的比较-1会转换为unsigned int在比较
	for (int i = _size+1; i > pos; i--)
	{
		_str[i] = _str[i-1];
	}
	_str[pos] = c;
	return *this;
}

string& string::insert(size_t pos, const char* str)
{
	assert(pos <=_size);
	int len = strlen(str);
	//需要扩容
	if (len + _size > _capacity)
	{
		reserve(len + _size > 2 * _capacity ? len + _size : 2 * _capacity);
	}
	for (int i = _size + len; i >= pos+len; i--)
	{
		_str[i] = _str[i - len];
	}
	for (int i = 0; i < len; i++)
	{
		_str[pos++] = str[i];
	}
	return *this;
}



string& string::erase(size_t pos, size_t len)
{
	assert(pos < _size);
	//len超过pos位置起剩余元素个数
	if (len >= _size - pos)
	{
		_str[pos] = '\0';
		_size = pos;
	}
	else
	{
		for (int i = pos; i < pos + len; i++)
		{
			_str[i] = _str[i + len];
		}
		_size -= len;
		_str[_size] = '\0';
	}
	return* this;

}

2.7 流插入流提取的实现

ostream& operator<<(ostream& out, const string& s)
{
	out << s.c_str() << endl;
	return out;
}
istream& operator>>(istream& in, string& s)
{
	s.clear();
	const int N = 567;
	char buffer[N];
	char ch;
	int i = 0;
	ch = in.get();
	while (ch != ' ' && ch != '\n')
	{
		buffer[i++] = ch;
		if (i == N - 1)
		{
			buffer[i] = '\0';
			s += buffer;
			i = 0;
		}
		ch = in.get();
	}
	if (i > 0)
	{
		buffer[i] = '\0';
		s += buffer;
	}
	return in;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值