当前位置 博文首页 > senjiaxi的博客:C++STL—string类的模拟实现

    senjiaxi的博客:C++STL—string类的模拟实现

    作者:[db:作者] 时间:2021-08-19 15:44

    namespace shen
    {
    class string
    {
    public:
    typedef char* iterator;
    //
    // iterator
    iterator begin(){
    return _str;
    }
    iterator end(){
    return _str + _size;
    }

    	string(const char* str = ""){
    		_size = strlen(str);
    		_capacity = _size;
    		_str = new char[_capacity + 1];
    		strcpy(_str, str);
    	}
    	string(const string& s){
    		_size = s._size;
    		_capacity = s._capacity;
    		_str = new char[s._capacity + 1];
    		strcpy(_str, s._str);
    	}
    
    	~string(){
    		delete[] _str;
    		_str = nullptr;
    		_size = _capacity = 0;
    	}
    
    
    	void reserve(size_t n){
    		if (n > _capacity){
    			char* newstr = new char[n + 1];
    			strcpy(newstr, _str);
    			delete[] _str;
    			_str = newstr;
    			_capacity = n;
    		}
    	}
    	void PushBack(char c){
    		if (_size == _capacity){
    			size_t newcapcacity = _capacity == 0 ? 2 : _capacity * 2;
    			reserve(newcapcacity);
    		}
    		_str[_size] = c;
    		++_size;
    		_str[_size] = '\0';
    	}
    	string& operator+=(char c){
    		this->PushBack(c);
    		return *this;
    	}
    	void Append(const char* str){
    		size_t len = strlen(str);
    		if (len > _capacity){
    			reserve(_size + len);
    		}
    		strcpy(_str + _size, str);
    		_size += len;
    	}
    	string& operator+=(const char* str){
    		this->Append(str);
    		return *this;
    	}
    	void clear(){
    		_size = 0;
    		_str[_size] = '\0';
    	}
    	const char* c_str()const{
    		return _str;
    	}
    
    
    	/
    	// capacity
    	size_t size()const{
    		return _size;
    	}
    	size_t capacity()const{
    		return _capacity;
    	}
    	char& operator[](size_t i){
    		assert(i < _size);
    		return _str[i];
    	}
    	bool empty()const{
    		return _size == 0;
    	}
    	void resize(size_t n, char c = '\0'){
    		if (n < _size){
    			_str[n] = '\0';
    			_size = n;
    		}
    		else{
    			if (n>_capacity){
    				reserve(n);
    			}
    			for (size_t i = _size; i < n; ++i){
    				_str[i] = c;
    			}
    			_size = n;
    			_str[_size] = '\0';
    		}
    	}
    
    
    
    	//relational operators
    	bool operator<(const string& s){
    		int ret = strcmp(_str, s._str);
    		return ret < 0;
    	}
    	bool operator<=(const string& s){
    		return *this<s || *this == s;
    	}
    	bool operator>(const string& s){
    		return !(*this <= s);
    	}
    	bool operator>=(const string& s){
    		return !(*this < s);
    	}
    	bool operator==(const string& s){
    		int ret = strcmp(_str, s._str);
    		return ret == 0;
    	}
    	bool operator!=(const string& s){
    		return !(*this == s);
    	}
    
    	// 返回c在string中第一次出现的位置
    	size_t find(char c, size_t pos = 0) const{
    		for (size_t i = pos; i < _size; ++i){
    			if (_str[i] == c){
    				return i;
    			}
    		}
    		return npos;
    	}
    	// 返回子串s在string中第一次出现的位置
    	size_t find(const char* s, size_t pos = 0) const{
    		char* p = strstr(_str, s);
    		if (p == nullptr){
    			return npos;
    		}
    		else{
    			return p - _str;
    		}
    	}
    	// 在pos位置上插入字符c/字符串str,并返回该字符的位置
    	string& insert(size_t pos, char c){
    		assert(pos < _size);
    		if (_size == _capacity){
    			size_t newcapacity = _capacity == 0 ? 2 : _capacity * 2;
    			reserve(newcapacity);
    		}
    		int end = _size;
    		while (end >= pos){
    			_str[end + 1] = _str[end];
    			--end;
    		}
    		_str[_size] = c;
    		++_size;
    		return *this;
    	}
    	string& insert(size_t pos, const char* str){
    		assert(pos < _size);
    		size_t len = strlen(str);
    		if (_size + len > _capacity){
    			reserve(_size + len);
    		}
    		int end = _size;
    		while (end >= pos){
    			_str[end + len] = _str[end];
    			--end;
    		}
    		strncpy(_str + pos, str, len);
    		_size += len;
    		return *this;
    	}
    
    	// 删除pos位置上的元素,并返回该元素的下一个位置
    	string& erase(size_t pos, size_t len){
    		if (len >= _size - pos){
    			_str[pos] = '\0';
    			_size = pos;
    		}
    		else{
    			size_t i = pos + len;
    			while (i <= _size){
    				_str[pos++] = _str[i++];
    			}
    			_size -= len;
    		}
    		return *this;
    	}
    private:
    	char* _str;
    	size_t _capacity;//能存多少个有效字符,/0不算有效字符
    	size_t _size;//已经有多少个有效字符
    	static size_t npos;
    };
    size_t string::npos = -1;
    istream& operator>>(istream& in, string& s)
    {
    	while (1)
    	{
    		char ch;
    		//in >> ch;
    		ch = in.get();
    		if (ch == ' ' || ch == '\n')
    		{
    			break;
    		}
    		else
    		{
    			s += ch;
    		}
    	}
    
    	return in;
    }
    
    ostream& operator<< (ostream& out,  string& s)
    {
    	for (size_t i = 0; i < s.size(); ++i)
    	{
    		cout << (s[i]);
    	}
    
    	return out;
    
    }
    

    }
    本来想着挺简单的,都不想写底层代码,写了之后才发现里面有很多知识还是模糊,写了string类一看时间才发现写了两个小时,哎,继续加油吧。

    cs