经常调用的短小函数直接定义在头文件中,可以节省时间开销
#include<iostream>
#include<assert.h>
using namespace std;
namespace mumu
{
class string
{
friend ostream& operator<<(ostream& _cout, const mumu::string& s);
friend istream& operator>>(istream& _cin, mumu::string& s);
public:
typedef char* iterator;
typedef const char* const_iterator;
string(const char* str = "")
{
_size = strlen(str);
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str, str);
}
string(const string& s)
{
_size = s.size();
_capacity = s._capacity;
_str = new char[_capacity + 1];
strcpy(_str, s._str);
}
string& operator=(const string& s)
{
if (*this == s)
{
return *this;
}
_size = s._size;
_capacity = s._capacity;
_str = new char[_capacity + 1];
strcpy(_str, s._str);
return *this;
}
~string()
{
if (_str)
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
}
// iterator
iterator begin()
{
return _str;
}
const_iterator begin() const
{
return _str;
}
iterator end()
{
return _str + _size;
}
const_iterator end() const
{
return _str + _size;
}
// modify
void push_back(char c);
string& operator+=(char c);
void append(const char* str);
string& operator+=(const char* str);
void clear()
{
_str[0] = '\0';
_size = 0;
}
void swap(string& s)
{
string tmp;
tmp = *this;
*this = s;
s = tmp;
}
const char* c_str()const
{
return _str;
}
// capacity
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');
void reserve(size_t n);
// access
char& operator[](size_t index)
{
assert(index<_size);
return _str[index];
}
const char& operator[](size_t index)const
{
assert(index < _size);
return _str[index];
}
//relational operators
bool operator<(const string& s);
bool operator<=(const string& s);
bool operator>(const string& s);
bool operator>=(const string& s);
bool operator==(const string& s);
bool operator!=(const string& s);
// 返回c在string中第一次出现的位置
size_t find(char c, size_t pos = 0) const;
// 返回子串s在string中第一次出现的位置
size_t find(const char* s, size_t pos = 0) const;
// 在pos位置上插入字符c/字符串str,并返回该字符的位置
string& insert(size_t pos, char c);
string& insert(size_t pos, const char* str);
// 删除pos位置上的元素,并返回该元素的下一个位置
string& erase(size_t pos, size_t len = npos);
string substr(size_t pos=0, size_t len = npos);
private:
char* _str = nullptr;
size_t _capacity = 0;
size_t _size = 0;
static const size_t npos;
};
};
string模拟实现的源代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"string.h"
namespace mumu
{
const size_t string::npos = -1;
ostream& operator<<(ostream& _cout, const mumu::string& s)
{
for (auto ch : s)
{
_cout << ch;
}
return _cout;
}
istream& operator>>(istream& _cin, mumu::string& s)
{
char ch;
ch = _cin.get();
while (ch != ' ' && ch != '\n')
{
s += ch;
ch = _cin.get();
}
return _cin;
}
void string::push_back(char c)
{
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
_str[_size] = c;
_str[++_size] = '\0';
}
string& string::operator+=(char c)
{
push_back(c);
return *this;
}
void string::append(const char* str)
{
size_t length = strlen(str);
if (_size + length > _capacity)
{
reserve(_size + length > 2 * _capacity ? _size + length : _capacity * 2);
}
strcpy(_str + _size, str);
_size += length;
}
string& string::operator+=(const char* str)
{
append(str);
return *this;
}
void string::resize(size_t n, char c )
{
assert(n <= _size);
_str[n] = c;
if (c == '\0')
{
_size = n;
}
}
void string::reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
bool string::operator<(const string& s)
{
return !(*this >= s);
}
bool string::operator<=(const string& s)
{
return !(*this > s);
}
bool string::operator>(const string& s)
{
return strcmp(this->c_str(), s.c_str()) > 0;
}
bool string::operator>=(const string& s)
{
return (*this > s) || (*this == s);
}
bool string::operator==(const string& s)
{
return strcmp(this->c_str(), s.c_str())==0;
}
bool string::operator!=(const string& s)
{
return !(*this == s);
}
// 返回c在string中第一次出现的位置
size_t string::find(char c, size_t pos) const
{
assert(pos < _size);
for (size_t i = pos; i < _size; i++)
{
if (_str[i] == c)
{
return i;
}
}
return npos;
}
// 返回子串s在string中第一次出现的位置
size_t string::find(const char* s, size_t pos) const
{
assert(pos < _size);
const char* ptr = strstr(_str + pos, s);
if (ptr == nullptr)
{
return npos;
}
else
{
return ptr - _str;
}
}
// 在pos位置上插入字符c/字符串str,并返回该字符的位置
string& string::insert(size_t pos, char c)
{
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] = c;
++_size;
return *this;
}
string& string::insert(size_t pos, const char* str)
{
assert(pos <= _size);
size_t length = strlen(str);
if (_size + length > _capacity)
{
reserve(_size + length > 2 * _capacity ? _size + length : 2 * _capacity);
}
size_t end = _size + length;
while (end > pos + length - 1)
{
_str[end] = _str[end - length];
--end;
}
for (size_t i = 0; i < length; i++)
{
_str[pos + i] = str[i];
}
_size += length;
return *this;
}
// 删除pos位置上的元素,并返回该元素的下一个位置
string& string::erase(size_t pos, size_t len)
{
assert(pos < _size);
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;
}
return *this;
}
string string::substr(size_t pos, size_t len)
{
assert(pos < _size);
// len大于剩余字符长度,更新一下len
if (len > _size - pos)
{
len = _size - pos;
}
string sub;
sub.reserve(len);
for (size_t i = 0; i < len; i++)
{
sub += _str[pos + i];
}
return sub;
}
};
测试用主函数源代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"string.h"
using namespace mumu;
void test_string1()
{
mumu::string s1;
mumu::string s2("hello world");
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
for (size_t i = 0; i < s2.size(); i++)
{
s2[i] += 2;
}
cout << s2.c_str() << endl;
for (auto e : s2)
{
cout << e << " ";
}
cout << endl;
mumu::string::iterator it = s2.begin();
while (it != s2.end())
{
//*it += 2;
cout << *it << " ";
++it;
}
cout << endl;
}
void test_string2()
{
mumu::string s1("hello world");
s1 += 'x';
s1 += '#';
cout << s1.c_str() << endl;
s1 += "hello bit";
cout << s1.c_str() << endl;
s1.insert(5, '$');
cout << s1.c_str() << endl;
s1.insert(0, '$');
cout << s1.c_str() << endl;
mumu::string s2("hello world");
cout << s2.c_str() << endl;
s2.insert(5, "$$$");
cout << s2.c_str() << endl;
s2.insert(0, "$$$&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
cout << s2.c_str() << endl;
}
void test_string3()
{
mumu::string s1("hello world");
s1.erase(6, 100);
cout << s1.c_str() << endl;
mumu::string s2("hello world");
s2.erase(6);
cout << s2.c_str() << endl;
mumu::string s3("hello world");
s3.erase(6, 3);
cout << s3.c_str() << endl;
}
void test_string4()
{
mumu::string s("test.cpp.zip");
size_t pos = s.find('.');
mumu::string suffix = s.substr(pos);
cout << suffix.c_str() << endl;
mumu::string copy(s);
cout << copy.c_str() << endl;
s = suffix;
cout << suffix.c_str() << endl;
cout << s.c_str() << endl;
s = s;
cout << s.c_str() << endl;
}
void test_string5()
{
mumu::string s1("hello world");
mumu::string s2("hello world");
cout << (s1 < s2) << endl;
cout << (s1 == s2) << endl;
//cout << ("hello world" < s2) << endl;
cout << (s1 == "hello world") << endl;
//cout << ("hello world" == "hello world") << endl;
cout << s1 << s2 << endl;
mumu::string s0;
cin >> s0;
cout << s0 << endl;
}
void test_string6()
{
mumu::string s1("hello world");
mumu::string s2 = s1;
cout << s1 << endl;
cout << s2 << endl;
mumu::string s3("xxxxxxxxxxxxxx");
s1 = s3;
cout << s1 << endl;
cout << s3 << endl;
}
void test_string7()
{
mumu::string s1("hello world");
mumu::string s2("xxxxxxxxxxxxxxxxxxxxxxxx");
std::swap(s1, s2);
s1.swap(s2);
}
int main()
{
test_string7();
return 0;
}