string类的基本实现

在面试中面试官常常会让你写出string类的基本操作,比如:构造函数,析构函数,拷贝构造等等.下面是除此之外的一些操作,希望可以帮助你更好的理解string以便以后的运用:

String& operator=(const String& s);
char* c_str();
char& operator[](int index);
void PushBack(char c);
String operator+(const String& s);
String& operator+=(const String& s);
String& insert(int pos,const char* str);//在指定位置插入字符串
bool operator==(const String& s);



为了读者方便,下面给出完整代码:

#include<iostream>
using namespace std;
#include<cstring>
#include<assert.h>
class String 
{
	friend ostream& operator<<(ostream& os,const String& s);
	friend istream& operator>>(istream& is,String& s);
public:
	String(const char* str=""):_sz(strlen(str))
		                  ,_capacity(strlen(str)+1)
				  ,_str(new char[strlen(str)+1])
        {
		  strcpy(_str,str);
	}
	String(const String& s):_sz(s._sz)
		               ,_capacity(strlen(s._str)+1)
		               ,_str(new char[strlen(_str)+1])

	{
		strcpy(_str,s._str);
	}
	~String()
	{
		if(_str!=NULL)
		{
			delete[] _str;
			_str=NULL;
			_sz=0;
			_capacity=0;

		}
	}
	//String& operator=(const String& s)
	//{
	//	if(this!=&s)
	//	{
	//		delete[] _str;                     //_str存放'\0',先将这块空间释放
	//		_str=new char[strlen(s._str)+1];  //再为_str开辟能存放s._str的足够空间
	//		strcpy(_str,s._str);   
	//	}
	//	return *this;
	//}
	String& operator=(String s)
	{
		std::swap(_str,s._str);
		std::swap(_sz,s._sz);
                std::swap(_capacity,s._capacity);
		return *this;
	}

	char* c_str()
	{
		return _str;
	}
	char& operator[](int index)
	{
		return _str[index];
	}
	void PushBack(char c)
	{
		CheckCapacity(1);
		_str[_sz]=c;
		_sz++;
		_str[_sz]='\0';
	}
	String operator+(const String& s)
	{
		String tmp;
		tmp._str=new char[strlen(_str)+strlen(s._str)+1];
		strcpy(tmp._str,_str);
		strcat(tmp._str,s._str);
		return tmp;
	}
	String& operator+=(const String& s)
	{
		char* tmp=_str;
		_str=new char[strlen(_str)+strlen(s._str)+1];
		if(NULL==tmp)
		{
			exit(EXIT_FAILURE);
		}
		strcpy(_str,tmp);
		strcat(_str,s._str);
		return *this;
	}
	String& insert(int pos,const char* str)//在指定位置插入字符串
	{
		assert(pos>=_sz);          //条件为真继续往下执行
		int len=strlen(str);
		CheckCapacity(_sz+len+1);
		int start=_sz;
		while(start>=pos)
		{
			_str[start+1]=_str[start];
			start--;
		}
		for(int i=0;i<len;i++)
		{
			_str[pos]=str[i];
			pos++;
		}
		return *this;
	}
	bool operator==(const String& s)
	{
		if(strcmp(_str,s._str)==0)
		{
			return true;
		}
		else
			return false;
	}
private:
	void CheckCapacity(int count)
	{
		if(_sz+count>=_capacity)
		{
			int newcapacity=(2*_capacity>_capacity+count)
				?(2*_capacity):(_capacity+count);
			char* tmp=new char[newcapacity];
			if(NULL==tmp)
			{
				exit(EXIT_FAILURE);
			}
			strcpy(tmp,_str);
			delete[] _str;
			_str=tmp;
			_capacity=newcapacity;
		}
	}
private:
	char* _str;
	int _sz;
	int _capacity;
};
ostream& operator<<(ostream& os,const String& s)
{
	os<<s._str<<endl;
	return os;
}
istream& operator>>(istream& is,String& s)
{
	is>>s._str;
	return is;
}
void test1()
{
	String s1("abcdef");
	String s2(s1);
	String s3;
	s3=s1;
	cout<<s1<<endl;
	cout<<s2<<endl;
	cout<<s3<<endl;
}
void test2()
{
	String s1="hello";
	cout<<*(s1.c_str()+1)<<endl;    //取出第二个字符
	cout<<strlen(s1.c_str())<<endl;
	cout<<s1[2]<<endl;
	s1[4]='a';
	cout<<s1<<endl;
}
void test3()
{
	String s1="abcdef";
	s1.PushBack('k');
	cout<<s1<<endl;
}
void test4()
{
	String s1("aacd");
	String s2("mmnp");
	String s3;
	s3=s1+s2;
	s1+=s2;
	cout<<s1<<endl;
	cout<<s3<<endl;
}
void test5()
{
	String s="aaabb";
	s.insert(2,"cd");
	cout<<s.c_str()<<endl;
}
int main()
{
	test5();
	system("pause");
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值