【摘自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";
}
}

MyString的更多相关文章

  1. myString操作符重载

    写在前面的话: 重载是C++的重要内容,在自定义一个类的时候,需要对类中的方法进行重载,才能方便的实现相应的功能,比如一些运算符,构造,析构函数,一些功能函数等等,而C++语言自带的这些东西只使用于基 ...

  2. 第3天作业 PoEdu MyString实现

    作业要求 代码: #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstring> class My ...

  3. MyString(重写String)

    http://wenku.baidu.com/view/d7ac113243323968011c925b.html 已知类String的原型为: class String  { public:     ...

  4. 【面试题001-补充】C++ MyString类的封装

    [面试题001-补充]C++ MyString类的封装  一,C++ MyString类的封装 String.h: 123456789101112131415161718192021222324252 ...

  5. 在String()构造器不存在的情况下自定义一个MyString()函数,实现如下内建String()方法和属性:

    在String()构造器不存在的情况下自定义一个MyString()函数,实现如下内建String()方法和属性: var s = new MyString("hello"); s ...

  6. mystring c++ 自己的string 封装

    1 /************************************************************************* > File Name: mystrin ...

  7. js面向对象自定义MyString()的构造器函数,实现内建String()属性和方法:

    js面向对象自定义MyString()的构造器函数,实现内建String()属性和方法: var s = new MyString('hello'); s.length; s[0]; // " ...

  8. error C2556: 'const char &MyString::operator [](int)' : overloaded function differs only by return type from 'char &MyString::operator [](int)'

    char & operator[](int i);const char & operator[](int i);/*const char & operator(int i);* ...

  9. error C2248: 'MyString::pCharArray' : cannot access private member declared in class 'MyString'

    std::ostream & operator<<(std::ostream os,const MyString & mystr){os<<mystr.pCha ...

随机推荐

  1. Django之ContentType组件

    一.理想表结构设计 1.初始构建 1. 场景刚过去的双12,很多电商平台都会对他们的商品进行打折促销活动的,那么我们如果要实现这样的一个场景,改如何设计我们的表? 2. 初始表设计 注释很重要,看看吧 ...

  2. Java爬取12306余票

    一.前言 今年国庆和中秋一起放,虽然很欢快,但是没有票了!!! 于是自己倒腾了一个查询余票的小程序. 二.准备工作 1.先打开12306的页面 2.然后右键检查,点network 3.再点一下1230 ...

  3. mpvue——小程序修改input的placehold样式

    前言 官方地址 https://developers.weixin.qq.com/miniprogram/dev/component/input.html 用placeholder-class的时候没 ...

  4. webpack学习记录-初步体验(一)

    一.关于webpack 自从出现模块化以后,大家可以将原本一坨代码分离到个个模块中,但是由此引发了一个问题.每个 JS 文件都需要从服务器去拿,由此会导致加载速度变慢.Webpack 最主要的目的就是 ...

  5. Shell入门及实践

    解释器 解释器是一种命令解释器,主要作用是对命令进行运行和解释,将需要执行的操作传递给操作系统内核并执行 #!/bin/bash(默认),指定解释器 #!/bin/bash #这是第一个shell脚本 ...

  6. centos6/7安装java和maven

    下载安装包并解压到相关目录即可 编辑环境变量vim /etc/profile.d/maven.sh export JAVA_HOME=/app/soft/java-1.8.0_181 export J ...

  7. 给react-native添加图标和启动屏

    react native 项目默认是没有图标,并且启动页面只有文字.这个样子并不能算是一个完整的APP,现在就给APP加一个图标和一个适应所有屏幕尺寸的启动图,并且设置启动图遮住项目启动时候的白色闪屏 ...

  8. secureCRT自动断开的解决方法

    转: secureCRT自动断开的解决方法 secureCRT自动断开的解决方法 在secureCRT上登录时,一段时间不用的话会自动断开,必须重新连接,有点麻烦. 有时候服务器端的 /etc/pro ...

  9. HTML(八)HTML meta标签&base标签

    HTML meta元素 标签(meta-information)用于提供页面有关的元数据,除了提供文档字符集.使用语言.作者等基本信息外,还涉及对关键词和网页等级的设定.通过设置不同的属性,元数据可以 ...

  10. mui弹出菜单

    详细操作见代码: <!doctype html> <html> <head> <meta charset="UTF-8"> < ...