C++:string类(2)string模拟实现

 1.string.h

//string.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
#include<string>
using namespace std;
namespace ht
{
	class string
	{
	public:

		typedef char* iterator;
		typedef 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;
		}
		//初始化列表
		//按private的顺序初始化,和初始化列表顺序无关
		//string()
		//	:_str(new char[1]{'\0'})//空间不可以是0
		//	,_size(0)
		//	,_capacity(0)
		//{}变成全缺省
		string(const char*str = "")
		{
			_size = strlen(str);
			_capacity = _size;//_capacity不包含'\0'
			_str = new char[_size+1];
			strcpy(_str,str);
		}
		string(const string& s)
		{
			_str = new char[s._capacity];
			strcpy(_str, s._str);
			_size = s._size;
			_capacity = s._capacity;
		}
		const char* c_str()const
		{
			return _str;
		}
		size_t size()
		{
			return _size;
		}
		size_t capacity()
		{
			return _capacity;
		}
		char& operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}
		~string()
		{
			delete[] _str;
			_str = nullptr;
			_capacity = 0;
			_size = 0;
		}
		string& operator=(const string& s)
		{
			delete[] _str;
			_str = new char[s._capacity+1];
			strcpy(_str, s._str);
			_size = s._size;
			_capacity = s._capacity;
		}

		void clear()
		{
			_str[0] = '\0';
			_size = 0;
		}
		void reserve(size_t n);
		void push_back(char ch);
		string& operator+=(char c);
		void append(const char* str);
		string& operator+=(const char* c);

		void insert(size_t pos, char ch);
		void insert(size_t pos, const char* ch);
		void erase(size_t pos, size_t len);//从Pos考试删除len个字符
	
		size_t find(char ch, size_t pos = 0);
		size_t find(const char* ch, size_t pos = 0);

		string substr(size_t pos, size_t len);

	private:
		char* _str;
		size_t _size;
		size_t _capacity;
	    static const size_t npos;
	};
	 void test_string1();//声明和定义分离
	 void test_string2();


	 bool operator<(const string& s1, const string& s2);
	 bool operator<=(const string& s1, const string& s2);
	 bool operator>(const string& s1, const string& s2);
	 bool operator>=(const string& s1, const string& s2);
	 bool operator==(const string& s1, const string& s2);
	 bool operator!=(const string& s1, const string& s2);

	 ostream& operator<<(ostream& out, const string& s);
	 istream& operator>>(istream& in, string& s);
}

2.string.cpp 

//string.cpp
#include"string.h"
namespace ht
{
    const size_t string::npos = -1;

	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* temp = new char[n];
			strcpy(temp, _str);
			_str = nullptr;
			_str = temp;
			_capacity = n;

		}
	}
	void string::push_back(char ch)
	{
		if (_size == _capacity)
		{
			reserve(_capacity == 0 ? 4 : _capacity * 2);
		}
		_str[_size] = ch;
		_size++;
		_str[_size] = '\0';
	}
	string& string::operator+=(char c)
	{
		push_back(c);
		return *this;
	}

	void string::append(const char* str)
	{
		size_t len = strlen(str);
		if (_size + len >= _capacity)
		{
			reserve(_size + len >= _capacity * 2 ? _size + len : _capacity * 2);
		}
		strcpy(_str + _size, str);
		_size += len;
	}
	string& string::operator+=(const char* c)
	{
		append(c);
		return *this;
	}


	void string::insert(size_t pos, char ch)
	{
		if (_size == _capacity)
		{
			reserve(_capacity == 0 ? 4 : _capacity * 2);
		}
		assert(pos < _size);
		/*int end = _size;
		while (end >= (int)pos)//隐式类型转换-1转成size_t
		{
			_str[end + 1] = _str[end];
			--end;
		}*/
		//挪动数据
		int end = _size+1;
		while (end > pos)
		{
			_str[end] = _str[end-1];
			--end;
		}
		_str[pos] = ch;
		++_size;
	}
	void string::insert(size_t pos, const char* ch)
	{
		assert(pos <= _size);
		int len = strlen(ch);
		if (_size + len > _capacity)
		{
			reserve(_size + len > 2 * _capacity ?  _size + len : 2 * _capacity);
		}
		int end = _size + len;
		while (end - len >= pos)//end-len>pos-1
		{
			_str[end] = _str[end - len];
			end--;
		}
		for (int i = 0; i < len; i++)
		{
			_str[pos+i] = ch[i];
		}
		_size += len;
	}
	void string::erase(size_t pos, size_t len)//从Pos考试删除len个字符
	{
		if (len >= _size - pos)//后面删完了
		{
			_str[pos] = '\0';
			_size = pos;
		}
		else//没删完
		{
			for (int i = pos + len; i < _size; i++)
			{
				_str[i -len ] = _str[i];
			}
			_size -= len;
		}
	}
	size_t string::find(char ch, size_t pos)
	{
		for (size_t i = pos; i < _size; i++)
		{
			if (_str[i] == ch)
			{
				return i;
			}
		}
		return npos;
	}
	size_t string::find(const char* ch, size_t pos)
	{
		assert(pos < _size);
		char* ptr = strstr(_str + pos, ch);
		if (ptr == nullptr)
		{
			return npos;
		}
		else
		{
			return ptr - _str;
		}
		return npos;
	}
	string string::substr(size_t pos, size_t len)
	{
		assert(pos < _size);
		if (len > _size - pos)
		{
			len = _size - pos;
		}
		string sub;
		sub.reserve(len+1);
		for (int i = 0; i < len; i++)
		{
			sub += _str[pos+i];
		}
		return sub;//要调用拷贝构造,要写
	}
	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);
	}

	bool operator>=(const string& s1, const string& s2)
	{
		return !(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);
	}
	ostream& operator<<(ostream& out, const string& s)
	{
		for (auto ch : s)
		{
			out << ch;
		}

		return out;
	}


	istream& operator>>(istream& in, string& s)
	{
		s.clear();

		const int N = 256;
		char buff[N];
		int i = 0;

		char ch;
		//in >> ch;提取不到空格和换行
		ch = in.get();
		while (ch != ' ' && ch != '\n')
		{
			buff[i++] = ch;
			if (i == N - 1)
			{
				buff[i] = '\0';
				s += buff;

				i = 0;
			}

			//in >> ch;
			ch = in.get();
		}

		if (i > 0)
		{
			buff[i] = '\0';
			s += buff;
		}

		return in;
	}
}

3.Test.cpp 

//Test.cpp
#include"string.h"

namespace ht
{
	void test_string1()
	{
		string s1;
		string s2("hello world");
		cout << s1.c_str() << endl;
		cout << s2.c_str() << endl;

		for (size_t i = 0; i < s2.size(); i++)
		{
			cout << s2[i] << " ";
		}
		//加上迭代器,for也好了
		for (auto e : s2)
		{
			cout << e << " ";
		}
		cout << endl;
		//迭代器不是指针
		string::iterator it = s2.begin();
		while (it != s2.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;
	}
	void test_string2()
	{
		string s2("hello");
		s2.push_back('s');
		s2 += '0';
		cout << s2.c_str() << endl;
		s2.append("sss");
		cout << s2.c_str() << endl;
		s2 += "abc";
		cout << s2.c_str() << endl;
		s2.insert(0, '$');
		cout << s2.c_str() << endl;
		s2.insert(1, "###");
		cout << s2.c_str() << endl;
	}
	void test_string3()
	{
		string s3("hello");
		s3.erase(1, 1);
		cout << s3.c_str() << endl;
		s3.erase(1, 10);
		cout << s3.c_str() << endl;

		string s4("hello world");
		size_t pos = s4.find('w', 0);
		cout << pos << endl;
		size_t pos1 = s4.find("world");
		cout << pos1 << endl;
		string str1 = s4.substr(6, 10);
		cout << str1.c_str() << endl;
	
	}
}
int main()
{
	ht::test_string1();
	cout << endl;
	ht::test_string2();
	cout << endl;

	ht::test_string3();
}

4.待继续考究:

流插入和流提取

ostream& operator<<(ostream& out, const string& s);
	 istream& operator>>(istream& in, string& s);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值