string类的简单实现

          string类是stl里面个一个基础类,同时也是一个容器。stl在string类里面塞了很多东西,大概有106个成员函数,以及大量的typedef、宏,晦涩难懂。本文只是简单通过面向对象的方法简单实现了一个string,以后会在此基础进行扩充和改进。

//MyString.h
#ifndef MYSTRING_H
#define MYSTRING_H

#include<iostream>
#include<cstddef>
namespace chu
{
	class MyString
	{
		private:
			size_t strLen;
			char* pStr;
		public:
			//default constructor
			MyString()
				:strLen(0),pStr(new char[1])
			{
				*pStr = '\0';
			}
			MyString(const char* p);//construct from a C-style string
			MyString(char ch, int n);//construct from repeated charcter
			MyString(int number);//construct string representation of integer
			MyString(const MyString& rStr);//copy constructor
			~MyString();
			
			int length()const //return length excluding terminating null
			{
				return strLen;
			}
			int find(char ch)const; //find the occurrence of a character
			int find(const char* pString)const;//find the occurrence of C-style string
			int find(const MyString& rString)const;///find the occurrence of a MyString as a sub-string
			void show()const;//output the string

			MyString& operator=(const MyString& str);//overloaded assignment operator
			MyString operator+(const MyString& str);//string concatenation
			MyString& operator+=(const MyString& str);//Append MyString
			MyString& operator+=(const char* rStr);//Append C-Style string
			
			const char& operator[](int index)const;//subscript operator for const objects
			char& operator[](int index);//subscript operator for non-const objects

			/*comparison operator all return type bool*/
			bool operator==(const MyString& rStr)const;
			bool operator!=(const MyString& rStr)const;
			bool operator<(const MyString& rStr)const;
			bool operator>(const MyString& rStr)const;
			
			//return an object corresponding to a substring
			MyString operator()(int index, size_t length)const;

			//overloaded input and output operator
			friend std::istream& operator>>(std::istream& in, MyString& str);
			friend std::ostream& operator<<(std::ostream& out, MyString& str);


	};
}
#endif
//MyString.cpp
#include"mystring.h"
#include<cstring>
#include<cstdlib>
namespace chu
{
	MyString::MyString(const char* p)
	{
		strLen = strlen(p);
		pStr = new char[strLen+1];
		strcpy(pStr, p);
	}
	MyString::MyString(char ch, int n)
	{
		strLen = n;
		pStr = new char[strLen+1];
		for(size_t i = 0; i < strLen; ++i )
		{
			*(pStr+i) = ch;
		}
		*(pStr+strLen) = '\0';
	}
	MyString::MyString(int num)
	{
		char buffer[20];
		size_t count = 0;
		while(num != 0)
		{
			buffer[count] = num % 10+'0';
			num = num/10;
			++count;
		}
		strLen = count;
		std::cout<<"s"<<std::endl;	
		pStr = new char[strLen+1];
		std::cout<<count<<std::endl;	
		for(int i = count-1, j = 0;i >= 0; --i, ++j )
		{
			std::cout<< i<<std::endl;
			*(pStr+j) = buffer[i];
	//	std::cout<<"s"<<std::endl;	
		}
		std::cout<<"s"<<std::endl;	
		*(pStr+strLen) = '\0';
	}
	MyString::MyString(const MyString& rStr)
	{
		strLen = rStr.strLen;
		pStr = new char[strLen+1];
		strcpy(pStr, rStr.pStr);
	}
	MyString::~MyString()
	{
		strLen = -1;
		delete[] pStr;
	}
	int MyString::find(char ch)const
	{
		size_t i;
		for(i = 0; i < strLen; ++i)
		{
			if(*(pStr+i) == ch)
			{
				break;
			}
		}
		if(i != strLen)
		{
			return i;
		}
		else
			return -1;
	}
	int MyString::find(const char* pString)const
	{
		size_t length = strlen(pString);
		if(length >= strLen)
		{
			return -1;
		}
		else
		{
			size_t i, j;
			for(i = 0, j =0; i+length <= strLen && j <= length;)
			{
				while((*(pStr+i) == *(pString+ j))&& j < length)
				{
					++j;
					++i;
				}
				if(j == length)
				{
					break;
				}
				if(j > 0 && j < length)
				{
					j = 0;
					--i;
				}
				++i;

			}
			std::cout<<j<<std::endl;
			if(j == length)
			{
				return i - j;
			}
			else 
			{
				return -1;
			}
		}
	}
	int MyString::find(const MyString& rStr)const
	{
		return find(rStr.pStr);
	}
	void MyString::show()const
	{
		if(strLen)
		{
			std::cout<<std::endl<<pStr;
		}
		else
		{
			std::cout<<std::endl<<"this string is empty";
		}
	}
	MyString& MyString::operator=(const MyString& str)
	{
		if(this == &str)
		{
			return *this;
		}
		else
		{
			delete[] pStr;
			strLen = str.strLen;
			pStr = new char[strLen+1];
			strcpy(pStr, str.pStr);
			return *this;
		}
	}
	MyString& MyString::operator+=(const char* str)
	{
		size_t newlen = strLen + strlen(str);
		char* newStr = new char[newlen + 1];
		strcpy(newStr, pStr);
		strcpy(newStr+strLen, str);
		strLen = newlen;
		delete[] pStr;
		pStr = newStr;
		return *this;
	}
	MyString MyString::operator+(const MyString& str)
	{  
		return *this += str;
	}
	MyString& MyString::operator+=(const MyString& str)
	{
		return *this += str.pStr;
	}

	const char& MyString::operator[](int index)const
	{
		if(strLen == 0)
		{
			std::cout<<"\nstring is empty"<<std::endl;
			exit(1);
		}
		else if(strLen < index||index < 0)
		{
			std::cout<<"\nout of range index in subsrcipt opeattion"<<std::endl;
			exit(1);
		}
		else
		{
			return pStr[index];
		}
	}
	char& MyString::operator[](int index)
	{
		if(strLen == 0)
		{
			std::cout<<"\nstring is empty"<<std::endl;
			exit(1);
		}
		else if(strLen < index||index < 0)
		{
			std::cout<<"\nout of range index in subsrcipt opeattion"<<std::endl;
			exit(1);
		}
		else
		{
			return pStr[index];
		}
	}
	bool MyString::operator==(const MyString& rStr)const
	{
		return strcmp(pStr, rStr.pStr) == 0;
	}
	
	bool MyString::operator!=(const MyString& rStr)const
	{
		return strcmp(pStr, rStr.pStr) != 0;
	}
	bool MyString::operator<(const MyString& rStr)const
	{
		return strcmp(pStr, rStr.pStr) < 0;
	}
	bool MyString::operator>(const MyString& rStr)const
	{
		return strcmp(pStr, rStr.pStr) > 0;
	}
	MyString MyString::operator()(int index, size_t length)const
	{
		if(index < 0 || index+length > strLen || length < 0)
		{
			std::cout<<"out of range"<<std::endl;
			exit(1);
		}
		else
		{
			char* newstr = new char[length+1];
			for(int i = 0; i < length; ++i)
			{
				newstr[i] = pStr[i+index];
			}
			newstr[length] = '\0';
			MyString newStr(newstr);
			delete newstr;
			return newStr;
		}
	}
	std::istream& operator>>(std::istream& in, MyString& str)
	{
		int i = 0;
		char ch;
		char temp[1024];
		while((ch=in.get())!='\n')
		{
			temp[i] = ch;
			++i;
		}
		temp[i] = '\0';
		if(!str.pStr)
			delete[] str.pStr;
		str.pStr = new char[i+1];
		strcpy(str.pStr,temp);
		str.strLen = i;
	}
	std::ostream& operator<<(std::ostream& out, MyString& str)
	{
		if(!str.strLen)
		{
			std::cout<<"string is empty";
		}
		else
		{
			out<<str.pStr;
		}
	}
} 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值