std::string的模拟实现
模拟标准库中的string
代码
my_string.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include <assert.h>
#include <iostream>
using namespace std;
namespace my_string
{
class string
{
public:
typedef char* iterator;
typedef const char* const_iterator;
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
const_iterator begin() const
{
return _str;
}
const_iterator end() const
{
return _str + _size;
}
//构造 全缺省
string(const char* str = "")
:_size(strlen(str))
{
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str, str);
}
//拷贝构造
//传统方法
//string(const string& str)
//{
// _str = new char[str._capacity + 1];
// strcpy(_str, str._str);
// _size = str._size;
// _capacity = str._capacity;
//}
//现代写法
string(const string& str)
{
string tmp(str._str);
swap(tmp);
}
//析构
~string()
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
size_t size() const
{
return _size;
}
size_t capacity() const
{
return _capacity;
}
char& operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];
}
const char& operator[](size_t pos) const
{
assert(pos < _size);
return _str[pos];
}
const char* c_str()const
{
return _str;
}
void swap(string& s)
{
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
void push_back(char ch)
{
//扩容
//2倍
if (_size == _capacity)
{
reserve(_capacity==0?4:2*_capacity);
}
_str[_size] = ch;
_size++;
_str[_size] = '\0';
}
//扩容
void reserve(size_t n)
{
if (n > _capacity)
{
char* cmp = new char[n+1];
strcpy(cmp,_str);
delete[] _str;
_str = cmp;
_capacity = n;
}
}
void append(const char* str)
{
//1.
//扩容
//扩 str的长度
/*size_t len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
strcpy(_str + _size, str);
_size += len;*/
//2.
insert(_size, str);
}
string& operator+=(char ch)
{
push_back(ch);
return *this;
}
string& operator+=(const char* str)
{
append(str);
return *this;
}
void insert(size_t pos, char ch)
{
assert(pos <= _size);
//扩容
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : 2 * _capacity);
}
// end 会等于-1 无法和size_t比较
int end = _size;
while (end >= (int)pos)
{
_str[end+1] = _str[end];
end--;
}
第二种比较
//size_t end = _size+1;
//while (end >= (int)pos)
//{
// _str[end] = _str[end-1];
// end--;
//}
_str[pos] = ch;
_size++;
}
void insert(size_t pos,const char* str)
{
assert(pos <= _size);
size_t len = strlen(str);
//扩容
if (len+_size > _capacity)
{
reserve(_capacity == 0 ? 4 : 2 * _capacity);
}
char* cmp = new char[_size - pos + 1];
strcpy(cmp, _str + pos);
strcpy(_str + pos, str);
strcpy(_str + pos + len, cmp);
_size += len;
}
void erase(size_t pos,size_t len = npos)
{
assert(pos < _size);
if (len>=_size-pos||len == npos)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
}
void resize(size_t n, char ch = '\0')
{
if (n < _size)
{
_str[n] = '\0';
_size = n;
}
else
{
reserve(n);
for (size_t i = _size; i < n; i++)
{
_str[i] = ch;
}
_str[n] = '\0';
_size += n;
}
}
//s1 = s3
//string& operator=(const string& s)
//{
// char* tmp = new char[s._capacity+1];
// strcpy(tmp, s._str);
// delete[] _str;
// _str = tmp;
// _size = s._size;
// _capacity = s._capacity;
// return *this;
//}
//现代写法
string& operator=(string tmp)
{
swap(tmp);
return *this;
}
bool operator==(const string& s)
{
int ret = strcmp(_str, s._str);
return ret==0;
}
size_t find(char ch,size_t pos =0)const
{
for (size_t i = pos; i < _size; i++)
{
if (_str[i] == ch)
return i;
}
return npos;
}
size_t find(const char* sub, size_t pos = 0)const
{
assert(pos < _size);
char* ptr=strstr(_str, sub);
if (ptr)
{
return ptr - _str;
}
else
{
return npos;
}
}
string substr(size_t pos = 0, size_t len = npos)
{
string sub;
if (len >= _size - pos)
{
for (size_t i = pos; i < _size; i++)
{
sub += _str[i];
}
}
else
{
for (size_t i = pos; i < pos + len; i++)
{
sub += _str[i];
}
}
return sub;
}
void clear()
{
_size = 0;
_str[_size] = '\0';
}
private:
char* _str=nullptr;
size_t _size=0;
size_t _capacity=0;
public:
static const int npos;
};
const int string::npos = -1;
void swap(string& s1,string& s2)
{
s1.swap(s2);
}
bool operator==(const string& s1,const string& s2)
{
int ret = strcmp(s1.c_str(), s2.c_str());
return ret==0;
}
bool operator> (const string & s1, const string & s2)
{
int ret = strcmp(s1.c_str(), s2.c_str());
return ret > 0;
}
bool operator<(const string& s1, const string& s2)
{
int ret = strcmp(s1.c_str(), s2.c_str());
return ret < 0;
}
bool operator!=(const string& s1, const string& s2)
{
int ret = strcmp(s1.c_str(), s2.c_str());
return !(ret == 0);
}
bool operator>=(const string& s1, const string& s2)
{
int ret = strcmp(s1.c_str(), s2.c_str());
return ret >= 0;
}
bool operator<=(const string& s1, const string& s2)
{
int ret = strcmp(s1.c_str(), s2.c_str());
return ret <= 0;
}
ostream& operator<<(ostream& out, const string& s)
{
for (auto ch : s)
{
out << ch;
}
return out;
}
istream& operator>>(istream& in, string& s)
{
s.clear();
char ch;
//in >> ch;
ch = in.get();
char buff[128];
size_t i = 0;
while (ch != ' ' && ch != '\n')
{
buff[i++] = ch;
// [0,126]
if (i == 127)
{
buff[127] = '\0';
s += buff;
i = 0;
}
ch = in.get();
}
if (i > 0)
{
buff[i] = '\0';
s += buff;
}
return in;
}
//istream& operator>>(istream& in, string& s)
//{
// s.clear();
// char ch;
// //in >> ch;
// ch = in.get();
// s.reserve(128);
// while (ch != '\n' && ch != ' ')
// {
// s += ch;
// ch = in.get();
// }
// return in;
//}
istream& getline(istream& in, string& s)
{
s.clear();
char ch;
//in >> ch;
ch = in.get();
char buff[128];
size_t i = 0;
while (ch != '\n')
{
buff[i++] = ch;
// [0,126]
if (i == 127)
{
buff[127] = '\0';
s += buff;
i = 0;
}
ch = in.get();
}
if (i > 0)
{
buff[i] = '\0';
s += buff;
}
return in;
}
//------------------------------以下是单元测试部分
void string_test1()
{
string s1("hello world");
string s2;
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
for (size_t i = 0; i < s1.size(); i++)
{
s1[i]++;
}
cout << endl;
// s1[100];
for (size_t i = 0; i < s1.size(); i++)
{
cout << s1[i] << " ";
}
cout << endl;
//const
const string s3("xxxx");
for (size_t i = 0; i < s3.size(); i++)
{
//s3[i]++;
cout << s3[i] << " ";
}
cout << endl;
// 越界检查是一种抽查
//int a[10];
// a[10];
// a[11];
// //a[10] = 1;
// a[15] = 1;
}
void string_test2()
{
//
string s3("hello world");
for (auto ch : s3)
{
cout << ch << " ";
}
cout << endl;
string::iterator it3 = s3.begin();
while (it3 != s3.end())
{
*it3 -= 1;
cout << *it3 << " ";
++it3;
}
cout << endl;
//const
const string s4("xxxx");
string::const_iterator it4 = s4.begin();
while (it4 != s4.end())
{
//*it4 += 3;
cout << *it4 << " ";
++it4;
}
cout << endl;
for (auto ch : s4)
{
cout << ch << " ";
}
cout << endl;
}
void string_test3()
{
string s3("hello world");
s3.push_back('1');
s3.push_back('2');
cout << s3.c_str() << endl;
s3 += 'x';
s3 += "yyyyyyy";
cout << s3.c_str() << endl;
s3.insert(s3.size(), "'a'");
s3.insert(0, "'a'");
cout << s3.c_str() << endl;
}
void string_test4()
{
string s3("hello world");
cout << s3.c_str() << endl;
s3.erase(5, 3);
cout << s3.c_str() << endl;
s3.erase(5);
cout << s3.c_str() << endl;
s3.erase(2, 30);
cout << s3.c_str() << endl;
}
void string_test5()
{
string s3("hello world");
cout << s3.c_str() << endl;
s3.resize(5);
cout << s3.c_str() << endl;
s3.resize(20,'x');
cout << s3.c_str() << endl;
}
void string_test6()
{
string s1("hello world");
cout << s1.c_str() << endl;
string s2(s1);
cout << s2.c_str() << endl;
s1[0] = 'x';
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
string s3("aaa");
cout << s3.c_str() << endl;
s2 = s3;
cout << s2.c_str() << endl;
s1.append("asdasd");
cout << s1.c_str() << endl;
}
void string_test7()
{
string s1("hello world");
string s2="aaaaaa";
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
s1.swap(s2);
swap(s1, s2);
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
}
void string_test8()
{
string url1("https://www.baidu.com/");
string url2 = "https://legacy.cplusplus.com/reference/";
string protocol, domain, uri;
rsize_t i1 = url1.find(':');
if (i1 != string::npos)
{
protocol = url1.substr(0, i1 - 0);
cout << protocol.c_str() << endl;
}
rsize_t i2 = url1.find('/',i1+3);
if (i2 != string::npos)
{
domain = url1.substr(i1+3, i2-(i1+3));
cout << domain.c_str() << endl;
uri = url1.substr(i2 +1);
cout << uri.c_str() << endl;
}
cout << (url1 < url2) << endl;
cout << url1<<endl;
string s2("ssad");
cin >> s2;
cout << s2 << endl;
}
}