【STL】之String類的模擬實現

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<Windows.h>
#include<iostream>	
#include<string>
using namespace std;

//string實現
namespace myself
{
	class String
	{
	public:
		typedef char* Iterator;   //迭代器(類似指針)
	public:
		String(const char* str = "")   //構造函數
		{
			if (str == nullptr)
				str = "";
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}

		String(const String& s)   //拷貝構造
			:_str(new char[s._capacity + 1])
			, _size(s._size)
			, _capacity(s._capacity)
		{
			strcpy(_str, s._str);
		}

		String& operator=(const String& s)    //賦值運算符重載
		{
			if (this != &s)
			{
				char* pstr = new char[s._capacity + 1];
				strcpy(pstr, s._str);
				delete[] _str;
				_str = pstr;
				_size = s._size;
				_capacity = s._capacity;
			}
			return *this;
		}

		~String()    //析構函數
		{
			if (_str)
			{
				delete[] _str;
				_str = nullptr;
				_size = 0;
				_capacity = 0;
			}
		}

		Iterator Begin()
		{
			return _str;
		}
		Iterator End()
		{
			return _str + _size;
		}
		void Reserve(size_t newCapacity)  //增容(爲字符串預留空間)
		{
			if (_capacity < newCapacity)   //如果新容量大於舊容量,則開闢新空間
			{
				char* newstr = new char[newCapacity + 1];
				strcpy(newstr,_str);
				delete[] _str;
				_str = newstr;
				_capacity = newCapacity;
			}

		}
		void Resize(size_t newSize, char c = char())// 將有效字符的個數該成newSize個,多出的空間用字符c填充
		{
			if (newSize > _capacity)
			{
				Reserve(newSize);
				memset(_str + _size, c, newSize - _size);  //將原字符串末尾開始到新字符串大小結束這之間用c填充
			}
			_size = newSize;
			_str[newSize] = '\0';
		}
		void PushBack(char c)    //在字符串後尾插c字符
		{
			if (_size == _capacity)
				Reserve(_capacity * 2 + 2);
			_str[_size++] = c;
			_str[_size] = '\0';
		}
		void append(const char* s)  //在字符串後面追加一個字符串
		{
			size_t n = strlen(s);
			size_t i = 0;
			for (i = 0; i < n; i++)
			{
				PushBack(*s);
				s++;
			}
			/*
			size_t len = strlen(s);
			if(len>_capacity-_size)
				Reserve(2 * _capacity+len);
			strcat(_str,str);   //strcat將'\0'直接連接上去了
			_size += len;
			*/
		}
		String& operator+=(const char* s)     //在字符串後面追加c個數字符串
		{
			size_t sz = strlen(s);
			if (_size + sz > _capacity)
				Reserve(_capacity * 2 + sz);
			for (size_t i = 0; i < sz; i++)
			{
				PushBack(*s);
				s++;
			}
			return *this;
		}

		char& operator[](size_t index)      //返回index位置的字符,非const string類對象調用
		{
			return _str[index];
		}
		const char& operator[](size_t index)const  //返回index位置的字符,const string類對象調用
		{
			return _str[index];
		}

		size_t Size()const
		{
			return _size;
		}
		size_t Capacity()const
		{
			return _capacity;
		}
		bool empty()const  //判空
		{
			return 0 == _size;
		}
		int Find(char c, size_t pos)    //正序查找
		{
			for (size_t i = pos; i < _size; i++)
			{
				if (c == _str[i])
					return i;
			}
			return npos;
		}
		int rFind(char c, size_t pos)    //倒序查找
		{
			for (int i = _size - 1 - pos; i >= 0; --i)
			{
				if (c == _str[i])
					return i;
			}
			return npos;
		}
		const char* C_Str()const  //以C打印
		{
			return _str;
		}
		void Clear()   //清空
		{
			_size = 0;
			_str[_size] = '\0';
		}
		void Swap(String& s)      //交換
		{
			swap(_str, s._str);
			swap(_size, s._size);
			swap(_capacity, s._capacity);
		}
	private:  
		friend ostream& operator<<(ostream& _cout, const myself::String& s);
	private:
		char* _str;
		size_t _size;
		size_t _capacity;
		const static int npos;
	};
}

ostream& myself::operator<<(ostream& _cout, const myself::String& s) 
{ 
	cout << s._str;  
    return _cout; 
}




-------------------------------測試函數-----------------------------------------
void Test1()
{
	myself::String s1;
	myself::String s2("hello world");	
	myself::String s3(s2);

	s1 = s3;
	cout << s1 << " " << s2 << " " << s3 << " " << endl;

}
void Test2()
{
	myself::String s1("hello");
	s1.PushBack('w');
	char* str1 = "or";
	s1.append(str1);
	char* str2 = "ld";
	s1 += str2;
	cout << s1 << endl;   
	cout << s1.Size() << endl; 
	cout << s1.Capacity()<< endl;

}
void Test3()
{
	myself::String s1("hello");
	auto it = s1.Begin();// 利用迭代器打印string中的元素
	while (it != s1.End())
	{
		cout << *it++ << " ";
	}
	cout << endl;

	myself::String s2("fine");
	s1.Swap(s2);
	cout << s1 << " " << s2 << endl;
}
void Test4()
{
	myself::String s1("hello");
	cout << s1 << endl;
	cout << s1.Size() << endl;
	cout << s1.Capacity() << endl;

	s1.Resize(10, 'd');
	cout << s1 << endl;
	cout << s1.Size() << endl;
	cout << s1.Capacity() << endl;

	s1.Resize(20);
	cout << s1 << endl;
	cout << s1.Size() << endl;
	cout << s1.Capacity() << endl;

	s1.Resize(6);
	cout << s1 << endl;
	cout << s1.Size() << endl;
	cout << s1.Capacity() << endl;

	s1.Reserve(30);
	cout << s1 << endl;
	cout << s1.Size() << endl;
	cout << s1.Capacity() << endl;

}
int main()
{
	//Test1();
	//Test2();
	//Test3();
	Test4();
	system("pause");
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章