c++模拟实现string类

//注意,实现string类时,在拷贝,赋值等操作时要深拷贝
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string.h>
#include<algorithm>
#include<assert.h>
using namespace std;
namespace ming {         //命名空间
	class String {      
	public:
		typedef char* iterator;      //迭代器
		typedef const char* const_iterator;      //常量迭代器
	public:
		String(const char* str = "") {    //全缺省的默认构造
			if (nullptr == str)
				str = "";
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}
		String(const String& s)       //拷贝构造
			:_str(nullptr)
			, _capacity(0)
			, _size(0)
		{
			String tmp(s._str);
			Swap(tmp);
		}
		~String() {
			if (_str) {
				delete[] _str;
				_str = nullptr;
			}
		}
		String& operator= (String s) {
			/*if (this != &s) {
				String tmp(s);
				swap(_str, tmp._str);
			}*/
			Swap(s);
			return *this;
		}

		void Swap(String& s) {
			swap(_str, s._str);
			swap(_capacity, s._capacity);
			swap(_size, s._size);
		}
		// iterator
		iterator begin() { return _str; }
		iterator end() { return _str + _size; }
		const_iterator begin() const { return _str; }
		const_iterator end()const { return _str + _size; }
		//修改
		void push_back(char c) {
			if (_size == _capacity) {
				Reserve(_capacity * 2);
			}
			_str[_size++] = c;
			_str[_size] = '\0';
		}
		String& operator+=(char c) {
			push_back(c);
			return *this;
		}
		void Append(const char* str) {
			int size = strlen(str);
			Reserve(_capacity + size);
			for (int i = 0; i < size; ++i) {
				push_back(str[i]);
			}
		}
		//在指定位置插入字符串
		void insert(const char* str, const size_t& pos = 0) {
			int len = strlen(str);
			if (pos <= _size) {
				if (len + _size > _capacity - 1)
					Reserve(len + _size + 1);
				if (pos == _size) {
					int tmp = 0;
					int j = pos;
					while (tmp < len) {
						_str[pos] = str[tmp];
						++tmp;
						++j;
					}
					_str[len + _size] = '\0';
				}
				else {
					int i = _size;
					while (i >= pos) {
						_str[i + len] = _str[i];
						--i;
					}
					int tmp = 0;
					int j = pos;
					while (tmp < len) {
						_str[j] = str[tmp];
						++tmp;
						++j;
					}
				}
			}
			_size += len;
		}
		//在指定位置插入str
		void insert(String str, const size_t& pos = 0) {
			int len = str._size;
			if (pos <= _size) {
				if (len + _size > _capacity - 1)
					Reserve(len + _size + 1);
				if (pos == _size) {
					int tmp = 0;
					int j = pos;
					while (tmp < len) {
						_str[j] = str._str[tmp];
						++tmp;
						++j;
					}
					_str[len + _size] = '\0';
				}
				else {
					int i = _size;
					while (i >= pos) {
						_str[i + len] = _str[i];
						--i;
					}
					int tmp = 0;
					int j = pos;
					while (tmp < len) {
						_str[j] = str._str[tmp];
						++tmp;
						++j;
					}
				}
			}
			_size += len;
		}
		String& operator+=(const char* str) {
			Append(str);
			return *this;
		}
		void Clear() {
			_size = 0;
			_str[0] = '\0';
		}
		const char* C_Str()const {
			return _str;
		}
		bool Empty() {
			return (_size == 0) ? 1 : 0;
		}
		//设定有效元素数
		void Resize(size_t newSize, char c = '\0') {
		//预设有效元素个数小于原个数,不做操作
			if (newSize > _size) {
				if (newSize < _capacity) {
					for (size_t i = _size; i < newSize; ++i) {
						_str[i] = c;
					}
					_size = newSize;
				}
				//若大于容量,增容
				else {
					Reserve(newSize + 1);
					for (size_t i = _size; i < newSize; ++i) {
						_str[i] = c;
					}
					_size = newSize;
				}
			}
			_str[_size] = '\0';
		}
		//设置容量capacity
		void Reserve(size_t newCapacity) {
			//预设容量小于源容量,不做操作
			if (newCapacity > _capacity) {
				char* tmp = new char[newCapacity];
				strcpy(tmp, _str);
				delete[] _str;
				_str = nullptr;
				_str = tmp;
				_capacity = newCapacity;
			}
		}
		char& operator[](size_t index) {
			return _str[index];
		}
		const char& operator[](size_t index)const {
			return _str[index];
		}
		bool operator<(const String& s) {
			int a = s._size;
			int b = _size;
			int min = (b < a) ? b : a;
			int i = 0;
			while (i < min) {
				if (_str[i] >= s[i])
					return false;
				++i;
			}
			return true;
		}
		bool operator<=(const String& s) {
			return operator<(s) || operator==(s);
		}
		bool operator>(const String& s) {
			return !(operator<=(s));
		}
		bool operator>=(const String& s) {
			return !(operator<(s));
		}
		bool operator==(const String& s) {
			int a = s._size;
			int b = _size;
			if (b != a) {
				return false;
			}
			else {
				int i = 0;
				while (i < _size) {
					if (s[i] != _str[i])
						return false;
					++i;
				}
			}
			return true;
		}
		bool operator!=(const String& s) {
			return !(operator==(s));
		}
	private:  //将<<申明为友元,使其能访问到私有成员
		friend ostream& operator<<(ostream& _cout, const ming::String& s);
	private:
		char* _str;   //字符串
		size_t _capacity;   //容量
		size_t _size;   //有效元素数
	};
}
//<<重载
ostream& ming::operator<<(ostream& _cout, const ming::String& s) {
	cout << s._str;
	return _cout;
}
using namespace ming;
//测试
void test1() {
	String  s1("teststring");
	String  s2(s1);
	String s3;
	s3 = s1;
	char* tmp = s1.begin();
	tmp = s3.end();
	s3.push_back('s');
	s3.Append("123");
	s3.Resize(18, 'c');
	s3.Reserve(40);
	s1.insert("kkk", 3);
	s1.insert(s2, 1);
	bool a = (s1 != s2);
	cout << s1 << endl;
	cout << s1[2] << endl;
}
int main() {
	test1();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值