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类是不可变的.那么到底什么是不可变 ...
随机推荐
- html标签全称和功能介绍
html标签全称和功能介绍,里面有些大家不常用的,主要是方便大家查找检索 按字母顺序排列 DTD:指示在哪种 XHTML 1.0 DTD 中允许该标签.S=Strict, T=Transition ...
- VO , PO , BO , QO, DAO ,POJO
VO , PO , BO , QO, DAO ,POJO, O/R Mapping 是 Object Relational Mapping (对象关系映射)的缩写.通俗点讲,就是将对象与关系数据库绑定 ...
- javascript模糊查询一个数组
/* * 模糊查询一个数组 */ com.ty.repairtech.JsonOperation.searchList = function(str, container) { var newList ...
- 四维dp 或者 剪枝 + dfs Codeforces Beta Round #6 (Div. 2 Only) D
http://codeforces.com/contest/6/problem/D 题目大意:有一队人,排成一列,每个人都有生命值,你每次可以攻击2~n位置的一个的人,假设每次攻击的位置为pos,那么 ...
- laravel使用多个数据库连接
1.配置.env文件 DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=database_name DB_USERNAME= ...
- 笨方法学python--多行,转义序列
1 输入多行字符串的方法有2个,一个是使用换行符 \n.另一个是使用 "三引号". 2 针对不同的符号,有很多这样的"转义序列"(escape sequence ...
- VB 对象变量或with块变量未设置
先看错误代码,以下代码提示 对象变量或with块变量未设置: Dim obj As Object obj = WebBrowser1.Document.getElementById("swi ...
- adodb.stream对象的方法/属性
Cancel 方法 使用方法如下 Object.Cancel 说明:取消执行挂起的异步 Execute 或 Open 方法的调用.Close 方法 使用方法 ...
- JavaScript的第一次小结
一. JavaScript是一种的脚本语言:特点是:具有解释性,基于对象,事件驱动,安全性和跨平台等特点 对于这几种特点有必要说明一下 解释性:就是JavaScripte本身就是一种解释性语言 基于对 ...
- Gym 100917L Liesbeth and the String 规律&&胡搞
题目: Description standard input/outputStatements Little Liesbeth likes to play with strings. Initiall ...