C++:string的的模拟实现

慈云数据 6个月前 (05-28) 技术支持 40 0

hello,各位小伙伴,本篇文章跟大家一起学习《C++:string的的模拟实现》,感谢大家对我上一篇的支持,如有什么问题,还请多多指教 !

如果本篇文章对你有帮助,还请各位点点赞!!!

在这里插入图片描述

话不多说,开始进入正题

文章目录

    • :heartbeat:模拟实现string头文件
    • 1.:maple_leaf:iterator begin();和iterator end();:maple_leaf:const_iterator begin() const;和const_iterator end() const;
    • 2.:maple_leaf:构造函数和拷贝构造函数
      • :leaves:构造函数
      • :leaves:拷贝构造函数
      • 3.:maple_leaf:析构函数
      • 4.:maple_leaf:赋值重载函数
      • 5.:maple_leaf:const char* c_str() const;
      • 6.:maple_leaf:size_t size() const;
      • 7. :maple_leaf:[]运算符重载
        • :leaves:char& operator[](size_t pos);
        • :leaves:const char& operator[](size_t pos) const;
        • 8.:maple_leaf:void reserve(size_t n);
        • 9.:maple_leaf:void push_back(char ch);
        • 10.:maple_leaf:void append(const char* str);
        • 11.:maple_leaf:+=运算符重载函数
          • :leaves:string& operator+=(char ch);
          • :leaves:string& operator+=(const char* str);
          • 12.:maple_leaf:insert的实现
            • :leaves:void insert(size_t pos, char ch);
            • :leaves:void insert(size_t pos, const char* str);
            • 13.:maple_leaf:void erase(size_t pos = 0, size_t len = npos);
            • 14.:maple_leaf:find函数实现
              • :leaves:size_t find(char ch, size_t pos);
              • :leaves:size_t find(const char* str, size_t pos);
              • 15.:maple_leaf:swap函数的实现
              • 16.:maple_leaf:substr的实现
              • 17.:maple_leaf:运算符重载
              • 18.:maple_leaf:>>重载函数和重载函数
              • :leaves: class string { public: typedef char* iterator; typedef const char* const_iterator; iterator begin(); iterator end(); const_iterator begin() const; const_iterator end() const; //string(); string(const char* str = ""); string(const string& s); string& operator=(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* str, size_t pos = 0); void swap(string& s); string substr(size_t pos = 0, size_t len = npos); bool operator (istream& is, string& str); ostream& operator 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; } _str = new char[_size + 1]; _capacity = _size; strcpy(_str, str); } string tmp(s._str); swap(tmp); } _size = _capacity = 0; delete[] _str; _str = nullptr; } if(this != &s) { char* tmp = new char[s._capacity + 1]; strcpy(tmp,s._str); delete[] _str; _size = s._size; _capacity = s._capacity; _str = tmp; } return *this; } swap(s);// 让别人来干活 return *this; } return _str; } return _size; } assert(pos

                🍃在实现insert之后的写法:

                void string::append(const char* str)
                {
                	insert(_size,str);
                }
                

                11.🍁+=运算符重载函数

                🍃string& operator+=(char ch);

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

                🍃string& operator+=(const char* str);

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

                12.🍁insert的实现

                🍃void insert(size_t pos, char ch);

                void string::insert(size_t pos, char ch)
                {
                	assert(pos 
                		size_t newcapacity = _capacity == 0 ? 4 : 2*_capacity;
                		reserve(newcapacity);
                	}
                	size_t end = _size + 1;
                	while(end  pos)
                	{
                		_str[end] = _str[end - 1];
                		--end;
                	}
                	_str[pos] = ch;
                	++_size;
                }
                

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

                void string::insert(size_t pos, const char* str)
                {
                	assert(pos  _capacity)
                	{
                		reserve(_size + len);
                	}
                	size_t end = _size + len;
                	while(end >= pos + len)
                	{
                		_str[end] = _str[end - len];
                		--end;
                	}
                	memcpy(_str + pos,str,len);
                	// 由于strcpy会将后面的字符覆盖,所以这里要用memcpy
                	_size += len;
                }
                

                13.🍁void erase(size_t pos = 0, size_t len = npos);

                const size_t string::npos = -1;
                void string::erase(size_t pos, size_t len)
                {
                	assert(_size > pos);
                	if(len > _size - pos)
                	{
                		_str[pos] = '\0';
                		_size = pos;
                	}
                	else
                	{
                		strcpy(_str + pos, _str + pos + len);
                		_size -= len;
                	}
                }
                

                14.🍁find函数实现

                🍃size_t find(char ch, size_t pos);

                size_t string::find(char ch, size_t pos)
                {
                	assert(_size > pos);
                	int index = pos;
                	// npos在前面有定义
                	while (index  
                

                🍃size_t find(const char* str, size_t pos);

                size_t string::find(const char* str, size_t pos)
                {
                	char* p = strstr(_str + pos, str);
                	return p - _str;
                }
                

                15.🍁swap函数的实现

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

                16.🍁substr的实现

                string string::substr(size_t pos, size_t len)
                {
                	if(len >= _size - pos)
                	{
                		string tmp(_str + pos);
                		return tmp;
                	}
                	else
                	{
                		string tmp;
                		tmp.reserve(len);
                		for(int i = 0;i  
                

                17.🍁运算符重载

                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 string::operator
                	return strcmp(_str, s._str)  
                

                18.🍁>>重载函数和重载函数

                由于流提取需要将整个值改变,所以需要清空原本的值,所以引用函数clear()。

                由于流提取会自动忽略空格和换行,所以我们用get()函数来解决流提取问题

                void string::clear()
                {
                	_str[0] = '\0';
                	_size = 0;
                }
                istream& My_string::operator>> (istream& is, string& str)
                {
                	str.clear();
                	char buff[128];
                	char ch = is.get();
                	int i = 0;
                	while (ch != ' ' && ch != '\n')
                	{
                		buff[i] = ch;// 临时变量,能够快速生成
                		++i;
                		if (i == 127)// 当buff已经满了,进行+=操作
                		{
                			buff[i] = '\0';
                			str += buff;
                			i = 0;// 记得i要置为0
                		}
                		ch = is.get();// 提取下一个字符
                	}
                	if (i > 0)// 当还有字符没有进行插入时,继续进行+=操作
                	{
                		buff[i] = '\0';
                		str += buff;
                	}
                	return is;
                }
                

                🍃 for (size_t i = 0; i

微信扫一扫加客服

微信扫一扫加客服

点击启动AI问答
Draggable Icon