赞
踩
- #define _CRT_SECURE_NO_WARNINGS 1
- #pragma once
- #include<iostream>
- #include<assert.h>
- using namespace std;
-
- namespace STring
- {
- 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* str, 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;
- };
- }
//string.cpp
- #include"string.h"
- namespace STring
- {
- const size_t string::npos = -1;
-
- string::iterator string::begin()
- {
- return _str;
- }
- string::const_iterator string::begin() const
- {
- return _str;
- }
- string::iterator string::end()
- {
- return _str + _size;
- }
- string::const_iterator string::end() const
- {
- return _str+_size;
- }
- string:: string(const char* str)
- :_size(strlen(str))
- {
- _str = new char[_size+1];
- _capacity = _size+1;
- strcpy(_str, str);
- }
- string::string(const string& s)
- {
- _str = new char[s._capacity];
- strcpy(_str, s._str);
- _size = s._size;
- _capacity = s._capacity;
- }
- string::~string()
- {
- delete[] _str;
- _str = nullptr;
- _size = _capacity = 0;
- }
- const char* string::c_str() const
- {
- return _str;
- }
- size_t string :: size() const
- {
- return _size;
- }
- 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)
- {
- if (_size == _capacity)
- {
- size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
- reserve(newcapacity);
- }
- _str[_size] = ch;
- _str[_size + 1] = '\0';
- ++_size;
- }
- void string::append(const char* str)
- {
- size_t len = strlen(str);
- if (_size + len > _capacity)
- {
- reserve(_size + len);
- }
- strcpy(_str + _size, _str);
- _size += len;
- insert(_size, _str);
- }
- 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] + 1;
- --end;
- }
- _str[pos] = ch;
- ++_size;
- }
- void string::insert(size_t pos, const char* str)
- {
- assert(pos <= _size);
- int len = strlen(str);
- if (_size + len > _capacity)
- {
- reserve(_capacity+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 = len;
- }
- 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* str, size_t pos)
- {
- char* p = strstr(_str + pos, str);
- 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[i + pos];
- }
- return sub;
- }
- }
- string& string:: operator=(const string& s)
- {
- if (this != &s)
- {
- char* tmp = new char[s._capacity];
- strcpy(tmp, s._str);
- delete[] _str;
- _str = tmp;
- _size = s._size;
- _capacity = s._capacity;
- }
- return *this;
- }
-
- char& string::operator[](size_t pos)
- {
- if (pos < _size)
- return _str[pos];
- }
- const char& string::operator[](size_t pos) const
- {
- if (pos < _size)
- return _str[pos];
- }
- string& string::operator+=(char ch)
- {
- push_back(ch);
- return *this;
- }
- string& string::operator+=(const char* str)
- {
- append(str);
- return *this;
- }
- 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;
- }
- }
现在我们看到的是,我这边写的,接下来我将会对于这些内容进行解析,首先我这边的模拟实现都是基于理解和string。
- namespace STring
- {
- }
- class string
- {
- public:
- private:
- }
这里的内容比较少。
- char* _str;
- size_t _size;
- size_t _capacity;
- const static size_t npos;
- 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=(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* str, 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();
我们都知道在string中我们常常使用迭代器来完成不少操作,我们知道迭代器其实就是一种指针,所以这里我们选择这样实现
- 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:: string(const char* str)
- :_size(strlen(str))
- {
- _str = new char[_size+1];
- _capacity = _size+1;
- strcpy(_str, str);
- }
首先我们用列表初始化得出存储的字符串大小,然后给字符串开辟空间,然后把求出空间(注意:字符串最后有'\0')然后通过拷贝函数把str字符串把内容拷贝给_str存储。
声明:
string(const string& s);
函数内部实现:
- string::string(const string& s)
- {
- _str = new char[s._capacity];
- strcpy(_str, s._str);
- _size = s._size;
- _capacity = s._capacity;
- }
思路具体和上面差不多,开辟新空间,然后把字符串拷入,把大小和空间也拷贝。
声明:
~string();
这里比较简单,就是把开辟的空间释放然后大小空间归零
- string::~string()
- {
- delete[] _str;
- _str = nullptr;
- _size = _capacity = 0;
- }
声明:
- const char* c_str() const;
- size_t size() const;
这个直接返回相应的值
- const char* string::c_str() const
- {
- return _str;
- }
- size_t string :: size() const
- {
- return _size;
- }
声明:
void reserve(size_t n);
函数内部:
- void string::reserve(size_t n)
- {
- if (n > _capacity)
- {
- char* tmp = new char[n + 1];
- strcpy(tmp, _str);
- delete[] _str;
- _str = tmp;
- _capacity = n;
- }
- }
这个函数(这里我们是大致模拟VS的)我们之前讲过如果n>_capacity就会扩容,否则无反应。
声明:
- void insert(size_t pos, char ch);
- void insert(size_t pos, const char* str);
因为我们会用inset插入字符和字符串,所以这里我们定义两种
函数内部:
- 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] + 1;
- --end;
- }
- _str[pos] = ch;
- ++_size;
- }
- void string::insert(size_t pos, const char* str)
- {
- assert(pos <= _size);
- int len = strlen(str);
- if (_size + len > _capacity)
- {
- reserve(_capacity+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 push_back(char ch);
- void append(const char* str);
着两个一个是尾插入,一个是插入字符串
- void string::push_back(char ch)
- {
- if (_size == _capacity)
- {
- size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
- reserve(newcapacity);
- }
- _str[_size] = ch;
- _str[_size + 1] = '\0';
- ++_size;
- }
- void string::append(const char* str)
- {
- size_t len = strlen(str);
- if (_size + len > _capacity)
- {
- reserve(_size + len);
- }
- strcpy(_str + _size, _str);
- _size += len;
- insert(_size, _str);
- }
这个就是正常尾插的实现,但是append是字符串,为了方便我们可以直接调用insert
声明:
void erase(size_t pos = 0, size_t len = npos);
这是声明我直接仿照的cplusplus的定义
注意我们需要
const size_t string::npos = -1;
函数内部:
- void string::erase(size_t pos, size_t len)
- {
- assert(pos < _size);
- if (len >= _size - pos)
- {
- _str[pos] = '\0';
- _size = len;
- }
- else
- {
- strcpy(_str + pos, _str + pos + len);
- _size = len;
- }
- }
这里很简单就是把pos后删除,我们考虑两种情况,一种是直接删除,一种是删除部分
声明:
- size_t find(char ch, size_t pos = 0);
- size_t find(const char* str, size_t pos = 0);
这个函数真正的底层实现很难,这里因为我们只是模拟实现,我们可以这样子解决
函数内部:
- 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* str, size_t pos)
- {
- char* p = strstr(_str + pos, str);
- return p - _str;
- }
声明:
void swap(string& s);
函数内部:
- void string::swap(string& s)
- {
- std::swap(_str, s._str);
- std::swap(_size, s._size);
- std::swap(_capacity, s._capacity);
- }
很简单的内容,没什么好说的
这个就是很简单的一个函数
声明:
string substr(size_t pos = 0, size_t len = npos);
函数内部:
- 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[i + pos];
- }
- return sub;
- }
- }
ps:上面我用了一些运算符重载,建议结合后面的运算符重载观看
这里是我所有使用的运算符重载以完成模拟实现
声明:
- string& operator=(const string& s);
- char& operator[](size_t pos);
- const char& operator[](size_t pos) const;
- string& operator+=(char ch);
- string& operator+=(const char *str);
- 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;
函数实现
- string& string:: operator=(const string& s)
- {
- if (this != &s)
- {
- char* tmp = new char[s._capacity];
- strcpy(tmp, s._str);
- delete[] _str;
- _str = tmp;
- _size = s._size;
- _capacity = s._capacity;
- }
- return *this;
- }
-
- char& string::operator[](size_t pos)
- {
- if (pos < _size)
- return _str[pos];
- }
- const char& string::operator[](size_t pos) const
- {
- if (pos < _size)
- return _str[pos];
- }
- string& string::operator+=(char ch)
- {
- push_back(ch);
- return *this;
- }
- string& string::operator+=(const char* str)
- {
- append(str);
- return *this;
- }
- 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);
- }
=号是重载的拷贝构造
[]号是实现下标访问
+=是重载push_back和append
剩下的都是完成判断的
声明:
void clear();
函数内部:
直接把_st[0]='\0',_size=0即可
- void string::clear()
- {
- _str[0] = '\0';
- _size = 0;
- }
这里我们之前写日期类的时候应该写过,我就不多讲了,直接展示
声明:
- istream& operator>> (istream& is, string& str);
- ostream& operator<< (ostream& os, const string& str);
注意:不要声明在类中
函数内部:
- 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;
- }
以上就是我们对于string类的模拟实现希望可以帮助大家更好的理解
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。