C++——string的模拟实现+详细讲解

迭代器

typedef char* iterator;
typedef const char* const_iterator;

定义迭代器的类型。iterator 是一个指向 char 类型的指针,可以用来访问和修改 char 类型的数据。而 const_iterator 是一个指向常量 char 类型的指针,只能用来访问 char 类型的数据,不能修改。

//迭代器:开始
iterator begin()
{
	return _str;
}
//迭代器:结束
iterator end()
{
	return _str + _size;
}
//const迭代器:开始
const_iterator begin() const
{
	return _str;
}
//const迭代器:结束
const_iterator end() const
{
	return _str + _size;
}
  1. begin() 返回一个迭代器的起始位置
  2. end() 返回一个迭代器的结束位置
  3. begin() const 是一个常量迭代器,只能访问,不能修改,返回一个迭代器的
  4. end() const 是一个常量迭代器,只能访问,不能修改,返回一个迭代器的结束位置

构造函数

//构造函数
string(const char* str = "")
	: _size(strlen(str))
	, _capacity(_size)
	, _str(new char[_capacity + 1])
{
	//strcpy(_str, str);
	memcpy(_str, str, _size + 1);
}
  1. 定义了一个字符串类的构造函数,他接受了一个 const char* 类型的参数str,默认为空字符串
  2. _size(strlen(str))计算字符串的字符串的大小
  3. _capacity(_size)计算字符串的容量
  4. _str(new char[_capacity + 1])在堆上分配一个字符数组,将他的地址赋给成员变量———_str,用来存放字符串的指针
  5. 最后,用memcpy函数将参数str的内容复制到_str指向的内存空间中,这里使用memcpy函数而不是strpy函数,是因为memcpy函数能够复制给定长度的字符到目标区域,而strcpy函数遇到\0就结束了,+1是为了在_str字符数组的结尾加一个空字符\0

拷贝构造函数

//拷贝构造函数
string(const string& s)
{
	_str = new char[s._capacity + 1];
	memcpy(_str, s._str, s._size + 1);
	_size = s._size;
	_capacity = s._capacity;
}
  1. 定义了一个字符串类拷贝构造函数,它接受一个 const string& 类型的参数 s,表示要拷贝的字符串对象。
  2. new char[s._capacity + 1] 在堆上动态分配了一个字符数组,并将它的地址赋给成员变量 _str,即存储字符串的指针。
  3. 利用memcpy函数将s._str指向的字符串数组的内容复制到_str指向的空间中,+1是为了_str字符数组的结尾加‘\0’,保证字符串的正常结束
  4. _size = s._size表示将s._size的值赋给成员变量_size,表示字符串的大小
  5. _capacity = s._capacity表示将 s._capacity 的值赋给成员变量 _capacity,表示字符串的容量。

赋值运算符重载函数

void swap(string& s)
{
	std::swap(_str, s._str);
	std::swap(_size, s._size);
	std::swap(_capacity, s._capacity);
}
string& operator=(string tmp)
{
	swap(tmp);
	return *this;
}

通过拷贝并交换,将临时对象tmp和当前对象进行交换,把临时变量tmp的内容拷贝到当前对象中,

最后返回当前对象的引用

析构函数

~string()
{
	delete[] _str;
	_str = nullptr;
	_size = _capacity = 0;
}
  1. delete[] _str释放字符串对象中动态分配的内存空间
  2. _str = nullptr表示将_str指针置为nullptr,表示该指针不指向任何有效的内存空间
  3. _size_capacity的值全部置为0,表示没有任何空间了

获取字符串函数

const char* c_str() const
{
	return _str;
}

c_str() 函数返回一个指向字符串数组的常量指针,指向的字符可以用来读取字符串的内容,但不能修改。

获取字符串的字符个数

size_t size() const
{
	return _size;
}

这段代码是一个字符串类中的成员函数 size() 的实现,其功能是返回字符串对象中存储的字符数量。

访问类对象中的成员

实现对类对象中成员的访问和操作

char& operator[](size_t pos)
{
	assert(pos < _size);
	return _str[pos];
}

函数的返回类型是char&,表示返回一个字符的引用。 函数名是operator[],表示这个函数是对[]运算符的重载。函数参数是size_t pos,表示要访问的元素的位置。assert断言确保pos小于数组的大小_size,函数返回 _str[pos],即数组中位置为 pos 的字符的引用。

实现对类对象中的成员的常量访问

const char& operator[](size_t pos) const
{
	assert(pos < _size);
	return _str[pos];
}

函数的返回类型是const char&,表示返回一个字符的常量引用。 函数名是operator[],表示这个函数是对[]运算符的重载。 函数参数是size_t pos,表示要访问的元素的位置。

在函数的实现中,首先通过assert语句进行断言验证,确保要访问的位置 pos 小于数组的大小 _size,以防止越界访问。然后,函数返回 _str[pos],即数组中位置为 pos 的字符的常量引用。

字符串容量调整

void reserve(size_t n)
{
	if (n > _capacity)
	{
		char* tmp = new char[n + 1];
		memcpy(tmp, _str, _size + 1);
		delete[] _str;
		_str = tmp;
		_capacity = n;
	}
}
  1. 如果函数n大于当前容量_capacity,重新分配更大的空间
  2. 创建一个新的字符串数组tmp,大小为n+1,用来存储新分配的内存空间。接着,将_str的内容复制到tmp中。
  3. 接下来,释放_str的内存空间,在将指针_str指向tmp
  4. 最后,函数更新容量_capacity的值为n

字符串大小调整

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

如果需要调整的大小n小于当前的大小_size,就缩小字符串的大小。将_size更新为n,将_str[_size]设置为‘\0’,截断字符串保留前个字符串,字符串以‘\0’结尾

如果需要调整的大小n大于等于当前的大小_size,扩大字符串并且填充新位置。

尾部插入字符

void push_back(char ch)
{
	if (_size == _capacity)
	{
		//二倍扩容
		reserve(_capacity == 0 ? 4 : _capacity * 2);
	}
	_str[_size] = ch;
	++_size;
	_str[_size] = '\0';
}
  1. 如果_size等于_capacity进行扩容
  2. 扩容时如果发现容量等于0,就将容量设置为4;就将容量扩大为当前容量的两倍。
  3. 接着,将字符ch赋值给字符串_str的末尾位置,并且将字符串的大小_size+1
  4. 最后将字符串的最后一个字符 _str[_str]设置为‘\0’,以确保字符串以‘\0’结尾

尾部插入字符串

void append(const char* str)
{
	size_t len = strlen(str);
	if (_size + len > _capacity)
	{
		//至少扩容到_size+len
		reserve(_size + len);
	}
	memcpy(_str + _size, str, len + 1);
	_size += len;
}
  1. 如果当前字符串的大小 _size 加上要添加的字符串的长度 len 超过了容量 _capacity就需要扩容;如果小于等于就不需要扩容。
  2. 扩容大小为_size+len,函数使用memcpy函数将要添加的字符串str复制到当前字符串的末尾位置_str+_size
  3. 更新字符串的大小_size,因为字符串的最后一个字符默认是‘\0’,不需要再设置了

重载函数符+=

字符串尾部添加字符

string& operator+=(char ch)
{
	push_back(ch);
	return *this;
}

使用push_back函数,将字符ch添加到字符串的末尾

实现了字符串对象对字符的追加操作,通过这函数,可以将一个字符追加到字符串的末尾

字符串尾部添加字符串

string& operator+=(const char* str)
{
	append(str);
	return *this;
}

该函数的功能是将指定的字符串追加到当前字符串的末尾,并返回一个引用指向当前字符串对象。

指定位置插入字符

void insert(size_t pos, size_t n, char ch)
{
	assert(pos <= _size);
	if (_size + n > _capacity)
	{
		//至少扩容到_size+n
		reserve(_size + n);
	}
	int end = _size;
	while (end >= (int)pos)
	{
		_str[end + n] = _str[end];
		--end;
	}
	for (size_t i = 0; i < n; i++)
	{
		_str[pos + i] = ch;
	}
}
  1. 首先assert先断言,确保插入位置合法
  2. 如果插入的总长度大于当前容量,就进行扩容,将容量扩展到_size+n
  3. 接着,从字符串的末尾开始,依次往后移,给插入字符留下足够的空间
  4. 最后,使用一个循环将指定数量的字符ch插入到指定位置pos之后。

指定位置插入字符串

void insert(size_t pos, const char* str)
{
	assert(pos <= _size);
	size_t len = strlen(str);
	if (_size + len > _capacity)
	{
		//至少扩容到_size+len
		reserve(_size + len);
	}
	int end = _size;
	while (end >= (int)pos)
	{
		_str[end + len] = _str[end];
		--end;
	}
	for (size_t i = 0; i < len; i++)
	{
		_str[pos + i] = str[i];
	}
	_size += len;
}
  1. 首先使用assert断言来确保插入位置pos不超过当前字符串的长度_size
  2. 如果插入后的总长度_size+len超过当前容量_capacity,就进行扩容
  3. 接着,将字符串的末尾开始,依次往后移动字符,为插入字符串成留出足够的位置。
  4. 然后使用一个循环将字符串中字符赋值到pos之后的指定位置
  5. 最后,更新字符串的长度_size,将__size添加len的值

删除指定长度的字符

void erase(size_t pos, size_t len = npos)
{
	assert(pos <= _size);
	if (len == npos || pos + len >= _size)
	{
		_str[pos] = '\0';
		_size = pos;
		_str[_size] = '\0';
	}
	else
	{
		size_t end = pos + len;
		while (end <= _size)
		{
			_str[pos++] = _str[end++];
		}
		_size -= len;
	}
}
  1. 首先使用assert断言来确保删除起始位置pos不超过当前字符串的长度_size
  2. 如果条件成立,则表示要删除的是从起始位置到字符串结尾的所有字符。
  3. 如果不满足删除全部字符的情况,表示删除的是指定长度的一部分字符。首先确定要删除的位置end,即pos+len;然后使用循环将待删除位置之后的字符逐个向前移动,覆盖待删除的字符;在移动字符完毕后,将字符串的长度减去待删除的长度len,即_size=len;

查找指定字符的位置

size_t find(char ch, size_t pos = 0)
{
	assert(pos < _size);
	for (size_t i = pos; i < _size; i++)
	{
		if (_str[i] == ch)
		{
			return i;
		}
	}
	return npos;
}
  1. 首先,使用断言assert验证起始位置pos是否小于字符串的长度_size,避免越界。
  2. 然后,使用循环依次比较每一个字符和要查找的字符ch
  3. 如果找到了与ch相等的字符,则返回该字符的索引位置i
  4. 如果遍历完整个字符串都没有找到与ch相等的字符`,则返回npos,表示没有找到

查找指定子字符串

size_t find(const char* str, size_t pos = 0)
{
	assert(pos < _size);;
	const char* ptr = strstr(_str, str);
	if (ptr)
	{
		return ptr - _str;
	}
	return npos;
}
  1. 首先,使用断言assert验证起始位置pos是否小于字符串的长度_size,防止越界。
  2. 然后,使用strstr()_str字符串中查找子字符串str的出现位置。如果找到了返回一个指向第一次出现的位置的指针,如果找不到则返回NULL
  3. 如果找到子字符串str,则使用指针相减的方式计算出相对于-str的偏移量,即所求的起始位置
  4. 如果没有找到,则返回npos,表示没有找到

从字符串中提取子字符串

string substr(size_t pos = 0, size_t len = npos)
{
	assert(pos < _size);
	size_t n = len;
	if (len == npos || pos + len > _size)
	{
		n = _size - pos;
	}
	string tmp;
	tmp.reserve(n);
	for (size_t i = pos; i < pos + n; i++)
	{
		tmp += _str[i];
	}
	return tmp;
}
  1. 首先,使用断言assert验证起始位置pos是否小于字符串的长度,避免越界。
  2. 如果lennpos或者pos+len超过了字符串的长度,n被设置为从pos到字符串末尾的长度
  3. 接下来,创建一个临时的字符串对象tmp,并使用reserve()为该字符串分配足够的容量以容纳字符串
  4. 然后,使用一个循环,依次将pospos+n的字符添加到tmp字符串中
  5. 最后,返回存储子字符串tmp字符串对象

清空字符串

void clear()
{
	_str[0] = '\0';
	_size = 0;
}
  1. 首先将字符串的第一个字符_str[0]设置为‘\0’来将字符串的内容置为空
  2. 然后,将字符串的大小_size设置为0,表示字符串中不包含任何字符

字符串比较

//<
bool operator<(const string& s)
{
	int ret = memcmp(_str, s._str,_size<s._size?_size:s._size);
	return ret == 0 ? _size < s._size: ret < 0;
}

函数内部使用了memcmp函数进行字符串的比较。memcmp函数比较两个内存块的内容,并返回一个整数作为比较的结果。

在这个函数中,memcmp函数比较了两个字符串的内存块的内容和大小:

它使用了_strs._str来分别表示当前字符串对象和参数字符串对象的内存块。

_sizes._size表示当前字符串对象和参数字符串对象的大小。

函数中的表达式_size < s._size ? _size : s._size是为了确定两个字符串之间较小的大小,以便在memcmp函数中进行比较。

函数通过比较memcmp函数的返回值和0来判断两个字符串的大小关系:

  • 如果返回值等于0,表示两个字符串的内容相等。
  • 如果返回值小于0,表示当前字符串对象小于参数字符串对象。
  • 如果返回值大于0,表示当前字符串对象大于参数字符串对象。

函数最终返回一个bool类型的值表示最终的大小比较结果:

  • 如果ret等于0,则返回_size < s._size的结果,表示当前字符串对象的大小是否小于参数字符串对象的大小。
  • 如果ret小于0,则直接返回true,表示当前字符串对象小于参数字符串对象。
  • 否则,返回false,表示当前字符串对象大于参数字符串对象。
//==
bool operator==(const string& s)
{
	return _size == s._size && memcpy(_str, s._str, _size) == 0;
}
  1. 首先,通过_size == s._size判断当前字符串对象和参数字符串对象的大小是否相等,如果不相等就直接返回false
  2. 然后,使用memcpy函数来比较当前字符串对象和参数字符串对象的内容。如果两个字符串的内容相等,那么memcpy函数会返回0。 所以,函数体内的memcmp(_str, s._str, _size) == 0即为比较当前字符串对象和参数字符串对象的内容是否相等。
  3. 最终,返回_size == s._size && memcmp(_str, s._str, _size) == 0的结果,即当前字符串对象和参数字符串对象的大小相等并且内容相等,则返回true,表示两个字符串相等;否则返回false,表示两个字符串不相等。
//<=
bool operator<=(const string& s)
{
	return *this < s || *this == s;
}
  • 首先,通过*this < s判断当前字符串对象是否小于参数字符串对象。如果当前字符串对象小于参数字符串对象,则返回true
  • 否则,通过*this == s判断当前字符串对象是否等于参数字符串对象。如果当前字符串对象等于参数字符串对象,则返回true
  • 最终,返回 *this < s || *this == s的结果,即当前字符串对象小于等于参数字符串对象则返回true,否则返回false
//>
bool operator>(const string& s)
{
	return !(*this <= s);
}
  • 首先,通过*this <= s判断当前字符串对象是否小于等于参数字符串对象。如果当前字符串对象小于等于参数字符串对象,则返回false
  • 否则,通过!(*this <= s)判断当前字符串对象不小于等于参数字符串对象。如果当前字符串对象不小于等于参数字符串对象,则返回true
//>=
bool operator>=(const string& s)
{
	return !(*this < s);
}
  • 首先,通过*this < s判断当前字符串对象是否小于参数字符串对象。如果当前字符串对象小于参数字符串对象,则返回false
  • 否则,通过!(*this <s)判断当前字符串对象不小于参数字符串对象。如果当前字符串对象不小于参数字符串对象,则返回true
//!=
bool operator!=(const string& s)
{
	return !(*this == s);
}
  • 首先,通过*this == s判断当前字符串对象是否等于参数字符串对象。如果当前字符串对象等于参数字符串对象,则返回false。
  • 否则,通过!(*this == s)判断当前字符串对象不等于参数字符串对象。如果当前字符串对象不等于参数字符串对象,则返回true

输出流插入运算符重载函数

ostream& operator<<(ostream& out, const string& s)
{
	for (auto ch : s)
	{
		out << ch;
	}
	return out;
}
  1. 函数的参数是一个输出流对象和一个常量字符串引用
  2. 遍历每一个字符,将字符依次输出到给定的流对象中
  3. 最后,函数返回输出流对象的引用

输入流提取运算符重载函数

istream& operator>>(istream& in, string& s)
{
	s.clear();
	char ch=in.get();
	//处理掉缓冲区前面的空格或者换行
	while (ch == ' ' || ch == '\n')
	{
		ch = in.get();
	}
	//in >> ch;
	char buff[128] = { '\0' };
	int i = 0;
	while (ch != ' ' && ch != '\n')
	{
		buff[i++] = ch;
		if (i == 127)
		{
			buff[i] = '\0';
			s += buff;
			i = 0;
		}
		ch = in.get();
	}
	if (i != 0)
	{
		buff[i] = '\0';
		s += buff;
	}
	return in;
}
  1. 函数的参数是一个输入流对象和一个字符串引用
  2. 首先清空字符串,然后读取输入流中的字符
  3. 在处理输入之前,函数会检查并跳过缓存区前面的空格或者换行符
  4. 然后,将字符一个接一个添加到字符缓冲区中,直到遇到换行符或者空格为止
  5. 如果字符缓冲区已满,将缓冲区的内容追加到字符串中,并清空缓冲区
  6. 最后,函数将最后一个字符缓冲区的内容追加到字符串中(如果缓冲区不为空)。函数返回输入流对象的引用。

完整代码

#pragma once
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include<iostream>
#include<string>
using namespace std;
namespace bit
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;
		//迭代器:开始
		iterator begin()
		{
			return _str;
		}
		//迭代器:结束
		iterator end()
		{
			return _str + _size;
		}
		//const迭代器:开始
		const_iterator begin() const
		{
			return _str;
		}
		//const迭代器:结束
		const_iterator end() const
		{
			return _str + _size;
		}
		/*string()
			:_size(0)
			,_capacity(0)
			,_str(new char[1])
		{
			_str[0] = '\0';
		}*/
		string(const char* str = "")
			: _size(strlen(str))
			, _capacity(_size)
			, _str(new char[_capacity + 1])
		{
			//strcpy(_str, str);
			memcpy(_str, str, _size + 1);
		}
		string(const string& s)
		{
			_str = new char[s._capacity + 1];
			//strcpy(_str, s._str);
			memcpy(_str, s._str, s._size + 1);
			_size = s._size;
			_capacity = s._capacity;
		}
		//赋值
		/*string& operator=(const string& s)
		{
			if (this != &s)
			{
				char* tmp = new char[s._capacity + 1];
				memcpy(tmp, s._str, s._size + 1);
				delete[] _str;
				_str = tmp;
				_size = s._size;
				_capacity = s._capacity;
			}
			return *this;
		}*/
		void swap(string& s)
		{
			std::swap(_str, s._str);
			std::swap(_size, s._size);
			std::swap(_capacity, s._capacity);
		}
		/*string& operator=(const string& s)
		{
			if (this != &s)
			{
				string tmp(s);
				swap(tmp);
			}
			return *this;
		}*/
		string& operator=(string tmp)
		{
			swap(tmp);
			return *this;
		}
		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = _capacity = 0;
		}
		const char* c_str() const
		{
			return _str;
		}
		size_t size() const
		{
			return _size;
		}
		char& operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}
		const char& operator[](size_t pos) const
		{
			assert(pos < _size);
			return _str[pos];
		}

		//增删查改

		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				cout << "reverse()->" <<n<< endl;
				char* tmp = new char[n + 1];
				//strcpy(tmp, _str);
				memcpy(tmp, _str, _size + 1);
				delete[] _str;
				_str = tmp;
				_capacity = n;
			}
		}
		void resize(size_t n, char ch = '\0')
		{
			if (n < _size)
			{
				_size = n;
				_str[_size] = '\0';
			}
			else
			{
				reserve(n);
				for (size_t i = _size; i < n; i++)
				{
					_str[i] = ch;
				}
				_size = n;
				_str[_size] = '\0';
			}
		}
		void push_back(char ch)
		{
			if (_size == _capacity)
			{
				//二倍扩容
				reserve(_capacity == 0 ? 4 : _capacity * 2);
			}
			_str[_size] = ch;
			++_size;
			_str[_size] = '\0';
		}
		void append(const char* str)
		{
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				//至少扩容到_size+len
				reserve(_size + len);
			}
			//strcpy(_str + _size, str);
			memcpy(_str + _size, str, len + 1);
			_size += len;
			//_str[_size] = '\0';
		}
		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}
		string& operator+=(const char* str)
		{
			append(str);
			return *this;
		}
		void insert(size_t pos, size_t n, char ch)
		{
			assert(pos <= _size);
			if (_size + n > _capacity)
			{
				//至少扩容到_size+n
				reserve(_size + n);
			}
			int end = _size;
			while (end >= (int)pos)
			{
				_str[end + n] = _str[end];
				--end;
			}
			for (size_t i = 0; i < n; i++)
			{
				_str[pos + i] = ch;
			}
		}
		void insert(size_t pos, const char* str)
		{
			assert(pos <= _size);
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				//至少扩容到_size+len
				reserve(_size + len);
			}
			int end = _size;
			while (end >= (int)pos)
			{
				_str[end + len] = _str[end];
				--end;
			}
			for (size_t i = 0; i < len; i++)
			{
				_str[pos + i] = str[i];
			}
			_size += len;
		}
		void erase(size_t pos, size_t len = npos)
		{
			assert(pos <= _size);
			if (len == npos || pos + len >= _size)
			{
				_str[pos] = '\0';
				_size = pos;
				_str[_size] = '\0';
			}
			else
			{
				size_t end = pos + len;
				while (end <= _size)
				{
					_str[pos++] = _str[end++];
				}
				_size -= len;
			}
		}
		size_t find(char ch, size_t pos = 0)
		{
			assert(pos < _size);
			for (size_t i = pos; i < _size; i++)
			{
				if (_str[i] == ch)
				{
					return i;
				}
			}
			return npos;
		}
		size_t find(const char* str, size_t pos = 0)
		{
			assert(pos < _size);;
			const char* ptr = strstr(_str, str);
			if (ptr)
			{
				return ptr - _str;
			}
			return npos;
		}
		string substr(size_t pos = 0, size_t len = npos)
		{
			assert(pos < _size);
			size_t n = len;
			if (len == npos || pos + len > _size)
			{
				n = _size - pos;
			}
			string tmp;
			tmp.reserve(n);
			for (size_t i = pos; i < pos + n; i++)
			{
				tmp += _str[i];
			}
			return tmp;
		}
		void clear()
		{
			_str[0] = '\0';
			_size = 0;
		}
		//"hello" "hello" false
		//"helloxx" "hello" false
		//"hello" "helloxx" ture
		/*bool operator<(const string& s)
		{
			size_t i1 = 0;
			size_t i2 = 0;
			while (i1 < _size && i2 < s._size)
			{
				if (_str[i1] < s._str[i2])
				{
					return true;
				}
				else if (_str[i1] > s._str[i2])
				{
					return false;
				}
				else
				{
					++i1;
					++i2;
				}
			}
			if (i1 == _size && i2 != s._size)
			{
				return true;
			}
			else
			{
				return false;
			}
		}*/
		//小于
		bool operator<(const string& s)
		{
			int ret = memcmp(_str, s._str,_size<s._size?_size:s._size);
			return ret == 0 ? _size < s._size: ret < 0;
		}
		//等于
		bool operator==(const string& s)
		{
			return _size == s._size && memcpy(_str, s._str, _size) == 0;
		}
		//小于等于
		bool operator<=(const string& s)
		{
			return *this < s || *this == s;
		}
		//大于
		bool operator>(const string& s)
		{
			return !(*this <= s);
		}
		//大于等于
		bool operator>=(const string& s)
		{
			return !(*this < s);
		}
		//不等于
		bool operator!=(const string& s)
		{
			return !(*this == s);
		}
		
	private:
		size_t _size;
		size_t _capacity;
		char* _str;
	public:
		const static size_t npos;
	};
	const size_t string::npos = -1;
	ostream& operator<<(ostream& out, const string& s)
	{
		for (auto ch : s)
		{
			out << ch;
		}
		return out;
	}
	istream& operator>>(istream& in, string& s)
	{
		s.clear();
		char ch=in.get();
		//处理掉缓冲区前面的空格或者换行
		while (ch == ' ' || ch == '\n')
		{
			ch = in.get();
		}
		//in >> ch;
		char buff[128] = { '\0' };
		int i = 0;
		while (ch != ' ' && ch != '\n')
		{
			buff[i++] = ch;
			if (i == 127)
			{
				buff[i] = '\0';
				s += buff;
				i = 0;
			}
			ch = in.get();
		}
		if (i != 0)
		{
			buff[i] = '\0';
			s += buff;
		}
		return in;
	}
};
//测试部分
void test_string1()
{
	bit::string s1("hello world");
	cout << s1.c_str() << endl;
	bit::string s2;
	cout << s2.c_str() << endl;
	for (size_t i = 0; i < s1.size(); i++)
	{
		s1[i]++;
	}
	cout << endl;
	for (size_t i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << " ";
	}
	cout << endl;

	bit::string::iterator it = s1.begin();
	while (it != s1.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;

	for (auto ch : s1)
	{
		cout << ch << " ";
	}
	cout << endl;
}
void test_string2()
{
	bit::string s1("hello world");
	//cout << s1.c_str() << endl;
	s1.push_back(' ');
	s1.push_back('#');
	s1.push_back('#');
	s1.append("hello bit");
	cout << s1.c_str() << endl;
	bit::string s2("hello world");
	//cout << s1.c_str() << endl;
	s2 += '&';
	s2 += '%';
	s2 += "hello";
	cout << s2.c_str() << endl;
}
void test_string3()
{
	bit::string s3("hello English");
	cout << s3.c_str() << endl;
	s3.insert(0, 5, 'x');
	cout << s3.c_str() << endl;

	bit::string s4("hello wolrd");
	cout << s4.c_str() << endl;
	s4.insert(5, "abcde");
	cout << s4.c_str() << endl;
}
void test_string4()
{
	bit::string s5("hello world");
	cout << s5.c_str() << endl;
	s5.erase(1);
	cout << s5.c_str() << endl;

}
//void test_string5()
//{
//	bit::string s5("hello world");
//	cout << s5.c_str() << endl;
//	s5.find('r',1);
//	cout << s5.c_str()<< endl;
//
//}
void test_string6()
{
	bit::string ur1 = "ftp:://www.baidu.com/?m=1234567";
	size_t pos1 = ur1.find("://");
	if (pos1 != bit::string::npos)
	{
		bit::string protocol = ur1.substr(0, pos1);
		cout << protocol.c_str() << endl;
	}

	//cout << protocol.c_str() << endl;
	bit::string domain;
	bit::string uri;
	size_t pos2 = ur1.find('/', pos1 + 3);
	if (pos2 != bit::string::npos)
	{
		bit::string domain = ur1.substr(pos1 + 3, pos2 - (pos1 + 3));
		bit::string uri = ur1.substr(pos2 + 1);
		cout << domain.c_str() << endl;
		cout << uri.c_str() << endl;
	}
}
void test_string7()
{

	bit::string s("hello world");
	s.resize(10);
	//cout << s << endl;
	s.resize(13, 'x');
	cout << s.c_str() << endl;
	s.resize(29, 'y');
	cout << s.c_str() << endl;
	//C的字符数组,以\0为终止算长度
	//string不看\0,以size为终止算长度
	std::string ss = "hello world";
	ss += '\0';
	ss += "!!!!!!!!!";
	cout << ss.c_str() << endl;
	cout << ss << endl;
	std::string copy(ss);
	cout << ss << endl;
	cout << copy << endl;

}
void test_string8()
{
	std::string s;
	cin >> s;
	cout << s << endl;
	cin >> s;
	cout << s << endl;
}
void test_string9()
{
	string s1("bb");
	string s2("aaa");
	cout << (s1 < s2) << endl;
}
void test_string10()
{
	bit::string s1("aaaa");
	bit::string s2("aaaa");
	cout << (s1 < s2) << endl;
	cout << (s1 > s2) << endl;
	cout << (s1 == s2) << endl;
	cout << (s1 != s2) << endl;
	cout << (s1 >= s2) << endl;
	cout << (s1 <= s2) << endl;
}
void test_string11()
{
	bit::string s1("hello");
	//bit::string s2(s1);
	s1.append("aaaaaaa");
	cout << "s1:" << s1 << endl;
	//cout << "s2:" << s2 << endl;
	cout << endl;
	bit::string s3("xxxxxxxxxx");
	s1 = s3;
	cout << s1 << endl;
	cout << s3 << endl;
}
int main()
{
	test_string11();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值