全版本string
#pragma once
#include <iostream>
#include <cstring>
#include <assert.h>
#include <list>
using namespace std;
namespace king
{
class string
{
public:
const static size_t npos = -1;
private:
char *_str;
size_t _size;
size_t _capacity;
public:
string(const char *str = "")
{
_size = strlen(str);
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str, str);
}
string(const string &s)
: _str(new char[s._capacity + 1]), _size(s._size), _capacity(s._capacity)
{
strcpy(_str, s._str);
}
void swap(string &t)
{
::swap(_str, t._str);
::swap(_size, t._size);
::swap(_capacity, t._capacity);
}
string(const string &s)
: _str(nullptr), _size(0), _capacity(0)
{
string t(s._str);
swap(t);
}
~string()
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
string &operator=(const string &s)
{
if (this != &s)
{
char *tmp = new char[s._capacity + 1];
delete[] _str;
strcpy(tmp, s._str);
_str = tmp;
_size = s._size;
_capacity = s._capacity;
}
return *this;
}
string &operator=(const string &s)
{
if (this != &s)
{
string tmp(s);
swap(tmp);
}
return *this;
}
string &operator=(string s)
{
swap(s);
return *this;
}
string(string &&s) noexcept
: _str(s._str), _size(s._size), _capacity(s._capacity)
{
s._str = nullptr;
}
string &operator=(string &&s) noexcept
{
delete[] _str;
_str = s._str;
_size = s._size;
_capacity = s._capacity;
s._str = nullptr;
return *this;
}
const char *c_str() const
{
return _str;
}
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];
}
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 &insert(size_t pos, char ch)
{
assert(pos <= _size);
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
size_t end = _size + 1;
while (end > pos)
{
_str[end] = _str[end - 1];
--end;
}
_str[pos] = ch;
++_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);
}
size_t end = _size + len;
while (end >= pos + len)
{
_str[end] = _str[end - len];
--end;
}
strncpy(_str + pos, str, len);
_size += len;
return *this;
}
void 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;
}
}
void reserve(size_t n)
{
if (n > _capacity)
{
char *tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
void resize(size_t n, char ch = '\0')
{
if (n > _size)
{
reserve(n);
for (size_t i = _size; i < n; ++i)
{
_str[i] = ch;
}
_str[n] = '\0';
_size = n;
}
else
{
_str[n] = '\0';
_size = n;
}
}
void push_back(char ch)
{
insert(_size, ch);
}
void append(const char *str)
{
insert(_size, str);
}
void append(const string &s)
{
append(s._str);
}
void append(size_t n, char ch)
{
reserve(_size + n);
for (size_t i = 0; i < n; ++i)
{
push_back(ch);
}
}
string &operator+=(char ch)
{
push_back(ch);
return *this;
}
string &operator+=(const char *str)
{
append(str);
return *this;
}
void clear()
{
_str[0] = '\0';
_size = 0;
}
size_t find(char ch, size_t pos = 0) const
{
assert(pos < _size);
for (size_t i = pos; i < _size; ++i)
{
if (ch == _str[i])
{
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 substr(size_t pos, size_t len = npos) const
{
assert(pos < _size);
size_t realLen = len;
if (len == npos || pos + len > _size)
{
realLen = _size - pos;
}
string sub;
for (size_t i = 0; i < realLen; ++i)
{
sub += _str[pos + i];
}
return sub;
}
bool operator>(const string &s) const
{
return strcmp(_str, s._str) > 0;
}
bool operator==(const string &s) const
{
return strcmp(_str, s._str) == 0;
}
bool operator>=(const string &s) const
{
return *this > s || *this == s;
}
bool operator<=(const string &s) const
{
return !(*this > s);
}
bool operator<(const string &s) const
{
return !(*this >= s);
}
bool operator!=(const string &s) const
{
return !(*this == s);
}
};
ostream &operator<<(ostream &out, const string &s)
{
for (size_t i = 0; i < s.size(); ++i)
out << s[i];
return out;
}
istream &operator>>(istream &in, string &s)
{
s.clear();
char ch;
ch = in.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 = in.get();
}
buff[i] = '\0';
s += buff;
return in;
}
}
精简版string
#pragma once
#include <iostream>
#include <cstring>
#include <assert.h>
#include <list>
using namespace std;
namespace king
{
class string
{
public:
const static size_t npos = -1;
void swap(string &t)
{
::swap(_str, t._str);
::swap(_size, t._size);
::swap(_capacity, t._capacity);
}
private:
char *_str;
size_t _size;
size_t _capacity;
public:
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;
}
string(const string &s)
: _str(nullptr), _size(0), _capacity(0)
{
string t(s._str);
swap(t);
}
string(string &&s) noexcept
: _str(s._str), _size(s._size), _capacity(s._capacity)
{
s._str = nullptr;
}
string &operator=(string s)
{
swap(s);
return *this;
}
string &operator=(string &&s) noexcept
{
delete[] _str;
_str = s._str;
_size = s._size;
_capacity = s._capacity;
s._str = nullptr;
return *this;
}
const char *c_str() const
{
return _str;
}
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];
}
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 &insert(size_t pos, char ch)
{
assert(pos <= _size);
if (_size == _capacity)
reserve(_capacity == 0 ? 4 : _capacity * 2);
size_t end = _size + 1;
while (end > pos)
{
_str[end] = _str[end - 1];
--end;
}
_str[pos] = ch;
++_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);
}
size_t end = _size + len;
while (end >= pos + len)
{
_str[end] = _str[end - len];
--end;
}
strncpy(_str + pos, str, len);
_size += len;
return *this;
}
void 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;
}
}
void reserve(size_t n)
{
if (n > _capacity)
{
char *tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
void resize(size_t n, char ch = '\0')
{
if (n > _size)
{
reserve(n);
for (size_t i = _size; i < n; ++i)
{
_str[i] = ch;
}
_str[n] = '\0';
_size = n;
}
else
{
_str[n] = '\0';
_size = n;
}
}
void push_back(char ch)
{
insert(_size, ch);
}
void append(const char *str)
{
insert(_size, str);
}
void append(const string &s)
{
append(s._str);
}
void append(size_t n, char ch)
{
reserve(_size + n);
for (size_t i = 0; i < n; ++i)
{
push_back(ch);
}
}
string &operator+=(char ch)
{
push_back(ch);
return *this;
}
string &operator+=(const char *str)
{
append(str);
return *this;
}
void clear()
{
_str[0] = '\0';
_size = 0;
}
size_t find(char ch, size_t pos = 0) const
{
assert(pos < _size);
for (size_t i = pos; i < _size; ++i)
{
if (ch == _str[i])
{
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 substr(size_t pos, size_t len = npos) const
{
assert(pos < _size);
size_t realLen = len;
if (len == npos || pos + len > _size)
{
realLen = _size - pos;
}
string sub;
for (size_t i = 0; i < realLen; ++i)
{
sub += _str[pos + i];
}
return sub;
}
bool operator>(const string &s) const
{
return strcmp(_str, s._str) > 0;
}
bool operator==(const string &s) const
{
return strcmp(_str, s._str) == 0;
}
bool operator>=(const string &s) const
{
return *this > s || *this == s;
}
bool operator<=(const string &s) const
{
return !(*this > s);
}
bool operator<(const string &s) const
{
return !(*this >= s);
}
bool operator!=(const string &s) const
{
return !(*this == s);
}
};
ostream &operator<<(ostream &out, const string &s)
{
for (size_t i = 0; i < s.size(); ++i)
out << s[i];
return out;
}
istream &operator>>(istream &in, string &s)
{
s.clear();
char ch;
ch = in.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 = in.get();
}
buff[i] = '\0';
s += buff;
return in;
}
}
测试函数
#include "mini_string.hpp"
namespace king
{
void test_string1()
{
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)
{
cout << s1[i] << " ";
}
cout << endl;
for (size_t i = 0; i < s1.size(); ++i)
{
s1[i]++;
cout << s1[i] << " ";
}
}
void test_string2()
{
string s1("hello world");
string::iterator it = s1.begin();
while (it != s1.end())
{
*it += 1;
cout << *it << " ";
++it;
}
cout << endl;
for (auto ch : s1)
{
cout << ch << " ";
}
cout << endl;
}
void test_string3()
{
string s1("hello world");
string s2(s1);
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
s2[0] = 'x';
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
string s3("111111111111111111111111111111");
s1 = s3;
cout << s1.c_str() << endl;
cout << s3.c_str() << endl;
s1 = s1;
cout << s1.c_str() << endl;
cout << s3.c_str() << endl;
}
void test_string5()
{
string s1("hello");
cout << s1.c_str() << endl;
s1.push_back('x');
cout << s1.c_str() << endl;
cout << s1.capacity() << endl;
s1 += 'y';
s1 += 'z';
s1 += 'z';
s1 += 'z';
s1 += 'z';
s1 += 'z';
s1 += 'z';
cout << s1.c_str() << endl;
cout << s1.capacity() << endl;
}
void test_string6()
{
string s1("hello");
cout << s1.c_str() << endl;
s1 += ' ';
s1.append("world");
s1 += "bit hello";
cout << s1.c_str() << endl;
}
void test_string7()
{
string s1("hello");
cout << s1.c_str() << endl;
s1.insert(5, '#');
cout << s1.c_str() << endl;
s1.insert(0, '#');
cout << s1.c_str() << endl;
s1.insert(2, "world");
cout << s1.c_str() << endl;
s1.insert(0, "world ");
cout << s1.c_str() << endl;
}
void test_string8()
{
string s1("hello");
s1.erase(1, 10);
cout << s1.c_str() << endl;
string s2("hello");
s2.erase(1);
cout << s2.c_str() << endl;
string s3("hello");
s3.erase(1, 2);
cout << s3.c_str() << endl;
}
void test_string9()
{
string s1("hello");
cout << s1 << endl;
cout << s1.c_str() << endl;
s1 += '\0';
s1 += "world";
cout << s1 << endl;
cout << s1.c_str() << endl;
string s3, s4;
cin >> s3 >> s4;
cout << s3 << s4 << endl;
}
void DealUrl(const string &url)
{
size_t pos1 = url.find("://");
if (pos1 == string::npos)
{
cout << "非法url" << endl;
return;
}
string protocol = url.substr(0, pos1);
cout << protocol << endl;
size_t pos2 = url.find('/', pos1 + 3);
if (pos2 == string::npos)
{
cout << "非法url" << endl;
return;
}
string domain = url.substr(pos1 + 3, pos2 - pos1 - 3);
cout << domain << endl;
string uri = url.substr(pos2 + 1);
cout << uri << endl
<< endl;
}
void test_string10()
{
string url1 = "https://cplusplus.com/reference/string/string/";
string url2 = "ftp://ftp.cs.umd.edu/pub/skipLists/skiplists.pdf";
DealUrl(url1);
DealUrl(url2);
}
void test_string11()
{
string s1;
s1.resize(20);
string s2("hello");
s2.resize(20, 'x');
s2.resize(10);
}
void test_string12()
{
std::string s0;
std::string s1("111111");
std::string s2("11111111111111111111111111111111111111111");
cout << sizeof(s0) << endl;
cout << sizeof(s1) << endl;
cout << sizeof(s2) << endl;
}
}
void test_string4()
{
string s1("hello world");
string s2("xxxxxxx");
s1.swap(s2);
std::swap(s1, s2);
::swap(s1, s2);
}
int main()
{
king::test_string1();
return 0;
}