1 /*************************************************************************
> File Name: mystring.h
> Author: lukey
> Mail: lukey123@foxmail.com
> Created Time: Wed 17 Jun 2015 08:50:49 PM CST
************************************************************************/ #ifndef __MYSTRING__
#define __MYSTRING__ class String
{
public:
String();
String(const char *);//有参构造函数
String(const String & rhs); //复制构造
~String(); String & operator=(const String & rhs);//赋值运算符的两种情况
String & operator=(const char *str); String & operator+=(const String & rhs);
String & operator+=(const char * str); char & operator[](std::size_t index);
const char & operator[](std::size_t index) const; std::size_t size() const;
const char* c_str() const;
void debug(); //String 类和char相加的几个情况
friend String operator+(const String & s1, const String & s2);
friend String operator+(const String &, const char *);
friend String operator+(const char *, const String &); friend bool operator==(const String &, const String &);
friend bool operator!=(const String &, const String &); friend bool operator<(const String &, const String &);
friend bool operator>(const String &, const String &);
friend bool operator<=(const String &, const String &);
friend bool operator>=(const String &, const String &); friend std::ostream & operator<<(std::ostream & os, const String &s);
friend std::istream & operator>>(std::istream & is, String & s); private:
char *pstr_;
}; #endif
 /*************************************************************************
> File Name: mystring.cc
> Author: lukey
> Mail: lukey123@foxmail.com
> Created Time: Wed 17 Jun 2015 09:18:55 PM CST
************************************************************************/ #include<iostream>
#include<cstring>
#include<stdlib.h>
#include"mystring.h"
using namespace std;
#if 0
class String
{
public:
private:
char *pstr_;
};
#endif //构造函数
String::String()
{
std::cout << "String()" << std::endl;
pstr_ = new char[];//new 已经初始化了
}
String::String(const char *str)//有参构造函数
{
std::cout << "String(const char * str)" << std::endl;
pstr_ = new char[strlen(str)+];
strcpy(pstr_, str);
}
String::String(const String & rhs) //复制构造,考虑自复制情况?
{
std::cout << "String(const String & rhs)" << std::endl;
pstr_ = new char[strlen(rhs.pstr_) + ];
strcpy(pstr_, rhs.pstr_);
}
String::~String()
{
std::cout << "~String()" << std::endl;
delete []pstr_;
} String & String::operator=(const String & rhs)//赋值运算符的两种情况,考虑自赋值情况
{
std::cout << "String & operator=(const String & rhs)" << std::endl;
if(this == &rhs)
return *this;
delete []pstr_;
pstr_ = new char[strlen(rhs.pstr_) + ];
strcpy(pstr_, rhs.pstr_);
return *this;
}
String & String::operator=(const char *str)
{
std::cout << "String & operator=(const char *str)" << std::endl;
pstr_ = new char[strlen(str) + ];
strcpy(pstr_, str);
return *this;
} String & String::operator+=(const String & rhs) //rhs连接到pstr_后面
{
std::cout << "operator+=(const String & rhs)" << std::endl;
int len = strlen(rhs.pstr_) + strlen(pstr_);
pstr_ = (char *)realloc(pstr_, len + );
strcat(pstr_, rhs.pstr_);
return *this;
}
String & String::operator+=(const char * str)
{
std::cout << "operator+=(const char * str)" << std::endl;
int len = strlen(str) + strlen(pstr_);
pstr_ = (char *)realloc(pstr_, len + );
strcat(pstr_, str);
return *this;
} //下标运算符,非常量,可以修改值
char & String::operator[](std::size_t index)
{
return pstr_[index];
} //常量对象取下标,不能为其赋值
const char & String::operator[](std::size_t index) const
{
return pstr_[index];
} //字符串容量
std::size_t String::size() const
{
return strlen(pstr_);
} //转换成c类型字符串,以'\0'结尾
const char* String::c_str() const
{
int len = strlen(pstr_); pstr_[len + ] = '\0';
return pstr_;
} //不懂?打印出字符串?
void String::debug()
{
std::cout << pstr_ << std::endl;
} String operator+(const String & s1, const String & s2)
{
std::cout << "operator+(const String & s1,const String & s2)" << std::endl;
String ret_str = s1.pstr_;
ret_str += s2.pstr_;
return ret_str;
} String operator+(const String & s, const char * str)
{
std::cout << "operator+(String, char *)" << std::endl;
String temp(str);
return (s + temp); //直接调用上面的(+)函数 } String operator+(const char * str, const String & s)
{
std::cout << "operator+( char *, String)" << std::endl;
String temp(str);
return (s + temp); //直接调用上面的(+)函数
} bool operator==(const String & lstr, const String & rstr)
{
std::cout << "==" << std::endl;
if(strcmp(lstr.pstr_, rstr.pstr_) == )
return true;
else
return false;
} bool operator!=(const String & lstr, const String & rstr)
{
std::cout << "!=" << std::endl;
return !(lstr == rstr);
} bool operator<(const String & lstr, const String & rstr)
{
std::cout << "<" << std::endl;
if(strcmp(lstr.pstr_, rstr.pstr_) < )
return true;
else
return false;
} bool operator>(const String & lstr, const String & rstr)
{
std::cout << ">" << std::endl;
if(strcmp(lstr.pstr_, rstr.pstr_) > )
return true;
else
return false;
}
bool operator<=(const String & lstr, const String & rstr)
{
std::cout << "<=" << std::endl;
if(strcmp(lstr.pstr_, rstr.pstr_) <= )
return true;
else
return false;
} bool operator>=(const String & lstr, const String & rstr)
{
std::cout << ">=" << std::endl;
if(strcmp(lstr.pstr_, rstr.pstr_) >= )
return true;
else
return false;
} std::ostream & operator<<(std::ostream & os, const String &s)
{
os << s.pstr_ << " ";
return os;
}
std::istream & operator>>(std::istream & is, String & s)
{
is >> s.pstr_;
return is; //貌似有坑, 目前不能输入空格
} //测试时每个函数调用都打印了信息
int main(void)
{
String s1("hello");
s1.debug();
std::cout << s1;
std::cout << std::endl;
String s2("world");
s2.debug();
if(s1 > s2)
std::cout << "s1 > s2" << std::endl; s1 = s2;
s1.debug();
String s3(s1);
s3.debug(); String s4 = s2 + s3;
s4.debug(); String s5;
std::cout << s5 << std::endl;
std::cin >> s5;
std::cout << s5 << std::endl;
return ;
}

mystring c++ 自己的string 封装的更多相关文章

  1. String封装——读时共享,写时复制

    碰到过一位一直怀疑C++标准库(STL)效率的人,他说STL效率太低,企业开发根本不会用.我是持反对意见的. 说这话的人,肯定没有做过大量的调查.没有调查就没有发言权. STL的效率是不低的,足够满足 ...

  2. [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  3. 467 Unique Substrings in Wraparound String 封装字符串中的独特子字符串

    详见:https://leetcode.com/problems/unique-substrings-in-wraparound-string/description/ C++: class Solu ...

  4. Java中的String为什么是不可变的?

    转载:http://blog.csdn.net/zhangjg_blog/article/details/18319521 什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那 ...

  5. c++ string的实现。

    第三次做了.只是做个复习.偶然发现之前的版本有内存泄露.基本功还是不过关.这次应该没有内存泄漏了.虽然是个简单版本. 1)了解堆,栈,值copy. 2)几个常用的c的字符函数和c中的char 如何表示 ...

  6. stl string

    10.2.1 STL的string 1String概念 ²  string是STL的字符串类型,通常用来表示字符串.而在使用string之前,字符串通常是用char*表示的.string与char*都 ...

  7. Java基础知识强化101:Java 中的 String对象真的不可变吗 ?

    1. 什么是不可变对象?       众所周知, 在Java中, String类是不可变的.那么到底什么是不可变的对象呢? 可以这样认为:如果一个对象,在它创建完成之后,不能再改变它的状态,那么这个对 ...

  8. swift 中String常用操作

    1.  字符串定义 var s = "aaaaaa" // 两个字符串均为空并等价. var emptyString = ""   var anotherEmp ...

  9. Java学习之String对象为什么是不可变的

    转自:http://www.2cto.com/kf/201401/272974.html,感谢作者的总结 什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那么到底什么是不可变 ...

随机推荐

  1. php项目第三季

    1.百度浏览器路径写相对路径 2.svn--setting中两个勾去掉,第二页一个勾打上.工程上右击update是更新,commit是提交. 3.Deprecated: mysql_connect() ...

  2. python zookeeper 在 uwsgi中 watcher不生效

    def code_watcher(handle,type, state, path):     print "zk code watcher,path is: ",path #da ...

  3. python--lambda和def函数

    1.Python lambda和Python def区别分析 Python支持一种有趣的语法,它允许你快速定义单行的最小函数.这些叫做lambda的函数,是从Lisp借用来的,可以用在任何需要函数的地 ...

  4. Adobe Flash CC 2014 下载及破解

    来源 :http://prodesigntools.com/adobe-cc-2014-direct-download-links.html 地址:http://trials3.adobe.com/A ...

  5. 昨天上架出现问题,you binary is not optimized for iphone5.。。。。

    这个时候只需要加一个lanuch image 就可以了

  6. java使用iText生成pdf表格

    转载地址:http://www.open-open.com/code/view/1424011530749 首先需要你自己下载itext相关的jar包并添加引用,或者在maven中添加如下引用配置: ...

  7. maven 国内镜像地址

    由于连接国外网站时网速特慢,为解决这个问题,os china 建立了一个maven 的私服.为了记忆,特将此记录. settings.xml 设置镜像方法步骤如下: 1. mirrors 设置 < ...

  8. PHP安装后php-config命令干嘛的

    php-config 是一个简单的命令行脚本用于查看所安装的 PHP 配置的信息. 我们在命令行执行 php-config 会输出所有的配置信息 Usage: /usr/local/php/bin/p ...

  9. 关于C#静态构造函数的几点说明

    静态构造函数是C#的一个新特性,其实好像很少用到.不过当我们想初始化一些静态变量的时候就需要用到它了.这个构造函数是属于类的,而不是属于哪里实例的,就是说这个构造函数只会被执行一次.也就是在创建第一个 ...

  10. Echarts自适应浏览器大小

    var myChart = echarts.init(document.getElementById('sitesChar')); var option = { title : { text: 'No ...