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 ...
随机推荐
- IIS部署ASP.Net Core 502.5错误和解决
在Win7的机器上部署ASP.Net Core程序,老是提示502.5错误. 已经安装了 Microsoft Visual C++ 2015 Redistributable .NET Core Win ...
- 前置通知也能对参数进行加工 通过joiPoint这个方法
- [SimplePlayer] 1. 从视频文件中提取图像
在开始之前,我们需要了解视频文件的格式.视频文件的格式众多,无法三言两语就能详细分析其结构,尽管如此,ffmpeg却很好地提取了各类视频文件的共同特性,并对其进行了抽象描述. 视频文件格式,统称为co ...
- JS判断浏览器是否支持触屏事件
var hasTouch=function(){ var touchObj={}; touchObj.isSupportTouch = "ontouchend" in docume ...
- 如何修改hosts文件
如何修改hosts文件 1.进入路径 C:\Windows\System32\drivers\etc 2.拷贝hosts文件到其他地方3.修改拷贝的hosts文件,右键用记事本打开4.直接修改或添加 ...
- 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(2 配置spring-dao和测试)
用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(1 搭建目录环境和依赖) 四:在\resources\spring 下面 ...
- 什么是Tensor
https://blog.csdn.net/kansas_lh/article/details/79321234 tensor是tensorflow基础的一个概念——张量. Tensorflow用到了 ...
- 剑指Offer_编程题_19
题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2, ...
- Springboot配置多数据源(Mysql和Orcale)--(Idea Maven JDBCTemplate支持下的)
1.配置 orcale jdbc 对于一个Maven项目,使用Mysql时,可直接添加如下依赖: <dependency> <groupId>mysql</groupId ...
- C++回顾day03---<类型转换>
一:C++类型转换 (一)static_cast<>() 静态类型转换:基本类型可以转换但是指针类型不允许.可以进行隐式类型转换 double n=1.23 int m=static_ca ...