string的实现

namespace bing {
	class String {
	public:
		template<class T>
		void swap(T& a1, T& a2) {
			T tmp = a1;
			a1 = a2;
			a2 = tmp;
		}
		typedef char* iterator;
		friend ostream& operator<<(ostream& _cout, const bing::String& s);
		friend istream& operator>>(istream& _cin, bing::String& s);
		
		/
		//add
		// 返回c在string中第一次出现的位置
		size_t find(char c, size_t pos = 0) const {
			if (pos < _size) {
				for (int i = pos; i < _size; i++) {
					if (_str[i] == c) {
						return i;
					}
				}
			}
			return -1;
		}
		// 返回子串s在string中第一次出现的位置
		size_t find(const char* s, size_t pos = 0) const {
			assert(s);
			assert(pos < _size);
			const char* str = _str + pos;
			while (*str) {
				const char* cur = str;
				const char* match = s;
				while (*match && *match == *cur) {
					cur++;
					match++;
				}
				if (*match == '\0') {
					return str - _str;
				}
				else {
					str++;
				}
			}
			return -1;
		}
		// 在pos位置上插入字符c/字符串str,并返回该字符的位置
		String& insert(size_t pos, char c) {
			assert(pos <= _size);
			if (_size == _capcity) {
				reserve(_capcity * 2);
			}
			for (int i = _size; i >= (int)pos; i--) {
				_str[i + 1] = _str[i];
			}
			_str[pos] = c;
			_size++;
			return *this;
		}
		String& insert(size_t pos, const char* str) {
			size_t len = strlen(str);
			if (_size + len > _capcity) {
				reserve(_capcity * 2);
			}
			while (*str != '\0') {
				this->insert(pos, *str);
				str++;
				pos++;
			}
			return *this;
		}
		/
		// access
		char& operator[](size_t index) {
			assert(index < _size);
			return _str[index];
		}
		const char& operator[](size_t index)const {
			assert(index < _size);
			return _str[index];
		}
		/
		//relational operators
		bool operator<(const String& s) {
			int res = strcmp(_str, s._str);
			if (res < 0) {
				return true;
			}
			return false;
		}
		bool operator<=(const String& s) {
			return !(*this > s);
		}
		bool operator>(const String& s) {
			int res = strcmp(_str, s._str);
			if (res > 0) {
				return true;
			}
			return false;
		}
		bool operator>=(const String& s) {
			return !(*this < s);
		}
		bool operator==(const String& s) {
			int res = strcmp(_str, s._str);
			if (res == 0) {
				return true;
			}
			return false;
		}
		bool operator!=(const String& s) {
			return !(*this == s);
		}
		//
		// iterator
		iterator begin() {
			return _str;
		}
		iterator end() {
			return _str + _size;
		}
		/
		// capacity
		size_t size()const {
			return _size;
		}
		size_t capacity()const {
			return _capcity;
		}
		bool empty()const {
			return _size == 0;
		}
		void resize(size_t newSize, char c = '\0') {
			if (newSize > _size) {
				if (newSize > _capcity) {
					reserve(newSize);
				}
				memset(_str + _size, c, newSize - _size);
			}
			_size = newSize;
			_str[_size] = '\0';
		}
		void reserve(size_t newCapacity) {
			if (newCapacity > _capcity) {
				char* str = new char[newCapacity + 1];
				strcpy(str, _str);

				delete[] _str;
				_str = str;
				_capcity = newCapacity;
			}
		}			
		/
		// modify
		void PushBack(char c) {
			if (_size == _capcity) {
				reserve(_capcity * 2);
			}
			_str[_size] = c;
			_size++;
			_str[_size] = '\0';
		}
		String& operator+=(char c) {
			PushBack(c);
			return *this;
		}
		void Append(const char* str) {
			while (*str != '\0') {
				this->PushBack(*str);
				str++;
			}
		}
		String& operator+=(const char* str) {
			Append(str);
			return *this;
		}
		void clear() {
			_size = 0;
			_str[_size] = '\0';
		}
		void swap(String& s) {
			swap(_str, s._str);
			swap(_size, s._size);
			swap(_capcity, s._capcity);
		}
		const char* c_str() const {
			return _str;
		}
		/
		// 删除pos位置上的元素,并返回该元素的下一个位置
		String& erase(size_t pos, size_t len) {
			assert(pos < _size);
			if (pos + len >= _size) {
				_str[pos] = '\0';
				_size = pos;
			}
			else {
				strcpy(_str + pos, _str + pos + len);
				_size -= len;
			}
			return *this;
		}
		/
		// create
		//无参构造函数
		String()
			: _str(new char[16])
			, _capcity(0)
			, _size(0)
		{
			_str[_size] = '\0';
			_capcity = 15;
		}
		//带参构造函数
		String(const char* str)
		{
			_size = strlen(str);
			_str = new char[_size + 1];
			_capcity = _size;
			strcpy(_str, str);
		}
		//拷贝构造
		String(const String& str)
			:_str(new char[strlen(str._str) + 1])
			, _size(str._size)
			, _capcity(str._capcity)
		{
			strcpy(_str, str._str);

		}
		//赋值运算符重载
		String& operator=(const String s) {
			if (this != &s) {
				//开空间
				char* tmp = new char[s._capcity + 1];
				//内容拷贝
				strcpy(tmp, s._str);
				//释放原有空间
				delete[] _str;

				_str = tmp;
				_size = s._size;
				_capcity = s._capcity;
			}
			return *this;
		}
		//析构函数
		~String() {
			if (_str) {
				delete[] _str;
				_str = nullptr;
			}
		}
	private:
		char* _str;
		size_t _size;
		size_t _capcity;
	};
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值