STL_String 详解

目录

string常用接口函数

String类模拟实现

String类结构

默认成员函数

构造函数

析构函数 

赋值重载函数 

 容量大小相关函数

size

capacity

reserve 

resize 

clear 

迭代器 

反向迭代器的实现

迭代器结构

 begin 

end

rebegin

rend

 修改相关函数

 operator[]

 insert

erase 

push_back

append 

operator+= 

查找函数

 find

 大小比较

输入输出重载 

总结


string常用接口函数

函数功能
string()构造空的string对象
string(const char*s) 用字符串s构造string对象
string(const string&str)拷贝构造
size()返回有效字符长度
capacity()返回空间大小
empty()判断是否为空串
clear()清空有效字符
reserve(size_t n)预留空间,扩容
resize(size_t n,char ch)将有效字符改为n个,多余用ch填充

operator[](size_t pos)返回pos位置字符
begin+end迭代器
rbegin+rend反向迭代器
push_back(char ch)尾插ch字符
append(const char *s)尾插s字符串
operator+=(char ch)尾插ch字符
operator+=(const char*s)尾插s字符串
operator=()赋值重载
find+rfind查找字符或字符串
c_str返回const char*字符串
operator>>  <<输出,输入运算符重载
operator> ,<, =, !=,>=,<=比较

    

String类模拟实现

String类结构

private:
		char* _str;
		size_t _size;
		size_t _capacity;
		static size_t npos;//用于find

默认成员函数

构造函数

这里的构造函数一定要进行深拷贝,如果没有重新开辟空间进行深拷贝,将会造成析构两次,引起程序崩溃。

tmp是临时变量,出了作用域自动销毁,让其与_str交换,能释放交换前_str的空间并完成深拷贝。

        void swap(string& str)
		{
			std::swap(_str, str._str);
			std::swap(_size, str._size);
			std::swap(_capacity, str._capacity);
		}
        string(const char* s = "")
			:_str(new char[strlen(s) + 1])
			, _size(strlen(s))
			, _capacity(_size)
		{
			strcpy(_str, s);
		}
		string(const string& str)
			:_str(nullptr)
			, _size(0)
			, _capacity(0)
			//一定要初始化,不然_str会带有随机值
		{
			string tmp(str._str);//tmp临时变量,出了作用域自动销毁,
            //交换后能顺便销毁_str
			swap(tmp);
		}

析构函数 

 析构函数完成对象内资源清理。

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

赋值重载函数 

string& operator=(const char* s)
		{
			string tmp(s);
			swap(tmp);
			return *this;
		}
		string& operator=(string str)
		{
			swap(str);
			return *this;
		}

 容量大小相关函数

size

仅写一份const版本,这样const对象和非const对象都能够调用它。

size_t size()const
		{
			return _size;
		}

capacity

size_t capacity()const
		{
			return _capacity;
		}

reserve 

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

resize 

void resize(size_t n,char ch='\0')
		{
			if (n > _size)
			{
				if (n > _capacity)
				{
					reserve(n);
				}
				memset(_str + _size, ch, n - _size);
				_size = n;
				_str[_size] = '\0';
			}
			else
			{
				_str[n] = '\0';
				_size = n;
			}
		}

clear 

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

迭代器 

反向迭代器的实现

反向迭代器内部结构其实就是一个正向迭代器,通过模板使反向迭代器能够被多种容器所通用,这正是c++泛型编程的强大体现。

template<class Iterator,class Ref,class Ptr>
	class _reverse_iterator
	{
	public:
		typedef _reverse_iterator<Iterator, Ref, Ptr> self;
		_reverse_iterator(Iterator it)
			:_it(it)
		{
		}
		Ref operator*()
		{
			Iterator prev = _it;
			return *--prev;
		}
		Ptr operator->()
		{
			return &(operator*());
		}
		bool operator==(const self& rit)const
		{
			return _it == rit._it;
		}
		bool operator!=(const self& rit)const
		{
			return _it != rit._it;
		}
		self& operator++()
		{
			_it--;
			return *this;
		}
		self operator++(int )
		{
			self tmp(*this);
			_it--;
			return tmp;
		}
		self& operator--()
		{
			_it++;
			return *this;
		}
		self operator--(int)
		{
			self tmp(*this);
			_it++;
			return tmp;
		}
		
	private:
		Iterator _it;
	};

迭代器结构

typedef char* iterator;
		typedef const char* const_iterator;
		typedef _reverse_iterator<iterator, char&, char*> reverse_iterator;
		typedef _reverse_iterator<const_iterator, const char&,const char*> const_reverse_iterator;

 begin 

end

rebegin

rend

reverse_iterator rbegin()
		{
			return reverse_iterator(end());
		}
		reverse_iterator rend()
		{
			return reverse_iterator(begin());
		}
		const_reverse_iterator rbegin()const
		{
			return const_reverse_iterator(end());
		}
		const_reverse_iterator rend()const
		{
			return const_reverse_iterator(begin());
		}
		iterator begin()
		{
			return _str;
		}
		iterator end()
		{
			return _str + _size;
		}
		const_iterator begin()const
		{
			return _str;
		}
		const_iterator end()const
		{
			return _str + _size;
		}

 修改相关函数

 operator[]

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

 insert

string& insert(size_t pos, char ch)
		{
			assert(pos <= _size);
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : _capacity * 2);
			}
			size_t end = _size + 1;
			while (end > pos)
			{
				_str[end] = _str[end - 1];
				end--;
			}
			_str[pos] = ch;
			_size++;
			return *this;
		}
		string& insert(size_t pos, const char* s)
		{
			assert(pos <= _size);
			size_t len = strlen(s);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}
			size_t end = _size + len;
			while (end >= pos+len)//注意这里边界,要有=
			{
				_str[end] = _str[end - len];
				end--;
			}
			strncpy(_str + pos, s, len);
			_size += len;
			return *this;
		}

erase 

string& erase(size_t pos, size_t len =npos)
		{
			assert(pos < _size);
			if (len==npos||pos + len > _size)
			{
				_str[pos] = '\0';
				_size = pos;
			}
			else
			{
				strcpy(_str + pos, _str + pos + len);
				_size -= len;
			}
			return *this;
		}

push_back

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

append 

void append(const char* s)
		{
			/*size_t len = strlen(s);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}
			strcpy(_str + _size, s);
			_size += len;*/
			insert(_size, s);
		}

operator+= 

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

查找函数

 find

size_t find(char ch, size_t pos = 0)const
		{
			assert(pos < _size);
			for (size_t i = 0; i < _size; i++)
			{
				if (_str[i] == ch)
				{
					return i;
				}
			}
			return npos;
		}
		size_t find(const char* s, size_t pos = 0)const
		{
			assert(pos < _size);
			char* ptr = strstr(_str + pos, s);
			if (ptr)
			{
				return ptr - _str;
			}
			else
			{
				return npos;
			}
		}

 大小比较

 =,!=,>,<,>=,<=这几种函数的实现。这里采用的是全局实现。

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

输入输出重载 

 输入输出函数不一定要重载成友元函数,如果不需要访问对象内受保护的资源,重载全局也可以。

ostream& operator<<(ostream& out, const string& s)
	{
		for (size_t i = 0; i < s.size(); i++)
		{
			out << s[i] << " ";
		}
		return out;
	}
	istream& operator>>(istream& in, string& s)
	{
		s.clear();
		char ch = in.get();
		while (ch != ' ' && ch != '\n')
		{
			s += ch;
			ch = in.get();
		}
		return in;
	}

总结

stl还需更加深入的学习。 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

嚞譶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值