MyString

【摘自C++程序设计语言】

MyString.h

 #include <cstring>
#include <iostream>
#include <stdexcept> #ifndef _MYSTRING
#define _MYSTRING class MyString
{
public:
MyString();
MyString(const char* p); MyString(const MyString& x);
MyString& operator=(const MyString& x); MyString(MyString&& x);
MyString& operator=(MyString&& x); ~MyString()
{
if (short_max < sz)
delete[] ptr;
} char& operator[](int n) { return ptr[n]; }
char operator[](int n) const { return ptr[n]; } char& at(int n) { check(n); return ptr[n]; }
char at(int n) const { check(n); return ptr[n]; } MyString& operator+=(char c); const char* c_str() { return ptr; }
const char* c_str() const { return ptr; } int size() const { return sz; }
int capacity() const { return (sz <= short_max) ? short_max : sz + space; } private:
void check(int n) const
{
if (n < || sz <= n)
throw std::out_of_range("String::at()");
} char* expand(const char* ptr, int n)
{
char* p = new char[n];
strcpy(p, ptr);
return p;
} void copy_from(const MyString& x);
void move_from(MyString& x);
private:
static const int short_max = ;
unsigned int sz;
char* ptr;
union {
int space;
char ch[short_max+];
};
}; std::ostream& operator<<(std::ostream& os, const MyString& s);
std::istream& operator>>(std::istream& is, MyString& s);
bool operator==(const MyString& a, const MyString& b);
bool operator!=(const MyString& a, const MyString& b);
char* begin(MyString& x);
char* end(MyString& x);
const char* begin(const MyString& x);
const char* end(const MyString& x);
MyString& operator+=(MyString& a, const MyString& b);
MyString operator+(const MyString& a, const MyString b); #endif

MyString.cpp

 #include "MyString.h"
using namespace std; MyString::MyString()
: sz{}, ptr{ch}
{
ch[] = ;
} MyString::MyString(const char* p)
: sz{strlen(p)},
ptr{(sz <= short_max) ? ch : new char[sz + ]},
space{}
{
strcpy(ptr, p);
} MyString::MyString(const MyString& x)
{
copy_from(x);
} MyString& MyString::operator=(const MyString& x)
{
if (this == &x) return *this; char* p = (short_max < sz) ? ptr : ;
copy_from(x);
delete[] p;
return *this;
} MyString::MyString(MyString&& x)
{
move_from(x);
} MyString& MyString::operator=(MyString&& x)
{
if (this == &x) return *this; if (short_max < sz) delete[] ptr;
move_from(x);
return *this;
} MyString& MyString::operator+=(char c)
{
if (sz == short_max) {
int n = sz + sz + ;
ptr = expand(ptr, n);
space = n - sz - ;
}
else if (short_max < sz) {
if (space == ) {
int n = sz + sz + ;
char* p = expand(ptr, n);
delete[] ptr;
ptr = p;
space = n - sz - ;
}
else {
--space;
}
}
ptr[sz] = c;
ptr[++sz] = ; return *this;
} void MyString::copy_from(const MyString& x)
{
if (x.sz <= short_max) {
memcpy(this, &x, sizeof(x));
ptr = ch;
}
else {
ptr = expand(x.ptr, sz + );
sz = x.sz;
space = ;
}
} void MyString::move_from(MyString& x)
{
if (x.sz <= short_max) {
memcpy(this, &x, sizeof(x));
ptr = ch;
}
else {
ptr = x.ptr;
sz = x.sz;
space = x.space;
x.ptr = x.ch;
x.sz = ;
x.ch[] = ;
}
} ostream& operator<<(ostream& os, const MyString& s)
{
os << s.c_str();
return os;
} istream& operator>>(istream& is, MyString& s)
{
s = "";
is >> ws;
char ch = ' ';
while (is.get(ch) && !isspace(ch)) {
s += ch;
}
return is;
} bool operator==(const MyString& a, const MyString& b)
{
if (a.size() != b.size())
return false;
for (int i = ; i != a.size(); ++i) {
if (a[i] != b[i])
return false;
}
return true;
} bool operator!=(const MyString& a, const MyString& b)
{
return !(a == b);
} char* begin(MyString& x)
{
return (char*)x.c_str();
} char* end(MyString& x)
{
return (char*)(x.c_str() + x.size());
} const char* begin(const MyString& x)
{
return x.c_str();
} const char* end(const MyString& x)
{
return x.c_str() + x.size();
} MyString& operator+=(MyString& a, const MyString& b)
{
for (auto x : b) {
a += x;
}
return a;
} MyString operator+(const MyString& a, const MyString b)
{
MyString res{b};
res += b;
return res;
}

Test.cpp

 #include <iostream>
#include "MyString.h"
using namespace std; int main()
{
MyString s("abcdefghij");
cout << s << "\n";
s += 'k';
s += 'l';
s += 'm';
s += 'n';
cout << s << "\n"; MyString s2 = "Hell";
s2 += " and high water";
cout << s2 << "\n"; MyString s3 = "qwerty";
s3 = s3;
MyString s4 = "the quick brown fox jumped over the lazy dog";
s4 = s4;
cout << s3 << " " << s4 << "\n"; cout << s + "." + s3 + MyString(".") + "Horsefeathers\n"; MyString buf;
while (cin >> buf && buf != "quit") {
cout << buf << " " << buf.size() << " " << buf.capacity() << "\n";
}
}
上一篇:JS实现数组去重方法总结(三种常用方法)


下一篇:使用太过简单jqprint源码也极其简洁易懂