mystring c++ 自己的string 封装
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 封装的更多相关文章
- String封装——读时共享,写时复制
碰到过一位一直怀疑C++标准库(STL)效率的人,他说STL效率太低,企业开发根本不会用.我是持反对意见的. 说这话的人,肯定没有做过大量的调查.没有调查就没有发言权. STL的效率是不低的,足够满足 ...
- [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- 467 Unique Substrings in Wraparound String 封装字符串中的独特子字符串
详见:https://leetcode.com/problems/unique-substrings-in-wraparound-string/description/ C++: class Solu ...
- Java中的String为什么是不可变的?
转载:http://blog.csdn.net/zhangjg_blog/article/details/18319521 什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那 ...
- c++ string的实现。
第三次做了.只是做个复习.偶然发现之前的版本有内存泄露.基本功还是不过关.这次应该没有内存泄漏了.虽然是个简单版本. 1)了解堆,栈,值copy. 2)几个常用的c的字符函数和c中的char 如何表示 ...
- stl string
10.2.1 STL的string 1String概念 ² string是STL的字符串类型,通常用来表示字符串.而在使用string之前,字符串通常是用char*表示的.string与char*都 ...
- Java基础知识强化101:Java 中的 String对象真的不可变吗 ?
1. 什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那么到底什么是不可变的对象呢? 可以这样认为:如果一个对象,在它创建完成之后,不能再改变它的状态,那么这个对 ...
- swift 中String常用操作
1. 字符串定义 var s = "aaaaaa" // 两个字符串均为空并等价. var emptyString = "" var anotherEmp ...
- Java学习之String对象为什么是不可变的
转自:http://www.2cto.com/kf/201401/272974.html,感谢作者的总结 什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那么到底什么是不可变 ...
随机推荐
- @Autowired 和 @Resource
转自:Spring中@Autowired注解.@Resource注解的区别 Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resourc ...
- hdu_3062_Party(2-SAT)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3062/ 题意:2-SAT的裸题 题解:直接上模版 #include<cstdio> #in ...
- form异步无刷新提交,提交后可以显示弹出框,否则弹出框会被刷新不见,使用 preventDefault
出错点:确认按钮上.加onclick事件.每次点击都会追加给form追加on监听方法.累加on方法,重复提交 suppress_exception:true 阻止异常 (百度推送 jdk) 向下按 p ...
- POJ2524:Ubiquitous Religions (并查集模板)
Description There are so many different religions in the world today that it is difficult to keep tr ...
- Java良葛格 学习笔记《二》
正则表达式 . 符合任一字符\d 符合0到9任一个数字字符\D 符合0-9以外的字符\s 符合'\t'.'\n'.'\x0B'.'\f'.'\r'等空格符\w 符合a到z.A到Z.0到9等字符,也就是 ...
- 2016 ccpc 杭州赛区的总结
毕竟是在杭电比的,和之前大连的icpc不同,杭电毕竟是隔壁学校,来回吃住全都是在自家寝室,方便! 不过说到方便也是有点不方便,室友都喜欢玩游戏,即使我昨晚9.30就睡觉了,仍然是凌晨一点才睡着,233 ...
- 使用WMware新建linux虚拟机
使用WMware安装linux虚拟机的时候很多人搞不清楚使用什么适配器. 平时也看到很多误人子弟的观点,这里用事实说话. VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络 ...
- 解决编译时出现的警告:format string is not a string literal (potentially insecure)
NSLog([NSString stringWithFormat:@"%@/%@B.jpg", createDir, uuid]);//这是我的写法 应该写成 NSString * ...
- linux的学习系列 6---打印文件和发送邮件
文件打印 如果你希望打印文本文件,最好预先处理一下,包括调整边距.设置行高.设置标题等,这样打印出来的文件更加美观,易于阅读.当然,不处理也可以打印,但是可能会比较丑陋. 大部分的Linux自带了 n ...
- 444A/CF
题目链接[http://codeforces.com/problemset/problem/444/A] 题意:给出一个无向图,找出一个联通子图,定义密度#=v(顶点值的和)/e(边值的和). 条件: ...