C++String模拟实现

实际上string没什么可讲,主要是对string函数的运用与理解,与其写库函数如何用,不如直接去看c++库函数来得好。

以下是自己实现string功能函数。但没对string库中的全部函数进行实现,而是实现主要使用的。

.cpp内是用来测试函数功能是否正确。

.h内是用来实现string

.CPP

#include"String.h"

int main()
{
	moxuan::string s1;
	cout << s1.c_str() << endl;
	moxuan::string s2("abcd");
	cout << s2.c_str() << endl;
	//s1 = s2;
	cout << s1.c_str() << endl;
	cout << s2.c_str() << endl;

	s2.push_back('a');
	s2.append("11223344");
	cout << s2.c_str() << endl;
	//s1.push_back('a');
	s1.append("aabbccdd");
	cout << s1.c_str() << endl;
	s1.resize(10, 'a');
	cout << s1.c_str() << endl;
	s2.resize(20, 'v');
	cout << s2.c_str() << endl;
	s1 += 'a';
	s1.push_back('a');
	cout << s1.c_str() << endl;
	s2 += "99887766";
	cout << s2.c_str() << endl;

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

	moxuan::string::const_iterator cit = s2.begin();
	while (cit != s2.end())
	{
		cout << *cit;
		*cit++;
	}
	cout << endl;
	s2.inster(100, 'a');
	for (auto ch:s2)
	{
		cout << ch;
	}
	cout << endl;
	s2.inster(0, 'p');
	s2.inster(24, 'G');
	for (auto ch : s2)
	{
		cout << ch;
	}
	cout << endl;
	s2.inster(100, "ooooooo");
	s2.inster(0, "kkkkkk");
	s2.inster(12, "yyyyyyy");
	for (auto ch : s2)
	{
		cout << ch;
	}
	cout << endl;
	s2.erase(0, 5);
	for (auto ch : s2)
	{
		cout << ch;
	}
	cout << endl;
	cout << s2 << endl;
	moxuan::string s3;
	cin >> s3;
	cout << s3;
	return 0;
}

.h

#define CRT_SECURE_NO_WARNINGS
#pragma once
#include
#include
#include
using namespace std;

namespace moxuan
{
	class string
	{
	public:

		typedef char* iterator;
		typedef const char* const_iterator;

		iterator& begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		const_iterator begin()const
		{
			return _str;
		}

		const_iterator end()const
		{
			return _str + _size;
		}

		//s("abc")
		string(const char* s = "")
			:_size(strlen(s))
			, _capacity(_size)
		{
			_str = new char[_capacity + 1];
			strcpy(_str, s);
		}

		//s(s1)
		string(const string& s)
			:_size(s._size)
			, _capacity(s._capacity)
		{
			_str = new char[_capacity + 1];
			strcpy(_str, s._str);
		}
		//s=s1
		string& operator=(const string& s)
		{
			if (this != &s)
			{
				_size = s._size;
				_capacity = s._capacity;
				char* tmp = new char[_capacity + 1];
				delete[]_str;
				strcpy(tmp, s._str);
				_str = tmp;
			}
			return *this;
		}

		~string()
		{
			if (_str)
			{
				delete[]_str;
				_str = nullptr;
				_size = _capacity = 0;
			}
		}

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

		char& operator[](size_t n)
		{
			assert(n <= _capacity);

			return _str[n];
		}

		size_t size()const
		{
			return _size;
		}

		size_t capacity()const
		{
			return _capacity;
		}

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

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

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

				_capacity = n;
			}
		}

		void push_back(const char ch)
		{
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : _capacity * 2 + 1);
			}
			_str[_size] = ch;
			_str[++_size] = '\0';
		}

		void append(const char* s)
		{
			size_t len = _size + strlen(s);
			if (len > _capacity)
			{
				reserve(len);
			}
			strcpy(_str+_size, s);
			_size = len;
		}

		void resize(size_t  n, const char ch)
		{
			if (n < _size)
			{
				_size = n;
				_str[n] = '\0';
			}
			else
			{
				if (n > _capacity)
				{
					reserve(_capacity * 2);
				}

				for (size_t i = _size; i = _size)
			{
				_str[_size] = ch;
			}
			else
			{
				size_t end = _size+1;
				while (pos < end)
				{
					_str[end] = _str[end-1];
					--end;
				}
				_str[pos] = ch;
			}
			++_size;
			_str[_size] = '\0';
			return *this;
		}

		string& inster(size_t pos,const char* str)
		{
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + strlen(str));
			}

			if (pos >= _size)
			{
				this->append(str);
			}
			else
			{
				size_t end = _size + len+1;
				while (pos < end)
				{
					_str[end] = _str[end-len];
					--end;
				}
				strncpy(_str+pos,str,len);
				_size += len;
				++_size;
				_str[_size] = '\0';
			}

			return *this;
		}

		void erase(size_t pos, size_t len=npos)
		{
			assert(pos < _size);

			if (pos == npos || pos + len > _size)
			{
				_str[pos] = '\0';
				_size = pos;
			}
			else
			{
				size_t end = pos + len;
				while (end <= _size)
				{
					_str[end - len] = _str[end];
					++end;
				}
				_size -= len;
				_str[_size] = '\0';
			}
		}

		size_t find(char ch,size_t pos = 0)
		{
			for (; pos <= _size; pos++)
			{
				if (_str[pos] == ch)
				{
					return pos;
				}
			}
		}

		size_t find(char* str, size_t pos = 0)
		{
			const char* p = strstr(_str + pos, str);
			if (p == nullptr)
			{
				return npos;
			}
			else
			{
				return p - _str;
			}
		}

	private:
		char* _str;
		size_t _size;
		size_t _capacity;

	 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)
	{
        //这里创建数组为了防止单一的实现,重复调用。通过提取放入数组,结束再赋值。提高效率
		char ch;
		ch = in.get();
		char buff[128] = { '\0' };
		size_t i = 0;
		while (ch != ' ' && ch != '\n')
		{
			buff[i++] = ch;
			if (i == 127)
			{
				s += buff;
				memset(buff, '\0', 128);
				i = 0;
			}
			ch = in.get();
		}
		s += buff;
		return in;
	}

	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 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 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);
	}
};

你可能感兴趣的:(C++,c++)