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";
}
}
MyString的更多相关文章
- myString操作符重载
写在前面的话: 重载是C++的重要内容,在自定义一个类的时候,需要对类中的方法进行重载,才能方便的实现相应的功能,比如一些运算符,构造,析构函数,一些功能函数等等,而C++语言自带的这些东西只使用于基 ...
- 第3天作业 PoEdu MyString实现
作业要求 代码: #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstring> class My ...
- MyString(重写String)
http://wenku.baidu.com/view/d7ac113243323968011c925b.html 已知类String的原型为: class String { public: ...
- 【面试题001-补充】C++ MyString类的封装
[面试题001-补充]C++ MyString类的封装 一,C++ MyString类的封装 String.h: 123456789101112131415161718192021222324252 ...
- 在String()构造器不存在的情况下自定义一个MyString()函数,实现如下内建String()方法和属性:
在String()构造器不存在的情况下自定义一个MyString()函数,实现如下内建String()方法和属性: var s = new MyString("hello"); s ...
- mystring c++ 自己的string 封装
1 /************************************************************************* > File Name: mystrin ...
- js面向对象自定义MyString()的构造器函数,实现内建String()属性和方法:
js面向对象自定义MyString()的构造器函数,实现内建String()属性和方法: var s = new MyString('hello'); s.length; s[0]; // " ...
- 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);* ...
- error C2248: 'MyString::pCharArray' : cannot access private member declared in class 'MyString'
std::ostream & operator<<(std::ostream os,const MyString & mystr){os<<mystr.pCha ...
随机推荐
- Python 字符编码及其文件操作
本章节内容导航: 1.字符编码:人识别的语言与机器机器识别的语言转化的媒介. 2.字符与字节:字符占多少个字节,字符串转化 3.文件操作:操作硬盘中的一块区域:读写操作 注:浅拷贝与深拷贝 用法: d ...
- C++模板、.vimrc和一些Linux配置
C++模板 #include<cstdio> #include<iostream> #include<cmath> #include<cstring> ...
- Ubuntu下基于Virtualenv构建Python开发环境
1.安装virtualenv并建立虚拟环境 1).更新pip版本 sudo pip install --upgrade pip 如果出现如下异常: File , in <module> f ...
- Linux基本命令总结(六)
接上篇: 27,diff在命令行中打印每一个行的改动.最新版本的diff还支持二进制文件.diff程序的输出被称为补丁 (patch),因为Linux系统中还有一个patch程序,可以根据diff的输 ...
- Gym - 101606G Gentlebots
Rainforest Inc. is opening a large new automated warehouse in the far Northern reaches of theUK—some ...
- js上传图片压缩,并转化为base64
<input type="file" onchange="startUpload(this,'front')" id="renm"/& ...
- PowerDesigner生成pdm(适用Mysql)
废话不多说,直接开始: 1.首先安装所需要的驱动以及应用程序 ①和② 是 Mysql数据库连接驱动 ,根据PowerDesigner的位数来选择下载 下载地址:https://dev.mysql.co ...
- 【ASP.NET】website转webapplication
*以下操作都以VS2013为参考: #新建两种web项目 1.添加webapplication项目: 2.添加website项目: #比较两种web项目新建的webform页面的不同点: 1.文件目录 ...
- [物理学与PDEs]第5章习题4 广义 Hookean 定律的张量的对称性
设材料是超弹性的, 并设参考构形为自然状态, 证明由线性化得到的张量 ${\bf A}=(a_{ijkl})=\sex{2\cfrac{\p \bar p_{ij}}{c_{kl}}}$ 具有以下的对 ...
- [物理学与PDEs]第3章习题6 Lagrange 坐标下的一维理想磁流体力学方程组的数学结构
试讨论 Lagrange 形式下的一维理想磁流体力学方程组 (5. 33)-(5. 39) 的类型. 解答: 由 (5. 33), (5. 39) 知 $$\bex 0=\cfrac{\p p}{\p ...