阅读量:0
目录
1.模拟实现string
1.string基本属性和大体框架
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> #include <string> #include<assert.h> namespace String { class string { public: //默认构造 string(const char* str="") { _size = strlen(str); //capacity不包含"\0" _capacity = _size; _str = new char[_capacity + 1]; strcpy(_str, str); } //析构函数 ~string() { delete[] _str; _str = nullptr; _size = _capacity = 0; } //基本变量 private: char* _str; size_t _size; size_t _capacity; };
2.基本函数
2.1size()
内容个数
//模拟size() size_t size() { return _size; }
2.2 []
访问元素
# //模拟[] 普通引用和const引用两个版本 char& operator[](size_t pos) { assert(pos < _size); return _str[pos]; } const char& operator[](size_t pos) const { assert(pos < _size); return _str[pos]; }
2.3 begin() 和end()
简单用typedef重定义char* 来模拟iterator获取begin()和end()
typedef char* iterator; iterator begin() { return _str; } iterator end() { return _str + _size; }
2.4capacity()
获取容量
size_t capacity() const { return _capacity; }
2.5 reserve
调整空间
void string::reserve(size_t n) { if (n > _capacity) { char* tmp = new char[n + 1]; strcpy(tmp, _str); delete[] _str; _str = tmp; _capacity = n; } }
2.6push_back
尾插字符
void string::push_back(char ch) { if (_size = _capacity) { reserve(_capacity == 0 ? 4 : _capacity * 2); } _str[_size++] = ch; _str[_size] = '\0'; }
2.7 append
尾插支付串
void string::append(const char* str) { size_t len = strlen(str); //大于二倍就要多少开多少,低于二倍按二倍开 if (_size + len > _capacity) { reserve(_size + len > 2 * _capacity ? _size + len : 2 * _capacity); } strcpy(_str + _size, str); _size += len; }
2.8 +=
string& string::operator+=(char ch) { push_back(ch); return *this; } string& string::operator+=(const char* str) { append(str); return *this; }
2.9insert
在指定位置插入字符或字符串
void string::insert(size_t pos, char ch) { if (_size == _capacity) { reserve(_capacity == 0 ? 4 : _capacity * 2); } assert(pos <= _size); size_t end = _size; while (end > pos) { _str[end ] = _str[end-1]; --end; } _str[pos] = ch; ++_size; } void string::insert(size_t pos, const char* s) { assert(pos <= _size); size_t len = strlen(s); if(len==0) return; if (_size + len > _capacity) { reserve(_size + len > 2 * _capacity ? _size + len : 2 * _capacity); } size_t end = _size + len; while (end > pos + len-1) { _str[end - len] = _str[end]; --end; } for (size_t i = 0; i < len; i++) { _str[pos + i] = s[i]; } _size += len; }
2.10 erase
删除指定位置元素
void string::erase(size_t pos, size_t len) { if (len >= _size - pos) { _str[pos] = '\0'; _size = pos; } else { for (size_t i = pos + len; i < _size; i++) { _str[i - len] = _str[i]; } _size -= len; } }
2.10find
查找字符或字符串
size_t string::find(char ch, size_t pos = 0) { 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 = 0) { assert(pos < _size); const char* ptr = strstr(_str + pos, str); if (ptr == nullptr) { return npos; } else { return ptr - _str; } }
2.11substr
提取字符重新构建string类
//深拷贝 string(const string&s) { _str = new char[s._capacity + 1]; strcpy(_str, s._str); _size = s._size; _capacity = s._capacity; } string string::substr(size_t pos, size_t len ) { assert(pos < _size); //len答疑剩余有效长度更新一下 if (len > _size - len) { len = _size - len; } string sub; sub.reserve(len); for (size_t i = 0; i < len; i++) { sub += _str[pos + i]; } return sub; }
2.12 =
//s2=s1 string& operator=(const string& s) {//避免s1=s1出现释放了_str if(this!=&s) delete[] _str; _str = new char[s._capacity]; strcpy(_str, s._str); _size = s._size; _capacity = s._capacity; return *this; }
2.12 <
bool operator<(const string& s1, const string& s2) { return strcmp(s1.Get_str(), s2.Get_str())<0; }
2.13 >
bool operator>(const string& s1, const string& s2) { return !(s1 <= s2); }
2.14 <=
bool operator<=(const string& s1, const string& s2) { return s1 < s2 || s1 == s2; }
2.15 >=
bool operator>=(const string& s1, const string& s2) { return !( s1 < s2); }
2.16 ==
bool operator==(const string& s1, const string& s2) { return strcmp(s1.Get_str(), s2.Get_str()) ==0; }
2.17 !=
bool operator!=(const string& s1, const string& s2) { return !(s1 == s2); }
2.18 clear
void clear() { _str[0]='\0'; _size=0; }
2.19 <<
ostream& operator<<(ostream& out, const string& s) { for (auto ch : s) { out << ch; } return out; }
2.20 >>
istream& operator >> (istream& in, string& s) { char ch; ch = in.get(); while (ch != ' ' && ch != '\n') { s += ch; ch = in.get(); } return in; }
3.完整代码
string.h
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> #include <string> #include<assert.h> namespace String { class string { public: //s1(s) string(const string&s) { _str = new char[s._capacity + 1]; strcpy(_str, s._str); _size = s._size; _capacity = s._capacity; } string(const char* str="") { _size = strlen(str); //capacity不包含"\0" _capacity = _size; _str = new char[_capacity + 1]; //+1位置给'\0' strcpy(_str, str); } ~string() { delete[] _str; _str = nullptr; _size = _capacity = 0; } //短小频繁,默认inline //获取_str char* Get_str() const { return _str; } //模拟size() size_t size() { return _size; } //模拟实现capacity() size_t capacity() const { return _capacity; } //简单用typedef重定义char* 来模拟iterator获取begin()和end() typedef char* iterator; iterator begin() const { return _str; } iterator end() const { return _str + _size; } //模拟[] 普通引用和const引用两个版本 char& operator[](size_t pos) { assert(pos < _size); return _str[pos]; } const char& operator[](size_t pos) const { assert(pos < _size); return _str[pos]; } //s2=s1 string& operator=(const string& s) {//避免s1=s1出现释放了_str if (this != &s) delete[] _str; _str = new char[s._capacity]; strcpy(_str, s._str); _size = s._size; _capacity = s._capacity; return *this; } void clear() { _str[0] = '\0'; _size = 0; } 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* s); void erase(size_t pos, size_t len = npos); size_t find(char ch, size_t pos = 0); size_t find(const char* str, size_t pos = 0); string substr(size_t pos = 0, size_t len = npos); private: char* _str; size_t _size; size_t _capacity; static const size_t npos; }; bool operator>(const string& s1, const string& s2); bool operator>=(const string& s1, const string& s2); bool operator<(const string& s1, const string& s2); bool operator<=(const string& s1, const string& s2); bool operator<=(const string& s1, const string& s2); bool operator==(const string& s1, const string& s2); bool operator!=(const string& s1, const string& s2); istream& operator>>(istream& out, const string& s); istream& operator<<(istream& in, string& s); }
string.c
//text.cpp using namespace std; #include"FileName.h" namespace String { const size_t string::npos = -1; 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) { reserve(_capacity == 0 ? 4 : _capacity * 2); } _str[_size++] = ch; _str[_size] = '\0'; } string& string::operator+=(char ch) { push_back(ch); return *this; } void string::append(const char* str) { size_t len = strlen(str); if (_size + len > _capacity) { reserve(_size + len > 2 * _capacity ? _size + len : 2 * _capacity); } strcpy(_str + _size, str); _size += len; } string& string::operator+=(const char* str) { append(str); return *this; } void string::insert(size_t pos, char ch) { if (_size == _capacity) { reserve(_capacity == 0 ? 4 : _capacity * 2); } assert(pos <= _size); size_t end = _size; while (end > pos) { _str[end ] = _str[end-1]; --end; } _str[pos] = ch; ++_size; } void string::insert(size_t pos, const char* s) { assert(pos <= _size); size_t len = strlen(s); if (len == 0) return; if (_size + len > _capacity) { reserve(_size + len > 2 * _capacity ? _size + len : 2 * _capacity); } size_t end = _size + len; while (end > pos + len-1) { _str[end - len] = _str[end]; --end; } for (size_t i = 0; i < len; i++) { _str[pos + i] = s[i]; } _size += len; } void string::erase(size_t pos, size_t len) { if (len >= _size - pos) { _str[pos] = '\0'; _size = pos; } else { for (size_t i = pos + len; i < _size; i++) { _str[i - len] = _str[i]; } _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 ) { assert(pos < _size); const char* ptr = strstr(_str + pos, str); if (ptr == nullptr) { return npos; } else { return ptr - _str; } } string string::substr(size_t pos, size_t len ) { assert(pos < _size); //len答疑剩余有效长度更新一下 if (len > _size - len) { len = _size - len; } string sub; sub.reserve(len); for (size_t i = 0; i < len; i++) { sub += _str[pos + i]; } return sub; } bool operator<(const string& s1, const string& s2) { return strcmp(s1.Get_str(), s2.Get_str())<0; } bool operator<=(const string& s1, const string& s2) { return s1 < s2 || s1 == s2; } bool operator>(const string& s1, const string& s2) { return !(s1 <= s2); } bool operator>=(const string& s1, const string& s2) { return !( s1 < s2); } bool operator==(const string& s1, const string& s2) { return strcmp(s1.Get_str(), s2.Get_str()) ==0; } bool operator!=(const string& s1, const string& s2) { return !(s1 == s2); } ostream& operator<<(ostream& out, const string& s) { for (auto ch : s) { out << ch; } return out; } istream& operator >> (istream& in, string& s) { s.clear(); const int N = 256; char buff[N]; int i = 0; char ch; //in >> ch ; ch = in.get(); while (ch != ' ' && ch != '\n') { buff[i++] = ch; if (i == N - 1) { buff[i] = '\0'; s += buff; i = 0; } ch = in.get(); } return in; } } int main() { return 0; }