C++中string类的模拟实现

标准库中的string类

string类

  1. string是表示字符串的字符串类
  2. 该类的接口与容器的常规接口基本相同,再添加一些专门用来操作string的常规操作。
  3. string在底层实际是:basic_string类模板的别名,

typedef basic_string<char, char_traits, allocator> string;

  1. 不能操作多字节或变长字符的序列。
    在使用string类时必须包括#include头文件以及using namespace std.

string类的常用接口

string类对象的常见构造
函数名称功能说明
string()构造空的string类,即空的字符串
string(const char* s)用C_string类来构造string类对象
string(size_t n,char c)string类对象中包含n个字符c
string(const string& s)拷贝构造函数
void teststring(){
    string s1;               //构造空的string类对象s1;
    string s2("hello world");//用C_string类来构造string类对象s2;
    string s3(5,10);         //s3中包含5个10;
    string s4(s2);           //拷贝构造s3;
}
string类对象的容量操作
函数名称功能说明
size返回字符串的有效字符长度
length返回字符串的有效字符长度
capacity返回空间总大小
empty检测字符串释放为空串,是返回true,否则返回false
clear清空有效字符
reserve为字符串预留空间
resize将有效字符的个数改为n个,多出的空间用字符c填充
void teststring(){
    string s(“hello world!”);
    
    cout<<s.size()<<endl;//10
    cout<<s.lenth()<<endl;//10
    cout<<s.capacity()<<endl;//15
    cout<<s<<endl;
    
    //将s中的字符串清空(将size变为0,不改变底层空间的大小)
    s.clear();
    cout<<s.size()<<endl;//0
    cout<<s.capacity()<<endl;//15
    cout<<s<<endl;//无打印内容
    
    //将s中的有效字符增加到10个,多余的位置用‘a’填充
    s. resize(10,'a');
    cout<<s.size()<<endl;//10
    cout<<s.capacity()<<endl;//15
    cout<<s<<endl;//aaaaaaaaaa
    
    //将s中的有效字符增加到20个,多余位置用‘b’填充
    s.resize(20,'b');  
    cout<<s.size()<<endl;//20
    cout<<s.capacity()<<endl;//31,容量也增加
    cout<<s<<endl;//aaaaaaaaaabbbbbbbbbb
    
    //将s中的有效字符缩减到5个
    s.resize(5);
    cout<<s.size()<<endl;//5
    cout<<s.capacity()<<endl;//31//容量不会缩减
    cout<<s<<endl;//aaaaa
    
    //将空间扩容
    s.reserve(100);//不会改变有效字符的个数
    cout<<s.size()<<endl;//5
    cout<<s.capacity()<<endl;111
   
   //*****(重要)
    s.reserve(50);//reserve参数小于string的底层空间大小时,不会将空间缩小
	cout << s.size() << endl;//5
	cout << s.capacity() << endl;//111 
}

string类的模拟实现

深浅拷贝

class String{
public:
	/*String()
		:_str(new char[1])
	{
	    	*_str = '\0';		
	}

	String(const char* str)
		:_str(new char[strlen(str)+1])
	{
		strcpy(_str, str);
	}*/

	String(const char* str = "")
		:_str(new char[strlen(str)+1])
	{
		strcpy(_str, str);
	
	}

	~String(){
		delete[] _str;
		_str = nullptr;
	}
	//深拷贝拷贝出对象后,它的修改不影响原对象
	String(const String& str)
		:_str(new char[strlen(str._str)+1])
	{
		strcpy(_str, str._str);
	}

	String& operator=(const String& s){
		if (this != &s){
	        delete[] _str;
		    _str = new char[strlen(s._str) + 1];
			strcpy(_str, s._str);
		}
		return *this;
	}

	char& operator[](size_t pos){
		return _str[pos];
	}

	const char* c_str(){
		return _str;
	}

private:
	char* _str;
};

int main(){
	String s1("hello");
	String s2;

	cout << s1.c_str() << endl;//hello
	s1[0] = ' ';
	cout << s1.c_str() << endl;//ello
    cout << s2.c_str() << endl;

	String c1(s1);
	cout <<c1.c_str() << endl;// ello
	c1[0] = 'h';
	cout << c1.c_str() << endl;//hello
	s2 = "world";
	s1 = s2;
	cout << s1.c_str() << endl;
	system("pause");
	return 0;
}

namespace cyy
{
	// string 模拟实现  增删查改
	class string
	{
	public:
		typedef const char* const_iterator;
		typedef char* iterator;

		const_iterator begin() const 
		{
			return _str;
		}

		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		string(const char* str = "")
			:_str(new char[strlen(str) + 1])
		{
			// 已经拷贝'\0'
			strcpy(_str, str);// while (*dst++ = *src++);
			_size = strlen(str);
			_capacity = _size;
		}

		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = _capacity = 0;
		}

		//string copy1(s1)
		string(const string& s)
			:_str(new char[s._size + 1])
			, _size(s._size)
			, _capacity(s._size)
		{
			strcpy(_str, s._str);
		}

		// s1 = s2;
		// s1 = s1;
		string& operator=(const string& s)
		{
			if (this != &s)
			{
				delete[] _str;
				_str = new char[s._size + 1];
				strcpy(_str, s._str);
				_size = s._size;
				_capacity = s._capacity;
			}

			return *this;
		}

		const char* c_str()
		{
			return _str;
		}

		char& operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}

		const char& operator[](size_t pos) const
		{
			assert(pos < _size);
			return _str[pos];
		}

		size_t size()
		{
			return _size;
		}

		size_t capacity()
		{
			return _capacity;
		}

		//reverse
		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n+1];
				strcpy(tmp, _str);
				delete[] _str;
				_str = tmp;
				_capacity = n;
			}
		} 

		void push_back(char ch)
		{
			//if (_size == _capacity)
			//{
			//	// 扩容
			//	reserve(_capacity * 2);
			//}

			//_str[_size] = ch;
			//++_size;
			//_str[_size] = '\0';
			insert(_size, ch);
		}

		// s1.append("11111");
		void append(const char* str)
		{
			//size_t len = strlen(str);
			//if (_size+len > _capacity)
			//{
			//	// 扩容
			//	reserve(_size + len);
			//}

			//strcpy(_str + _size, str);
			//_size += len;

			insert(_size, str);
		}

		//s1 += ch
		const string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}

		const string& operator+=(const char* str)
		{
			append(str);
			return *this;
		}

		const string& operator+=(const string& s)
		{
			append(s._str);
			return *this;
		}

		void insert(size_t pos, char ch)
		{
			assert(pos <= _size);
			if (_size == _capacity)
			{
				reserve(_capacity * 2);
			}

			/*int end = _size;
			while (end >= (int)pos)
			{
				_str[end + 1] = _str[end];
				--end;
			}*/

			size_t end = _size+1;
			while (end >= pos + 1)
			{
				_str[end] = _str[end - 1];
				--end;
			}

			_str[pos] = ch;
			++_size;
		}

		void insert(size_t pos, const char* str)
		{
			assert(pos <= _size);
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}

			/*	int end = _size;
				while (end >= (int)pos)
				{
				_str[end + len] = _str[end];
				--end;
				}*/

			size_t end = _size + len;
			while (end >= pos + len)
			{
				_str[end] = _str[end-len];
				--end;
			}

			while (*str)
			{
				_str[pos++] = *str++;
			}

			_size += len;
		}

		// s1 > s2
		// hello  hello!
		bool operator>(const string& s) const
		{
			const char* str1 = _str;
			const char* str2 = s._str;
			while (*str1 && *str2)
			{
				if (*str1 > *str2)
				{
					return true;
				}
				else if (*str1 < *str2)
				{
					return false;
				}
				else
				{
					++str1;
					++str2;
				}
			}

			if (*str1)
			{
				return true;
			}
			else
			{
				return false;
			}
			/*else if (*str2)
			{
			return false;
			}
			else
			{
			return false;
			}*/
		}

		bool operator==(const string& s) const
		{
			const char* str1 = _str;
			const char* str2 = s._str;
			while (*str1 && *str2)
			{
				if (*str1 != *str2)
				{
					return false;
				}
				else
				{
					++str1;
					++str2;
				}
			}

			if (*str1 || *str2)
			{
				return false;
			}
			else
			{
				return true;
			}
		}

		bool operator>=(const string& s) const
		{
			return *this > s || *this == s;
		}
		// ...

		size_t find(char ch)
		{

		}

		size_t find(const char* str) // strstr ->kmp   
		{

		}

		// operator+
		// operator<<
		// operator>>
		// getline

	private:
		char* _str;
		size_t _size;
		size_t _capacity;
	};
}



void test1()
{
	cyy::string s1("hello");
	cout << s1.c_str() << endl;
	cyy::string copy1(s1);
	cout << copy1.c_str() << endl;

	for (size_t i = 0; i < s1.size(); ++i)
	{
		// s1.operator[](i); -> s1.operator[](&s1, i);
		s1[i] = 'a';
		cout << s1[i] << " ";
	}
	cout << endl;

	cyy::string::iterator it1 = s1.begin();
	while (it1 != s1.end())
	{
		cout << *it1 << " ";
		++it1;
	}
	cout << endl;

	for (auto e : s1)
	{
		cout << e << " ";
	}
	cout << endl;
}

void test2()
{
	cyy::string s1("hello");
	cout << s1.capacity() << endl;

	s1 += "world";
	cout << s1.capacity() << endl;

	s1 += '!';
	cout << s1.capacity() << endl;

	cout << s1.c_str() << endl;

	cyy::string s2("helloworld!");
	s2.insert(5, ' ');
	cout << s2.c_str() << endl;
	s2.insert(0, '$');
	cout << s2.c_str() << endl;
	s2.insert(0, "bit");
	cout << s2.c_str() << endl;
}

int main()
{
    test1();
	test2();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值