C++实现String部分功能!

头文件:
 

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
#include<algorithm>

using namespace std;

namespace zgw
{
	
	class string 
	{

		friend ostream& operator<<(ostream& _cout, string& s1);
	public:


		typedef char* iterator;    //重载迭代器
		char* begin();
		char* end();


		string& operator=(string& s);

		string(const char*str="");
		string(const zgw::string& s1);
		~string();
		char* c_str()const;
		char& operator[](size_t pos);
		size_t size()const;
		void reserve(size_t n);
		void append(const char *s);
		void push_back(const char ch);
		string& operator+=(const char* a);
		string& operator+=(const char ch);
		void insert(size_t pos, const char* s);
		void insert(size_t pos, const char ch);
		void serase(size_t pos=0, size_t len=nopos);
		size_t find(char ch, size_t pos = 0);
		size_t find(const char* str, size_t pos = 0);
		void swap(string& s);
		string substr(size_t pos = 0, size_t len = nopos);
		bool operator<(const string& s) const;
		bool operator>(const string& s) const;
		bool operator<=(const string& s) const;
		bool operator>=(const string& s) const;
		bool operator==(const string& s) const;
		bool operator!=(const string& s) const;
		void clear();
		


		
		//以下是自己搞的
		//void GetCapacity();
		//string& operator+=(const char* a);
		//string& operator+=(const zgw::string&s1);

		//void append(const char*a);
		//void append(const string& s1);
	private:
	
		static size_t nopos;
		char* _str;
		size_t _size;
		size_t _capacity;
	};

	istream& operator>> (istream& is, string& str);
	ostream& operator<< (ostream& os, const string& str);

	ostream& operator<<(ostream& _cout, string& s1);

}


String实现文件:

#include"String.h"



namespace zgw
{
	size_t string::nopos = -1;

	string& string::operator=(string& s)
	{
		char* str = new char[s._capacity + 1];
		strcpy(str, s._str);
		delete[] _str;
		_str = str;
		_size = s._size;
		_capacity = s._capacity;
		return s;
	}
	string::string(const char* str):_size(strlen(str))
	{
		_str = new char[_size + 1];
		strcpy(_str, str);
		_capacity = _size;
	}
	string::string(const string& s1):_size(s1.size())
	{
		_str = new char[_size+1];
		strcpy(_str, s1._str);
		_capacity = _size;
	}

	string::~string()
	{
		delete[] _str;
		_str = nullptr;
		_size = 0;
		_capacity = 0;
	}
	char* string::c_str()const 
	{
		return _str;
	}
	char& string::operator[](size_t pos)
	{
		return _str[pos];
	}
	size_t string::size()const 
	{
		return _size;
	}
	string::iterator string::begin()
	{
		return _str;
	}
	string::iterator string::end()
	{
		return _str + _size;
	}

	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* newnode = new char[n + 1];
			_capacity = n;
			strcpy(newnode, _str);
			delete[]_str;
			_str = newnode;
		}
	}
	void string::append(const char* s)
	{
		size_t sz= strlen(s);
		if ((sz + _size) > _capacity)
		{
			size_t newcapa = sz + _size;
			reserve(newcapa);
		}
		strcpy(_str + _size, s);
		_size += sz;
	}
	void string::push_back(const char ch)
	{
		if (_size == _capacity)
		{
			size_t newcapa = _capacity + 1;
			reserve(newcapa);
		}
		_str[_size++] = ch;
		_str[_size] = 0;
	}

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

	void string::insert(size_t pos, const char* s)
	{
		assert(pos < _size);
		size_t length = strlen(s);
		if ((_size + length) > _capacity)
		{
			reserve(_size + length);
		}
		int end = _size+length;
		while (end > (int)(pos + length-1))
		{
			_str[end] = _str[end - length];
			end--;
		}
		memcpy(_str + pos, s,length);
	}
	void string::insert(size_t pos, const char ch)
	{
		assert(pos < _size);
		if (_size == _capacity)
		{
			size_t newcapa = _capacity + 1;
			reserve(newcapa);
		}
		int end = _size;
		while (end >=(int)pos)
		{
			_str[end + 1] = _str[end];
			end--;
		}
		_str[pos] = ch;
		_size++;
	}


	void string::serase(size_t pos, size_t len)
	{
		assert(pos < _size);
		if (pos + len >= _size)
		{
			_str[pos] = '\0';
			_size = pos;
		}
		else
		{
			strcpy(_str + pos, _str + len);
			_size -= len;
		}
	}
	size_t string::find(char ch, size_t pos)
	{
		for (int i = 0; i < _size; i++)
		{
			if (ch == _str[i])
			return i;
		}
		return nopos;
	}
	size_t string::find(const char* str, size_t pos)
	{
		char* s1 = strstr((_str + pos), str);
		if (s1 == nullptr)
		{
			return nopos;
		}
		return s1 - _str;
	}

	void string::swap(string& s)
	{
		std::swap(_str,s._str);
		std::swap(_capacity, s._capacity);
		std::swap(_size, s._size);
	}
	string string::substr(size_t pos, size_t len)
	{
		if (pos + len > _size)
		{
			string s1(_str + pos);
			return s1;
		}
		string s1;
		s1.reserve(len);
		for (int i = 0; i < len; i++)
		{
			s1[i] = _str[i + pos];
		}
		return s1;
	}

	bool string::operator<(const string& s) const
	{
		return strcmp(_str, s._str)<0;
	}
	bool string::operator>(const string& s) const
	{
		return !(*this <= s);
	}
	bool string::operator<=(const string& s) const
	{
		return (*this) < s || (*this == s);
	}
	bool string::operator>=(const string& s) const
	{
		return !((*this) < s);
	}
	bool string::operator==(const string& s) const
	{
		return strcmp(_str, s._str);
	}
	bool string::operator!=(const string& s) const
	{
		return !(*this == s);
	}
	void string::clear()
	{
		_str[0] = 0;
	}

	//以下是自己搞的

	/*ostream& operator<<(ostream& _cout, string& s1)
	{
		_cout << s1._str ;
		return _cout;
	}
	void string::GetCapacity()
	{
		_capacity = _capacity * 2;
		char* newnode = new char[_capacity * 2];
		strcpy(newnode,_str);
		for (int i = _size; i < _capacity; i++)
		{
			 newnode[i] = 0;
		}
		delete[]_str;
		_str = newnode;
	}

	string& string::operator+=(const char* a)
	{
		while (*a)
		{
			if (_capacity == _size)
			{
				GetCapacity();
			}
			_str[_size++] = *a;
			a++;
		}
		return*this;
	}
	string& string::operator+=(const string&s1)
	{
	
		const char* a = s1.c_str();
		while (*a)
		{
			if (_capacity == _size)
			{
				GetCapacity();
			}
			_str[_size++] = *a;
			a++;
		}
		if (_capacity == _size)
		{
			GetCapacity();
		}
		return *this;
	}*/



	//void append(const char* a);
	//void append(const string& s1);
	istream& operator>> (istream& is, string& str)
	{
		str.clear();
		char ch=is.get();
		while (ch != ' ' && ch != '\n')
		{
			str += ch;
			ch = is.get();
		}
		return is;
	}


	ostream& operator<< (ostream& os,  string& str)
	{
		for (size_t i = 0; i < str.size(); i++)
		{
			cout<<str[i];
		}
		return os;
	}


}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值