C++初阶——简单实现string

简单实现std::string类及一些常用的成员函数。

迭代器,使用指针简单代替。

目录

1、String.h

2、String.cpp

3、Test.cpp


1、String.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <assert.h>

using namespace std;

namespace Lzc
{
	class string
	{
	private:
		char* _str = nullptr;
		size_t _size = 0;
		size_t _capacity = 0;
		static const size_t npos; // 声明,static ,共享,封装
	public:
		typedef char* iterator ;
		typedef const char* const_iterator;

		iterator begin()
		{
			return &_str[0];
		}

		iterator end()
		{
			return &_str[_size];
		}

		const_iterator begin() const
		{
			return &_str[0];
		}

		const_iterator end() const
		{
			return &_str[_size];
		}

		// 默认构造
		string(const char* str = "")// 空字符串应该是'\0'
		{
			_size = strlen(str);
			_capacity = _size; // _capacity 不包括'\0','\0'另外开空间
			_str = new char[_size + 1];
			strcpy(_str, str);
		}

		void swap(string& tmp)
		{
			std::swap(_str, tmp._str);
			std::swap(_size, tmp._size);
			std::swap(_capacity, tmp._capacity);
		}

		// 拷贝构造
		string(const string& str)
		{
			string tmp(str._str);
			swap(tmp);
		}

		//赋值重载
		string& operator=(const string& str)
		{
			string tmp(str);// 拷贝构造
			swap(tmp);
			return *this;
		}

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

		const char* c_str() const
		{
			return _str;
		}

		size_t size() const
		{
			return _size;
		}

		void resize(size_t n)
		{
			if(n<_size)
			_size = n;
			else
			{
				reserve(n);
				for (size_t i = _size; i < n; i++)
				{
					_str[i] = ' ';
					++_size;
				}
			}
		}

		size_t capacity() const
		{
			return _capacity;
		}

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

		bool empty() const
		{
			return _size == 0 ? true : false;
		}

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

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

		void reserve(size_t n); // n>_capacity 扩容,其他情况,不执行
		void push_back(char ch);
		void append(const char* str);
		string& operator+=(char ch);
		string& operator+=(const char* str);

		void insert(size_t pos, char ch);
		void insert(size_t pos, const char* str);

		void erase(size_t pos, size_t len = npos);

		size_t find(char ch, size_t pos = 0);
		size_t find(const char* str, size_t pos = 0);
		string substr(size_t pos = 0, size_t len = npos);
	};

	// 类中函数,第一个参数必须是this指针
	bool operator<(const string& s1, const string& s2);
	bool operator<=(const string& s1, const string& s2);
	bool operator>(const string& s1, const string& s2);
	bool operator>=(const string& s1, const string& s2);
	bool operator==(const string& s1, const string& s2);
	bool operator!=(const string& s1, const string& s2);

	ostream& operator<<(ostream& out, const string& s);
	istream& operator>>(istream& in, string& s);
}

2、String.cpp

#include "String.h"

namespace Lzc
{
	const size_t string::npos = -1;
	void string::reserve(size_t n) // n不包括'\0',n>_capacity 扩容,其他情况,不执行
	{
		if (n > _capacity)
		{
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
			_capacity = n; // reserve 不改变 _size
		}
	}

	void string::push_back(char ch)
	{
		if (_size == _capacity)
			reserve(_capacity == 0 ? 4 : 2 * _capacity);
		_str[_size++] = ch;
		_str[_size] = '\0';
	}

	void string::append(const char* str)
	{
		size_t len = strlen(str);
		if (_size + len > _capacity)
			reserve(_size + len > 2 * _capacity ? _size + len : 2 * _capacity);
		strcpy(_str + _size, str);
		_size += len;
	}

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

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

	void string::insert(size_t pos, char ch)
	{
		assert(pos <= _size);
		if (_size == _capacity)
			reserve(_capacity == 0 ? 4 : 2 * _capacity);
		for (size_t i = _size + 1; i >= pos + 1; i--)
		{
			_str[i] = _str[i - 1];
		}
		_str[pos] = ch;
		_size++;
	}

	void string::insert(size_t pos, const char* str)
	{
		assert(pos <= _size);
		size_t len = strlen(str);
		if (_size + len > _capacity)
			reserve(_size + len > 2 * _capacity ? _size + len : 2 * _capacity);
		for (size_t i = _size + len; i >= pos+len; i--)
		{
			_str[i] = _str[i - len];
		}
		for (size_t i = 0; i < len; i++)
		{
			_str[pos + i] = str[i];
		}
		_size += len;
	}
	
	void string::erase(size_t pos, size_t len)
	{
		assert(pos < _size);
		if (len >= _size - pos)
		{
			_str[pos] = '\0';
			_size = pos;
		}
		else
		{
			for (size_t i = pos; i <= _size - len; i++)
			{
				_str[i] = _str[i + len];
			}
			_size -= len;
		}
	}

	size_t string::find(char ch, size_t pos)
	{
		assert(pos < _size);
		for (size_t i = pos; i < _size; i++)
		{
			if (_str[i] == ch)
				return i;
		}
		return npos;
	}

	size_t string::find(const char* str, size_t pos)
	{
		assert(pos < _size);
		for (size_t i = pos; i < _size; i++)
		{
			size_t j = 0;
			size_t x = i;
			while (_str[x] == str[j] && j < strlen(str))
			{
				x++;
				j++;
			}
			if (j == strlen(str))
				return i;
		}
		return npos;
	}

	string string::substr(size_t pos, size_t len)
	{
		assert(pos < _size);
		if (len > _size - pos)
			len = _size - pos;
		string substr;
		substr.reserve(len);
		for (size_t i = 0; i < len; i++)
		{
			substr += _str[pos + i];
		}
		return substr;
	}

	bool operator<(const string& s1, const string& s2)
	{
		return strcmp(s1.c_str(), s2.c_str()) < 0;
	}
	bool operator<=(const string& s1, const string& s2)
	{
		return s1 < s2 || s1 == s2;
	}
	bool operator>(const string& s1, const string& s2)
	{
		return !(s1 <= s2);
	}
	bool operator>=(const string& s1, const string& s2)
	{
		return !(s1 < s2);
	}
	bool operator==(const string& s1, const string& s2)
	{
		return strcmp(s1.c_str(), s2.c_str()) == 0;
	}
	bool operator!=(const string& s1, const string& s2)
	{
		return !(s1 == s2);
	}

	ostream& operator<<(ostream& out, const string& s)
	{
		for (auto ch : s)
		{
			out << ch;
		}
		return out;
	}

	istream& operator>>(istream& in, string& s)
	{
		char ch = in.get(); // 获取每一个字符,不会跳
		// in >> ch; 会自动跳过,空格、换行符

		const size_t N = 256; // 一个缓存,减少s的频繁扩容
		char tmp[N];
		size_t i = 0;

		while (ch != ' ' && ch != '\n')
		{
			tmp[i] = ch;
			i++;
			if (i == N - 1)
			{
				tmp[i] = '\0';
				s += tmp;
				i = 0;
			}
			ch = in.get();
		}
		if (i > 0)
		{
			tmp[i] = '\0';
			s += tmp;
		}
		return in;
	}
}

3、Test.cpp

#include "String.h"

namespace Lzc
{
	void test_string1()
	{
		string s1;
		string s2("222");
		cout << s2.c_str() << endl;
		string s3(s2);
		cout << s3.c_str() << endl;
		string s4;
		s4 = s2;
		cout << s4.c_str() << endl;
	}
	void test_string2()
	{
		string s1("aaa");

		// 下标 + [ ]
		for (size_t i = 0; i < s1.size(); i++) // aaa
		{
			cout << s1[i];
		}
		cout << endl;

		// 迭代器
		// string::iterator it = s1.begin();
		auto it = s1.begin();
		while (it != s1.end()) // ccc
		{
			*it += 2;
			cout << *it;
			++it;
		}
		cout << endl;

		// 自动迭代,底层也是迭代器
		for (auto s : s1) // eee,注意:是将s1中的数据,赋值给s,值传递
		{                 // for (auto& s : s1),才会改变s1
			s += 2;       // for (const auto& s : s1),减少拷贝,s1不能修改
			cout << s;
		}
		cout << endl;
	}

	void test_string3()
	{
		string s1,s2;
		cout << s1.capacity() << endl;
		s1.reserve(6);
		cout << s1.capacity() << endl;
		s1.reserve(4);
		cout << s1.capacity() << endl;

		cout << endl;

		cout << s2.size() << endl;
		cout << s2.capacity() << endl;
		s2.resize(6);
		cout << s2.size() << endl;
		cout << s2.capacity() << endl;
		s2.resize(4);
		cout << s2.size() << endl;
		cout << s2.capacity() << endl;

	}

	void test_string4()
	{
		string s1("c");
		s1.append("d");
		s1.push_back('e');
		s1 += 'f';
		cout << s1.c_str() << endl;

		s1.insert(1, 'g');
		cout << s1.c_str() << endl;
		s1.insert(0, 'g');
		cout << s1.c_str() << endl;
		s1.insert(s1.size(), 'g');
		cout << s1.c_str() << endl;

		s1.insert(1, "hh");
		cout << s1.c_str() << endl;
		s1.insert(0, "hh");
		cout << s1.c_str() << endl;
		s1.insert(s1.size(), "hh");
		cout << s1.c_str() << endl;

		s1.erase(s1.size()-2, 2);
		cout << s1.c_str() << endl;
		s1.erase(0, 2);
		cout << s1.c_str() << endl;
		s1.erase(1, 2);
		cout << s1.c_str() << endl;
		
		s1.erase(s1.size() - 1, 1);
		cout << s1.c_str() << endl;
		s1.erase(0, 1);
		cout << s1.c_str() << endl;
		s1.erase(1, 1);
		cout << s1.c_str() << endl;
	}

	void test_string5()
	{
		string s1("abcdefg");
		cout << s1.find('a', 0) << endl;
		cout << s1.find('a', 1) << endl;

		cout << s1.find("cd", 0) << endl;
		cout << s1.find("cd", 1) << endl;

		cout << s1.substr(0, 2).c_str() << endl;
		cout << s1.substr(2, 2).c_str() << endl;
	}

	void test_string6()
	{
		string s1("aaa");
		string s2("bbb");
		cout << (s1 < s2) << endl;
		cout << (s1 <= s2) << endl;
		cout << (s1 == s2) << endl;
		cout << (s1 != s2) << endl;
		cout << (s1 > s2) << endl;
		cout << (s1 >= s2) << endl;
	}

	void test_string7()
	{
		string s1, s2, s3;
		cin >> s1 >> s2 >> s3;
		cout << s1 << endl;
		cout << s2 << endl;
		cout << s3 << endl;
	}
}

int main()
{
	Lzc::test_string3();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lzc-c

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值