C++STL(string底层实现)

string.h

#pragma once
#include
#include
using namespace std;
namespace rain
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;
		iterator begin();
		iterator end();
		const_iterator begin() const;
		const_iterator end() const;
		string(const char* str = "");
		string(const string& s);
		string& operator=(const string& s);
		~string();
		const char* c_str()const;
		size_t size() const;
		char& operator[](size_t pos);
		const char& operator[](size_t pos) const;
		void reserve(size_t n);
		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 = 0, size_t len = npos);

		size_t find(char ch, size_t pos = 0);
		size_t find(const char* sub, size_t pos = 0);
		void swap(string& s);
		string substr(size_t pos = 0, size_t len = npos);
		bool operator<(const string& s)const;
		bool operator>(const string& s)const;
		bool operator<=(const string& s)const;
		bool operator>=(const string& s)const;
		bool operator==(const string& s)const;
		bool operator!=(const string& s)const;
		void clear();


	private:
		char* _str;
		size_t _size;
		size_t _capacity;
		const static size_t npos;
	};
	istream& operator>>(istream& is, string& str);
	ostream& operator<<(ostream& os, const string& str);
}

string.cpp

#define _CRT_SECURE_NO_WARNINGS
#include "string.h"
namespace rain
{
	const size_t string::npos = -1;
	string::iterator string::begin()
	{
		return _str;
	}
	string::iterator string::end()
	{
		return _str + _size;
	}
	string::const_iterator string::begin() const
	{
		return _str;
	}
	string::const_iterator string::end() const
	{
		return _str + _size;
	}
	string::string(const char* str)
		:_size(strlen(str))
	{
		_str = new char[_size + 1];
		_capacity = _size;
		strcpy(_str, str);
	}
	string::string(const string& s)
	{
		_str = new char[s._capacity + 1];
		strcpy(_str, s._str);
		_size = s._size;
		_capacity = s._capacity;
	}
	string& string::operator=(const string& s)
	{
		if (this != &s)
		{
			char* tmp = new char[s._capacity + 1];
			strcpy(tmp, s._str);
			delete[] _str;
			_str = tmp;
			_size = s._size;
			_capacity = s._capacity;
		}
		return *this;
	}
	string::~string()
	{
		delete[] _str;
		_str = nullptr;
		_size = _capacity = 0;
	}
	const char* string::c_str()const
	{
		return _str;
	}
	size_t string::size() const
	{
		return _size;
	}
	char& string::operator[](size_t pos)
	{
		assert(pos < _size);
		return _str[pos];
	}
	const char& string::operator[](size_t pos) const
	{
		assert(pos < _size);
		return _str[pos];
	}
	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
			_capacity = n;
		}
	}
	void string::push_back(char ch)
	{
		insert(_size, ch);
	}
	void string::append(const char* str)
	{
		insert(_size, str);
	}
	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)
		{
			size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
			reserve(newcapacity);
		}
		size_t end = _size + 1;
		while (end > pos)
		{
			_str[end] = _str[end - 1];
			--end;
		}
		_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);
		}
		size_t end = _size + len;
		while (end > pos + len - 1)
		{
			_str[end] = _str[end - len];
			--end;
		}
		memcpy(_str + pos, str, len);
		_size += len;
	}
	void string::erase(size_t pos, size_t len)
	{
		assert(pos < _size);
		if (len >= _size - pos)
		{
			_str[pos] = '\0';
			_size = pos;
		}
		else
		{
			strcpy(_str + pos, _str + pos + len);
			_size -= len;
		}
	}
	size_t string::find(char ch, size_t pos)
	{
		for (size_t i = pos; i < _size; i++)
		{
			if (_str[i] == ch)
			{
				return i;
			}
		}
		return npos;
	}
	size_t string::find(const char* sub, size_t pos)
	{
		char* p = strstr(_str + pos,sub);
		return  p - _str;
	}
	void string::swap(string& s)
	{
		std::swap(_str, s._str);
		std::swap(_size, s._size);
		std::swap(_capacity, s._capacity);
	}
	string string::substr(size_t pos, size_t len)
	{
		if (len > _size - pos)
		{
			string sub(_str + pos);
			return sub;
		}
		else
		{
			string sub;
			sub.reserve(len);
			for (size_t i = 0; i < len; i++)
			{
				sub += _str[pos + i];
			}
			return sub;
		}
	}
	bool string::operator<(const string& s) const
	{
		return strcmp(_str, s._str) < 0;
	}

	bool string::operator>(const string& s) const
	{
		return !(*this <= s);
	}

	bool string::operator<=(const string& s) const
	{
		return *this < s || *this == s;
	}

	bool string::operator>=(const string& s) const
	{
		return !(*this < s);
	}

	bool string::operator==(const string& s) const
	{
		return strcmp(_str, s._str) == 0;
	}

	bool string::operator!=(const string& s) const
	{
		return !(*this == s);
	}

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

	istream& operator>> (istream& is, string& str)
	{
		str.clear();
		char ch = is.get();
		while (ch != ' ' && ch != '\n')
		{
			str += ch;
			ch = is.get();
		}

		return is;
	}

	ostream& operator<< (ostream& os, const string& str)
	{
		for (size_t i = 0; i < str.size(); i++)
		{
			os << str[i];
		}

		return os;
	}
}

代码整体来说不算很难,但是有几个很重要的点
1.涉及到深拷贝,浅拷贝的问题
比如在没有显式定义其拷贝构造函数与赋值运算符重载时,此时编译器会生成默认的,用s1构造s2时,编译器会调用默认的拷贝构造,最终导致两个对象共同指向同一块内存空间,比如s1想更改时,s2会随之更改,更严重的是,在释放时,会造成一个空间被释放两次的情况,从而引起程序崩溃,这种拷贝方式就是浅拷贝,解决办法就是自己写一个深拷贝的拷贝构造,即我再申请一块空间,让两个对象不指向同一个空间,各玩各的
2.还有一些小技巧,比如我们完成了insert函数,那我们的push_back是不是就可以进行复用了呢?
3.C++STL(string底层实现)_第1张图片
这是一种比较新的赋值,这里故意不用引用,就是为了让他生成一个新的对象,然后交换指针即可,是不是很简单?

写时拷贝
浅拷贝还有另外一种解决办法,就是引用计数
我们用一个变量来记录资源使用者的个数,当某个对象被销毁时,引用计数-1,只有当计数为0时,我们才释放资源

你可能感兴趣的:(c++,算法)