【摘自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. layui 日期初化一个月前

    lay('.test-item').each(function(){ var d = new Date(); laydate.render({ elem: this ,max: 0 ,value: d ...

  2. fedora上安装ffmpeg

    环境 fedora26 1. 安装 yasm sudo dnf install yasm yasm-devel -y 2.安装 ffmpeg 官方下载ffmpeg源码  ( http://ffmpeg ...

  3. Bugku 杂项 啊哒

    有趣的表情包来源:第七届山东省大学生网络安全技能大赛 下载下来安装包后可以得到一张图片,010发现jpg后面还夹带着一些东西,用binwalk提取后得到一个压缩包,但是需要密码. 我卡在这里了,尝试了 ...

  4. Flask初识

    一.Flask初识 1.Flask介绍 Flask是一个使用 Python 编写的轻量级 Web 应用框架.其 WSGI 工具箱采用 Werkzeug服务 ,模板引擎则使用 Jinja2 .Flask ...

  5. VScode中python环境配置

    vscode中python环境配置 想要在vscode中运行python代码必须要告诉vscode使用哪个解释器才行 方法1. 打开命令面板(Ctrl+Shift+P)输入Python: Select ...

  6. consul如何限制注册的ip

    假设当前服务器的ip是:192.168.56.130 允许 所有ip 注册 consul agent -server -ui -bootstrap-expect=1 -data-dir=/usr/lo ...

  7. Codeforces 1037E Trips

    原题 题目大意: 有\(n\)个人,起初他们都不是朋友.总共有\(m\)天,每天会有两个人成为朋友.他们计划在晚上出去旅游,对于一个人,有如下两种情况: 1.要么他不出去旅游 2.要么有至少\(k\) ...

  8. Openresty安装及使用配置(OPENRESTY+NGINX)

    Openresty 简介 Openresty是一个基于NGINX和Lua的高性能Web平台,内部有大量的Lua库和第三方模块,能够很方便的搭建处理高并发,扩展性高的Web平台和动态网关,充分利用 Ng ...

  9. 【强大精美的PS特效滤镜合集】Alien Skin Eye Candy for Mac 7.2.2.20

    [简介] Alien Skin Eye Candy for Mac 7.2.2 版本,支持最新的PhotoShop CC 2019.2018等版本,这是一款强大酷炫的PS特效滤镜合集,具有32种滤镜和 ...

  10. 第四节:跨域请求的解决方案和WebApi特有的处理方式

    一. 简介 前言: 跨域问题发生在Javascript发起Ajax调用,其根本原因是因为浏览器对于这种请求,所给予的权限是较低的,通常只允许调用本域中的资源, 除非目标服务器明确地告知它允许跨域调用. ...