namespace snow
{
class string
{
public:
static const size_t npos = -1;
private:
char* _str;
size_t _capacity;
size_t _size;
};
};
string(const char* str = "")
{
_size = strlen(str);
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str,str);
}
~string()
{
delete[]_str;
_str = nullptr;
_size = _capacity = 0;
}
void swap(string& s)
{
::swap(_str, s._str);
::swap(_size, s._size);
::swap(_capacity, s._capacity);
}
string(const string& s)
:_str(nullptr)
,_size(0)
,_capacity(0)
{
string tmp(s._str);
swap(tmp);
}
string& operator=(const string& s)
{
if (this != &s)
{
string tmp(s);
swap(tmp);
}
return *this;
}
void resize(size_t n, char c = '\0')
{
if (n <= _size)
{
_size = n;
}
else
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
_capacity = n;
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
}
for (_size; _size < n; ++_size)
{
_str[_size] = c;
}
}
_str[_size] = '\0';
}
void reserve(size_t n)
{
if (_capacity < n)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
const char* c_str()const
{
return _str;
}
char& operator[](size_t index)
{
assert(index < _size);
return _str[index];
}
const char& operator[](size_t index)const
{
assert(index < _size);
return _str[index];
}
string& insert(size_t pos, char c)
{
assert(pos <= _size);
if (_size == _capacity)
reserve(_capacity == 0 ? 4 : _capacity * 2);
for (size_t end = _size + 1; end > pos; --end)
_str[end] = _str[end - 1];
_str[pos] = 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);
for (size_t end = _size + len; end >= pos + len; --end)
_str[end] = _str[end - len];
strncpy(_str + pos, str, len);
_size += len;
return *this;
}
string& erase(size_t pos, size_t len = npos)
{
assert(pos < _size);
if (len == npos || pos + len >= _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
return *this;
}
void push_back(char c)
{
//if (_size == _capacity)
//{
// /*char* tmp = new char[_capacity * 2 + 1];
// _capacity = _capacity * 2;
// strcpy(tmp, _str);
// delete[] _str;
// _str = tmp;*/
// //此处可以复用reserve
// reserve(_capacity == 0 ? 4 : _capacity * 2);
//}
//_str[_size] = c;
//_str[++_size] = '\0';
//此处可以复用insert
insert(_size, c);
}
string& operator+=(char c)
{
push_back(c);
return *this;
}
void append(const char* str)
{
//size_t len = strlen(str);
//if (_size + len > _capacity)
//{
// /*char* tmp = new char[_size + len + 1];
// strcpy(tmp, _str);
// _str = tmp;
// _capacity = _size + len;*/
// //此处可以复用reserve
// reserve(_size + len);
//}
//strcpy(_str + _size, str);
//_size = _size + len;
//此处可以复用insert
insert(_size, str);
}
string& operator+=(const char* str)
{
append(str);
return *this;
}
bool operator>(const string& s)
{
return strcmp(_str, s._str) > 0;
}
bool operator==(const string& s)
{
return strcmp(_str, s._str) == 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)
{
return !(*this >= s);
}
size_t find(char c, size_t pos = 0) const
{
assert(pos < _size);
for (size_t i = pos; i < _size; ++i)
{
if (_str[i] == c)
return i;
}
return npos;
}
size_t find(const char* sub, size_t pos = 0) const
{
assert(sub);
assert(pos < _size);
const char* ptr = strstr(_str + pos, sub);
if (ptr == nullptr)
return npos;
else
return ptr - _str;
}
friend ostream& operator<<(ostream& _cout, const snow::string& s);
friend istream& operator>>(istream& _cin, snow::string& s);
ostream& operator<<(ostream& _cout, const snow::string& s)
{
for (size_t i = 0; i < s._size; ++i)
{
_cout << s[i];
}
return _cout;
}
istream& operator>>(istream& _cin, snow::string& s)
{
s.clear();
char ch = _cin.get();
const size_t N = 32;
char buff[N];
size_t i = 0;
while (ch != ' ' && ch != '\n')
{
buff[i++] = ch;
if (i == N - 1)
{
buff[i] = '\0';
s += buff;
i = 0;
}
ch = _cin.get();
}
buff[i] = '\0';
s += buff;
return _cin;
}
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include
#include
using namespace std;
namespace snow
{
class string
{
friend ostream& operator<<(ostream& _cout, const snow::string& s);
friend istream& operator>>(istream& _cin, snow::string& s);
public:
typedef char* iterator;
string(const char* str = "")
{
_size = strlen(str);
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str,str);
}
string(const string& s)
:_str(nullptr)
,_size(0)
,_capacity(0)
{
string tmp(s._str);
swap(tmp);
}
string& operator=(const string& s)
{
if (this != &s)
{
string tmp(s);
swap(tmp);
}
return *this;
}
~string()
{
delete[]_str;
_str = nullptr;
_size = _capacity = 0;
}
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
void push_back(char c)
{
//if (_size == _capacity)
//{
// /*char* tmp = new char[_capacity * 2 + 1];
// _capacity = _capacity * 2;
// strcpy(tmp, _str);
// delete[] _str;
// _str = tmp;*/
// //此处可以复用reserve
// reserve(_capacity == 0 ? 4 : _capacity * 2);
//}
//_str[_size] = c;
//_str[++_size] = '\0';
//此处可以复用insert
insert(_size, c);
}
string& operator+=(char c)
{
push_back(c);
return *this;
}
void append(const char* str)
{
//size_t len = strlen(str);
//if (_size + len > _capacity)
//{
// /*char* tmp = new char[_size + len + 1];
// strcpy(tmp, _str);
// _str = tmp;
// _capacity = _size + len;*/
// //此处可以复用reserve
// reserve(_size + len);
//}
//strcpy(_str + _size, str);
//_size = _size + len;
//此处可以复用insert
insert(_size, str);
}
string& operator+=(const char* str)
{
append(str);
return *this;
}
void clear()
{
_size = 0;
_str[_size] = '\0';
}
void swap(string& s)
{
::swap(_str, s._str);
::swap(_size, s._size);
::swap(_capacity, s._capacity);
}
const char* c_str()const
{
return _str;
}
size_t size()const
{
return _size;
}
size_t capacity()const
{
return _capacity;
}
bool empty()const
{
return _size == 0;
}
void resize(size_t n, char c = '\0')
{
if (n <= _size)
{
_size = n;
}
else
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
_capacity = n;
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
}
for (_size; _size < n; ++_size)
{
_str[_size] = c;
}
}
_str[_size] = '\0';
}
void reserve(size_t n)
{
if (_capacity < n)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
char& operator[](size_t index)
{
assert(index < _size);
return _str[index];
}
const char& operator[](size_t index)const
{
assert(index < _size);
return _str[index];
}
bool operator>(const string& s)
{
return strcmp(_str, s._str) > 0;
}
bool operator==(const string& s)
{
return strcmp(_str, s._str) == 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)
{
return !(*this >= s);
}
size_t find(char c, size_t pos = 0) const
{
assert(pos < _size);
for (size_t i = pos; i < _size; ++i)
{
if (_str[i] == c)
return i;
}
return npos;
}
size_t find(const char* sub, size_t pos = 0) const
{
assert(sub);
assert(pos < _size);
const char* ptr = strstr(_str + pos, sub);
if (ptr == nullptr)
return npos;
else
return ptr - _str;
}
string& insert(size_t pos, char c)
{
assert(pos <= _size);
if (_size == _capacity)
reserve(_capacity == 0 ? 4 : _capacity * 2);
//reserve(_capacity * 2);
for (size_t end = _size + 1; end > pos; --end)
_str[end] = _str[end - 1];
_str[pos] = 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);
for (size_t end = _size + len; end >= pos + len; --end)
_str[end] = _str[end - len];
strncpy(_str + pos, str, len);
_size += len;
return *this;
}
string& erase(size_t pos, size_t len = npos)
{
assert(pos < _size);
if (len == npos || pos + len >= _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
return *this;
}
static const size_t npos = -1;
private:
char* _str;
size_t _capacity;
size_t _size;
};
ostream& operator<<(ostream& _cout, const snow::string& s)
{
for (size_t i = 0; i < s._size; ++i)
{
_cout << s[i];
}
return _cout;
}
istream& operator>>(istream& _cin, snow::string& s)
{
s.clear();
char ch = _cin.get();
const size_t N = 32;
char buff[N];
size_t i = 0;
while (ch != ' ' && ch != '\n')
{
buff[i++] = ch;
if (i == N - 1)
{
buff[i] = '\0';
s += buff;
i = 0;
}
ch = _cin.get();
}
buff[i] = '\0';
s += buff;
return _cin;
}
//测试函数需写于命名空间内才能调试进来
void Test_string1()
{
string s1;
string s2("hello");
cout << s1 << endl;
cout << s2 << endl;
string s3(s2);
cout << s3 << endl;
s1 = s3;
cout << s1 << endl;
s2 = "snow";
for (auto it = s2.begin(); it < s2.end(); ++it)
{
cout << *it;
}
}
void Test_string2()
{
string s1("snow");
cout << s1 << endl;
cout << s1.size() << endl;
cout << s1.capacity() << endl;
/*s1.push_back('!');
cout << s1 << endl;*/
s1 += '!';
cout << s1 << endl;
cout << s1.size() << endl;
cout << s1.capacity() << endl;
/*s1.append("dragon!");
cout << s1 << endl;*/
s1 += "dragon!";
cout << s1 << endl;
s1.clear();
cout << s1 << endl;
}
void Test_string3()
{
string s1("snow");
s1 += '\0';
s1 += "dragon";
cout << s1.c_str() << endl;
cout << s1 << endl;
cout << s1.empty() << endl;
std::string s2;
cout << s2.empty() << endl;
string s3;
cout << s3.empty() << endl;
}
void Test_string4()
{
string s1("snow");
cout << s1 << endl;
s1.resize(10, '!');
cout << s1 << endl;
s1.resize(3);
cout << s1 << endl;
string s2("dragon");
cout << s2.capacity() << endl;
s2.reserve(18);
cout << s2.capacity() << endl;
}
void Test_string5()
{
string s1("snowdragon");
//std::string s1("snowdragon");
cout << s1.find('a', 0) << endl;
cout << s1.find('a') << endl;
cout << s1.find('a', 20) << endl;
cout << s1.find('l', 0) << endl;
cout << s1.find("dra", 0) << endl;
cout << s1.find("dra") << endl;
cout << s1.find("dra", 20) << endl;
cout << s1.find("sd", 0) << endl;
}
void Test_string6()
{
string s1("snowdragon");
cout << s1 << endl;
s1.insert(4, 'h');
cout << s1 << endl;
s1.insert(0, 'a');
cout << s1 << endl;
s1.insert(12, 'z');
cout << s1 << endl;
cout << "**************************" << endl;
string s2("snow");
//std::string s2("snow");
cout << s2 << endl;
s2.insert(4, "dragon");
cout << s2 << endl;
s2.insert(0, "hello");
cout << s2 << endl;
s2.insert(5, ",i am ");
cout << s2 << endl;
/*s2.insert(30, "hello");
cout << s2 << endl;*/
cout << "**************************" << endl;
s2.erase(10, 11);
cout << s2 << endl;
s2.erase(0, 6);
cout << s2 << endl;
s2.erase(2, 1);
cout << s2 << endl;
}
void Test_string7()
{
string s1, s2;
//std::string s1, s2;
cin >> s1 >> s2;
cout << s1 << " " << s2 << endl;
}
};
本文到这里就结束了,如有错误或者不清楚的地方欢迎评论或者私信
创作不易,如果觉得博主写得不错,请务必点赞、收藏加关注