string类的应用

本文详细介绍了C++中自定义string类的实现,包括构造函数、拷贝构造函数、赋值运算符重载、迭代器、容量管理、元素访问、字符串操作等,并通过示例展示了其在实际中的应用,如查找、插入、修改和输出等操作。
摘要由CSDN通过智能技术生成

C++中string类的应用

#include <iostream>
#include<string>
using namespace std;
#include<assert.h> 

namespace bite
{
	class string
	{
	public:
		typedef char* iterator;//将迭代器可以看成指针
		typedef char* reverse_iterator;
		// 构造(无参和单个参数构造合并) 
		string(const char* str = "")
		{
			if (nullptr == str)
				str = "";

			_size = strlen(str);
			_str = new char[_size + 1];//开辟空间
			strcpy(_str, str);
			_capacity = _size;//此处容量没有将\0包含进去 
		}

		//拷贝构造
		string(const string& s)
			: _str(nullptr)
			, _size(0)
			, _capacity(0)
		{
			string strTemp(s._str);
			this->swap(strTemp);
		}

		//string类中包含n个字符ch
		string(size_t n, char ch)
		{
			_size = n;
			_str = new char[n + 1];
			memset(_str, ch, n);
			_str[n] = '\0';
			_capacity = n;
		}

		//赋值运算符重载
		string& operator=(string s)
		{
			this->swap(s);
			return *this;
		}

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

		// 迭代器(相当于C语言中的指针)
		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		//反向迭代器
		reverse_iterator rbegin()
		{
			return end();
		}

		reverse_iterator rend()
		{
			return begin();
		}


		// 容量
		size_t size()const
		{
			return _size;
		}

		size_t capacity()const
		{
			return _capacity;
		}

		bool empty()const
		{
			return 0 == _size;
		}

		void clear()
		{
			_size = 0;
			_str[0] = '\0';
		}

		//将有效字符的个数改成n个,多出的空间用字符c填充
		//void resize (size_t n);
		//void resize(size_t n, char c);
		void resize(size_t newsize, char ch)
		{
			size_t oldsize = size();
			if (newsize > oldsize)
			{
				size_t oldcap = capacity();
				if (newsize > oldcap)
				{
					reserve(newsize);
				}

				memset(_str + _size, ch, newsize - oldsize);
			}

			_size = newsize;
			_str[_size] = '\0';
		}

		void resize(size_t newsize)
		{
			resize(newsize, 0);
		}


		//为字符串预留空间 void reserve (size_t n = 0);
		void reserve(size_t newcapacity)
		{
			size_t oldcapacity = capacity();
			if (newcapacity > oldcapacity)
			{
				char* temp = new char[newcapacity + 1];
				strcpy(temp, _str);//将旧空间的字符串拷贝到新空间
				delete[] _str;//将空间释放
				_str = temp;
				_capacity = newcapacity;
			}
		}

		// 元素访问(返回pos位置的字符,const string类对象调用)
		//char& operator[] (size_t pos);
		//const char& operator[] (size_t pos) const;
		char& operator[](size_t index)
		{
			assert(index < _size);
			return _str[index];
		}

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

		// 修改(在字符串后尾插字符c)void push_back (char c);
		void push_back(char ch)
		{
			if (_size == _capacity)
				reserve(_capacity * 2);

			_str[_size] = ch;
			_size++;
			_str[_size] = '\0';
		}

		//在字符串后追加字符串str
		//string& operator+= (const string& str);
		//string& operator+= (const char* s);
		//string& operator+= (char c);
		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}

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

		string& operator+=(const char* s)
		{
			size_t len = strlen(s);
			char* temp = new char[_size + len + 1];
			strcpy(temp, _str);
			strcat(temp, s);

			_size += len;
			delete[] _str;
			_str = temp;
			_capacity = _size;
			return *this;
		}

		// 特殊操作(返回C格式字符串)const char* c_str() const;
		const char* c_str()const
		{
			return _str;
		}

		//size_t find (const string& str, size_t pos = 0) const;
		//size_t find(const char* s, size_t pos = 0) const;
	    //size_t find(const char* s, size_t pos, size_t n) const;
	    //size_t find(char c, size_t pos = 0) const;
		size_t find(char ch, size_t pos = 0)
		{
			for (size_t i = pos; i < _size; ++i)
			{
				if (_str[i] == ch)
					return i;
			}

			return npos;
		}

		//string::rfind从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置
		//size_t rfind (const string& str, size_t pos = npos) const;
        //size_t rfind(const char* s, size_t pos = npos) const;
		//size_t rfind(const char* s, size_t pos, size_t n) const;
		//size_t rfind(char c, size_t pos = npos) const;
		size_t rfind(char ch, size_t pos = npos)
		{
			pos = pos < _size ? pos : _size - 1;
			for (int i = pos; i >= 0; --i)
			{
				if (_str[i] == ch)
					return i;
			}

			return npos;
		}

		//在str中从pos位置开始,截取n个字符,然后将其返回
		//string substr (size_t pos = 0, size_t len = npos) const;
		string substr(size_t pos = 0, size_t n = npos)
		{
			if (n == npos)
			{
				n = _size;
			}

			if (pos + n >= _size)
			{ 
				n = _size - pos;
			}

			char* temp = new char[n + 1];
			//char * strncpy ( char * destination, const char * source, size_t num );
			strncpy(temp, _str + pos, n);
			temp[n] = '\0';

			string strRet(temp);
			delete[] temp;
			return strRet;
		}

		//调用内部swap
		void swap(string& s)
		{
			std::swap(_str, s._str);
			std::swap(_size, s._size);
			std::swap(_capacity, s._capacity);
		}

		friend ostream& operator<<(ostream& _cout, const string& s)
		{
			_cout << s._str;
			return _cout;
		}

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

	public:
		static size_t npos;
	};

	size_t string::npos = -1;
}

void TestString1()
{
	bite::string s1;
	bite::string s2("hello");
	bite::string s3(s2);
	bite::string s4(10, 'A');

	cout << s2 << endl;

	for (size_t i = 0; i < s3.size(); ++i)
	{
		cout << s3[i];
	}
	cout << endl;

	for (auto e : s4)
		cout << e;
	cout << endl;

	auto it = s4.begin();
	while (it != s4.end())
	{
		cout << *it;
		++it;
	}
	cout << endl;
}

void TestString2()
{
 	bite::string s("hello");
	cout << s.size() << endl;
	cout << s.capacity() << endl;

	s.clear();
	cout << s.size() << endl;
	cout << s.capacity() << endl;

	if (s.empty())
	{
		cout << "ok" << endl;
	}
	else
	{
		cout << "error" << endl;
	}
}

void TestString3()
{
	bite::string s("hello");
	s.reserve(10);
	cout << s.size() << endl;
	cout << s.capacity() << endl;

	s.reserve(20);
	cout << s.size() << endl;
	cout << s.capacity() << endl;

	s.reserve(15);
	cout << s.size() << endl;
	cout << s.capacity() << endl;

	s.reserve(5);
	cout << s.size() << endl;
	cout << s.capacity() << endl;
}

void TestString4()
{
	bite::string s("hello");
	s.resize(10, '!');
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;

	s.resize(20, 'A');
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;

	s.resize(15);
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;

	s.resize(5);
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;
}

void TestString5()
{
	bite::string s1("hello");
	s1[0] = 'H';

	const bite::string s2(s1);
	cout << s2[0] << endl;
}

void TestString6()
{
	bite::string s("hello");
	s.push_back('!');
	cout << s << endl;

	s += "world";
	cout << s << endl;


	bite::string ss("!!!");
	s += ss;
	cout << s << endl;
}

void TestString7()
{
	bite::string s("hellohellohello");
	size_t pos = 0;
	while (true)
	{
		pos = s.find('h', pos);//find从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置
		if (pos == bite::string::npos)
		{
			break;
		}
		cout << pos << endl;
		pos++;
	}
}

void TestString8()
{
	bite::string s("aaabbbbbccc.txt");
	int start = s.find('b');
	int end = s.rfind('b');
	bite::string ret = s.substr(start, end - start + 1);
	cout << ret << endl;

	cout << s.substr(s.rfind('.') + 1) << endl;
}


int main()
{ 
	//TestString1();
	//TestString2(); 
	//TestString3();
	//TestString4();
	/*TestString6();
	_CrtDumpMemoryLeaks();*/
	//TestString7();
	TestString8();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值