c++:实现string类的增删查改等功能

string 是C++中的字符串。 字符串对象是一种特殊类型的容器,专门设计来操作的字符序列。

不像传统的c-strings,只是在数组中的一个字符序列,我们称之为字符数组,而C + +字符串对象属于一个类,这个类有很多内置的特点,在操作方式,更直观,另外还有很多有用的成员函数。

string 的定义为:typedef basic_string<char> string;

代码实现:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
using namespace std;
class String
{
public:
	String(const char* str = "")
		:_str(NULL)
		,_size(0)
		,_capacity(0)
	{
		_size = strlen(str);
		_capacity = _size;
		_str = new char[strlen(str) + 1];
		strcpy(_str,str);
	}
	String(const String& s)
	{
		String tmp(s._str);
		Swap(tmp);
	}
	void Swap(String& s)
	{
		swap(_str,s._str);
		swap(_size,s._size);
		swap(_capacity,s._capacity);
	}
	String& operator=(String s)
	{
		Swap(s);
		return *this;
	}
	~String()
	{
		if (_str)
		{
			delete[] _str;
		}
	}
	char* Getstr()
	{
		return _str;
	}
	size_t Size()
	{
		return _size;
	}
	size_t Capacity()
	{
		return _capacity;
	}
	void Expand(size_t n)
	{
		if (n > _capacity)
		{
			_str = (char*)realloc(_str,n + 1);
			assert(_str);
			_capacity = n;
		}
	}
	void PushBack(char ch)
	{
		if (_size == _capacity)
		{
			Expand(_capacity * 2);
		}
		_str[_size] = ch;
		++_size;
		_str[_size] = '\0';
	}
	void PushBack(const char* str)
	{
		size_t i = 0;
		size_t len = strlen(str);
		if (len + _size > _capacity)
		{
			Expand(len + _size + 1);
		}
		for (i = 0; i <= strlen(str); i++)
		{
			_str[_size] = str[i];
			_size++;
		}
		_str[_size] = '\0';
	}

	void Insert(int pos, char ch)
	{
		if (_size == _capacity)
		{
			Expand(_capacity * 2);
		}
		int end = _size;
		while (end >= pos)
		{
			_str[end + 1] = _str[end];
			end--;
		}
		_str[pos] = ch;
		++_size;
	}
	void Insert(int pos,const char* str)
	{
		size_t len = strlen(str);
		if (len + _size > _capacity)
		{
			Expand(len + _size + 1);
		}
		size_t end = _size;
		while ((int)end >= pos)
		{
			_str[end + len] = _str[end];
			end--;
		}
		while (*str)
		{
			_str[pos++] = *str++;
		}
		_size = _size + len;
	}
	void PopBack()
	{
		assert(_size);
		_str[_size - 1] = _str[_size];
		_size--;
	}
	void Erase(size_t pos, size_t count)//从pos开始删除count个
	{
		if (pos + count >= _size)
		{
			_str[pos] = '\0';
			_size = pos;
		}
		else
		{
			strcpy(_str + pos,_str + pos + count);
			_size -= count;
		}
	}
	void show()
	{
		size_t i = 0;
		for (i = 0;i < _size;i++)
		{
			cout << _str[i];
		}
		cout << endl;
	}
	size_t Find(char ch)const//查找字符
	{
		size_t i = 0;
		for (i = 0;i <_size;i++)
		{
			if (_str[i] = ch)
			{
				return i;
			}
		}
	}
	size_t Find(const char* str)const//查找字符串
	{
		assert(str);
		const char* str1 = _str;
		const char* str2 = str;
		size_t Index1 = 0;
		size_t Index2 = 0;
		size_t Len = strlen(str);
		while (Index1 < _size)
		{
			size_t Index = Index1;
			while (str1[Index] == str2[Index2])
			{
				Index++;
				Index2++;
				if (Index2 == Len)
				{
					return Index1;
				}
			}
			Index2 = 0;
			Index1++;
		}
		return -1;
	}
	char& operator[](size_t pos)
	{
		assert(pos < _size);
		return _str[pos];
	}
	bool operator<(const String& s)const
	{
		size_t i = 0;
		for (i = 0;i < _size && i < s._size;i++)
		{
			if (_str[i] < s._str[i])
			{
				return true;
			}
			else if (_str[i] > s._str[i])
			{
				return false;
			}
		}
		if (i == s._size)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	bool operator==(const String& s)const
	{
		size_t i = 0;
		if (_size != s._size)
		{
			return false;
		}
		for (i = 0;i < _size && i < s._size;i++)
		{
			if (_str[i] != s._str[i])
			{
				return false;
			}
		}
		return true;
	}
	bool operator<=(const String& s)const
	{
		return (*this < s) || (*this == s);
	}
	bool operator>(const String& s)const
	{
		return !(*this <= s);
	}
	bool operator>=(const String& s) const
	{
		return (*this>s) || (*this == s);
	}
	bool operator!=(const String& s)const
	{
		return !(*this == s);
	}
private:
	size_t _size;
	size_t _capacity;
	char* _str;
};

测试:

void test()
{
	String s("hello");
	s.PushBack('w');
	s.PushBack('o');
	s.PushBack('r');
	s.PushBack('l');
	s.PushBack('d');
	s.PushBack('!');
	s.show();
	s.PopBack();
	s.show();
	s.Insert(10,'!');
	s.show();
	s.Erase(5,5);
	s.show();
}
void test1()//传字符串
{
		String s("hello");
		s.PushBack(" world");
		s.show();
		s.Insert(5, " this");
		s.show();
}
int main()
{
	test1();
	system("pause");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值