模拟实现string类

模拟实现string,包含增删查改等功能。

mystring.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <assert.h>
#include <string.h>
using std::ostream;
using std::istream;
namespace bit
{

	class string

	{

		friend ostream& operator<<(ostream& _cout, const bit::string& s);

		friend istream& operator>>(istream& _cin, bit::string& s);

	public:

		typedef char* iterator;


		string(const char* str = "");

		string(const string& s);

		string& operator=(const string& s);

		~string();



		//

		// iterator

		iterator begin();

		iterator end();



		/

		// modify

		void push_back(char c);

		string& operator+=(char c);

		void append(const char* str);

		string& operator+=(const char* str);

		void clear();

		void swap(string& s);

		const char* c_str()const;



		/

		// capacity

		size_t size()const;

		size_t capacity()const;

		bool empty()const;

		void resize(size_t n, char c = '\0');

		void reserve(size_t n);



		/

		// access

		char& operator[](size_t index);

		const char& operator[](size_t index)const;

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

		size_t find(char c, size_t pos = 0) const;

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

		size_t find(const char* s, size_t pos = 0) const;

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

		string& insert(size_t pos, char c);

		string& insert(size_t pos, const char* str);



		// 删除pos位置上的元素,并返回该元素的下一个位置

		string& erase(size_t pos, size_t len = -1);

	private:

		char* _str;

		size_t _size;

		size_t _capacity;

	};
	/

//relational operators

	bool operator<(const string& s1,const string& s2);

	bool operator<=(const string& s1, const string& s2);

	bool operator>(const string& s1, const string& s2);

	bool operator>=(const string& s1, const string& s2);

	bool operator==(const string& s1, const string& s2);

	bool operator!=(const string& s1, const string& s2);
};

mystring.cpp

#include "mystring.h"
namespace bit
{
	//构造函数
	string::string(const char* str)
		:_size(strlen(str))
		,_capacity(_size)
	{
		_str = new char[_capacity + 1];
		strcpy(_str, str);
	}

	//拷贝构造函数
	string::string(const string& s)
		:_str(nullptr)
		, _size(0)
		, _capacity(_size)
	{
		string tmp(s._str);
		swap(tmp);//第一个参数是省略的this指针
	}

	//重载=函数 与拷贝构造不同的是,这个函数不是构造函数,例如s1 = s2,s1是已经存在的。
	string& string::operator=(const string& s)
	{
		string tmp(s);//这里就可以使用上面的拷贝构造函数
		swap(tmp);
		return *this;
	}

	//析构函数
	string::~string()
	{
		delete[] _str;
		_size = 0;
		_capacity = 0;
	}
	//迭代器
	string::iterator string::begin()
	{
		return _str;
	}

	string::iterator string::end()
	{
		return _str + _size;
	}

	// modify

	void string::push_back(char c)
	{
		insert(_size - 1, c);
	}

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

	void string::append(const char* str)
	{
		insert(_size - 1, str);
	}

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

	void string::clear()
	{
		_str[0] = '\0';
		_size = 0;
	}

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

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

	// capacity

	size_t string::size()const
	{
		return _size;
	}

	size_t string::capacity()const
	{
		return _capacity;
	}

	bool string::empty()const
	{
		return _size == 0;
	}

	void string::resize(size_t n, char c)
	{
		if (n < _size)
		{
			memset(_str, c, n);
		}
		else
		{
			if (n > _capacity)
			{
				reserve(n);
			}
			memset(_str, c, n);
			_size = n;
			_str[_size] = '\0';
		}
	}

	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
			_capacity = n;
		}
	}

	/

// access

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

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



	/

	//relational operators

	bool operator<(const string& s1, const string& s2)
	{
		return strcmp(s1.c_str(), s2.c_str()) < 0;
	}

	bool operator<=(const string& s1, const string& s2)
	{
		return s1 < s2 || s1 == s2;
	}

	bool operator>(const string& s1, const string& s2)
	{
		return !(s1 <= s2);
	}

	bool operator>=(const string& s1, const string& s2)
	{
		return s1 > s2 || s1 == s2;
	}

	bool operator==(const string& s1, const string& s2)
	{
		return strcmp(s1.c_str(), s2.c_str()) == 0;
	}

	bool operator!=(const string& s1, const string& s2)
	{
		return !(s1 == s2);
	}



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

	size_t string::find(char c, size_t pos) const
	{
		assert(pos < _size);
		while (pos < _size)
		{
			if (_str[pos] == c)
			{
				return pos;
			}
			pos++;
		}
		return -1;
	}

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

	size_t string::find(const char* s, size_t pos) const
	{
		assert(pos < _size);
		size_t index = pos, start = pos;
		while (index < _size)
		{
			if (_str[index] == s[0])
			{
				start = index;
				size_t i = 0;
				while (start < _size && i < strlen(s))
				{
					if (_str[start] == s[i])
					{
						start++;
						i++;
					}
					else
					{
						break;
					}
				}
				if (i == strlen(s))
				{
					return index;
				}
			}
			index++;
		}
		return -1;
	}

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

	string& string::insert(size_t pos, char c)
	{
		assert(pos <= _size);
		if (_size == _capacity)
		{
			reserve(_capacity == 0 ? 4 : 2 * _capacity);
		}
		size_t end = _size + 1;//防止下标越界
		while (end > pos + 1)
		{
			_str[end] = _str[end - 1];
			end--;
		}
		_str[end] = c;
		_size++;
		return *this;
	}

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



	// 删除pos位置上的元素,并返回该元素的下一个位置

	string& string::erase(size_t pos, size_t len)
	{
		assert(pos < _size);
		if (len == -1 || len + pos >= _size)
		{
			_str[pos] = '\0';
			_size = pos;
		}
		else
		{
			for (int i = pos + len; i <= _size; i++)
			{
				_str[i - len] = _str[i];
			}
		}
		_size -= len;
		return *this;
	}
	ostream& operator<<(ostream& _cout, const bit::string& s)
	{
		for (size_t i = 0; i < s._size; i++)
		{
			_cout << s[i];
		}
		return _cout;
	}

	istream& operator>>(istream& _cin, bit::string& s)
	{
		s.clear();
		char ch = _cin.get();
		while (ch != ' ' && ch != '\n')
		{
			s += ch;
			ch = _cin.get();
		}
		return _cin;
	}
};

test.cpp

#include "mystring.h"
using namespace bit;
int main()
{
	string s1("hello world");//构造函数
	string s2(s1);//拷贝构造
	s1 += "ab";
	s2 = s1;
	s1.push_back('\0');
	s1.push_back('1');
	int pos = s1.find('\0');
	int ret = s1.find("dls");
	s1.erase(11, 2);
	std::cout << s1;
	return 0;
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值