C++中string类的应用
#include <iostream>
#include<string>
using namespace std;
#include<assert.h>
namespace bite
{
class string
{
public:
typedef char* iterator;
typedef char* reverse_iterator;
string(const char* str = "")
{
if (nullptr == str)
str = "";
_size = strlen(str);
_str = new char[_size + 1];
strcpy(_str, str);
_capacity = _size;
}
string(const string& s)
: _str(nullptr)
, _size(0)
, _capacity(0)
{
string strTemp(s._str);
this->swap(strTemp);
}
string(size_t n, char ch)
{
_size = n;
_str = new char[n + 1];
memset(_str, ch, n);
_str[n] = '\0';
_capacity = n;
}
string& operator=(string s)
{
this->swap(s);
return *this;
}
~string()
{
if (_str)
{
delete[] _str;
_str = nullptr;
_size = 0;
_capacity = 0;
}
}
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
reverse_iterator rbegin()
{
return end();
}
reverse_iterator rend()
{
return begin();
}
size_t size()const
{
return _size;
}
size_t capacity()const
{
return _capacity;
}
bool empty()const
{
return 0 == _size;
}
void clear()
{
_size = 0;
_str[0] = '\0';
}
void resize(size_t newsize, char ch)
{
size_t oldsize = size();
if (newsize > oldsize)
{
size_t oldcap = capacity();
if (newsize > oldcap)
{
reserve(newsize);
}
memset(_str + _size, ch, newsize - oldsize);
}
_size = newsize;
_str[_size] = '\0';
}
void resize(size_t newsize)
{
resize(newsize, 0);
}
void reserve(size_t newcapacity)
{
size_t oldcapacity = capacity();
if (newcapacity > oldcapacity)
{
char* temp = new char[newcapacity + 1];
strcpy(temp, _str);
delete[] _str;
_str = temp;
_capacity = newcapacity;
}
}
char& operator[](size_t index)
{
assert(index < _size);
return _str[index];
}
const char& operator[](size_t index)const
{
assert(index < _size);
return _str[index];
}
void push_back(char ch)
{
if (_size == _capacity)
reserve(_capacity * 2);
_str[_size] = ch;
_size++;
_str[_size] = '\0';
}
string& operator+=(char ch)
{
push_back(ch);
return *this;
}
string& operator+=(const string& s)
{
*this += s._str;
return *this;
}
string& operator+=(const char* s)
{
size_t len = strlen(s);
char* temp = new char[_size + len + 1];
strcpy(temp, _str);
strcat(temp, s);
_size += len;
delete[] _str;
_str = temp;
_capacity = _size;
return *this;
}
const char* c_str()const
{
return _str;
}
size_t 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 rfind(char ch, size_t pos = npos)
{
pos = pos < _size ? pos : _size - 1;
for (int i = pos; i >= 0; --i)
{
if (_str[i] == ch)
return i;
}
return npos;
}
string substr(size_t pos = 0, size_t n = npos)
{
if (n == npos)
{
n = _size;
}
if (pos + n >= _size)
{
n = _size - pos;
}
char* temp = new char[n + 1];
strncpy(temp, _str + pos, n);
temp[n] = '\0';
string strRet(temp);
delete[] temp;
return strRet;
}
void swap(string& s)
{
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
friend ostream& operator<<(ostream& _cout, const string& s)
{
_cout << s._str;
return _cout;
}
private:
char* _str;
size_t _size;
size_t _capacity;
public:
static size_t npos;
};
size_t string::npos = -1;
}
void TestString1()
{
bite::string s1;
bite::string s2("hello");
bite::string s3(s2);
bite::string s4(10, 'A');
cout << s2 << endl;
for (size_t i = 0; i < s3.size(); ++i)
{
cout << s3[i];
}
cout << endl;
for (auto e : s4)
cout << e;
cout << endl;
auto it = s4.begin();
while (it != s4.end())
{
cout << *it;
++it;
}
cout << endl;
}
void TestString2()
{
bite::string s("hello");
cout << s.size() << endl;
cout << s.capacity() << endl;
s.clear();
cout << s.size() << endl;
cout << s.capacity() << endl;
if (s.empty())
{
cout << "ok" << endl;
}
else
{
cout << "error" << endl;
}
}
void TestString3()
{
bite::string s("hello");
s.reserve(10);
cout << s.size() << endl;
cout << s.capacity() << endl;
s.reserve(20);
cout << s.size() << endl;
cout << s.capacity() << endl;
s.reserve(15);
cout << s.size() << endl;
cout << s.capacity() << endl;
s.reserve(5);
cout << s.size() << endl;
cout << s.capacity() << endl;
}
void TestString4()
{
bite::string s("hello");
s.resize(10, '!');
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << s << endl;
s.resize(20, 'A');
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << s << endl;
s.resize(15);
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << s << endl;
s.resize(5);
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << s << endl;
}
void TestString5()
{
bite::string s1("hello");
s1[0] = 'H';
const bite::string s2(s1);
cout << s2[0] << endl;
}
void TestString6()
{
bite::string s("hello");
s.push_back('!');
cout << s << endl;
s += "world";
cout << s << endl;
bite::string ss("!!!");
s += ss;
cout << s << endl;
}
void TestString7()
{
bite::string s("hellohellohello");
size_t pos = 0;
while (true)
{
pos = s.find('h', pos);
if (pos == bite::string::npos)
{
break;
}
cout << pos << endl;
pos++;
}
}
void TestString8()
{
bite::string s("aaabbbbbccc.txt");
int start = s.find('b');
int end = s.rfind('b');
bite::string ret = s.substr(start, end - start + 1);
cout << ret << endl;
cout << s.substr(s.rfind('.') + 1) << endl;
}
int main()
{
TestString8();
return 0;
}