自实现部分string类的功能
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class MyString
{
public:
MyString::MyString(); //无参构造
MyString(const char* str ); //默认参数
MyString(const MyString& other); //拷贝构造
MyString& operator=(const MyString& other); //重载等号(参数不同)
MyString& operator=(const char* str); //重载等号(参数不同)
~MyString(); //析构函数
char& operator[](unsigned int index); //重载[]号 MyString& operator+=(const MyString& other); //重载+=号
friend MyString operator+(const MyString& s1, const MyString& s2); //重载加号;用全局的友元函数
friend ostream& operator<<(ostream& os, const MyString& str); //重载左移操作符;用全局函数
friend istream& operator>>(istream& is, MyString& str); //重载右移操作符,用全局函数
private:
char* m_str;
}; MyString::MyString()
{
m_str = new char[];
m_str = '\0';
} MyString::MyString(const char* str) //默认参数
{
if (str == NULL)
{
m_str = new char[];
m_str = '\0'; }
else
{
int len = strlen(str);
m_str = new char[len + ];
strcpy(m_str, str);
}
} MyString::MyString(const MyString& other)
{
int len = strlen(other.m_str);
m_str = new char[len + ];
strcpy(m_str, other.m_str);
} MyString& MyString::operator=(const MyString& other)
{
if (&other == this)
return *this;
if (m_str != NULL)
{
delete[]m_str;
} int len = strlen(other.m_str);
m_str = new char[len + ];
strcpy(m_str, other.m_str);
return *this;
} MyString& MyString::operator=(const char* str)
{
if (m_str != NULL)
{
delete[]m_str;
} if (str == NULL)
{
m_str = new char[];
m_str = '\0';
}
else
{
int len = strlen(str);
m_str = new char[len + ];
strcpy(m_str, str);
} return *this;
}
MyString::~MyString()
{
if (m_str != NULL)
{
delete[]m_str;
m_str = NULL;
}
}
char& MyString::operator[](unsigned int index)
{
unsigned int len = strlen(m_str);
if (index > len)
return m_str[len]; return m_str[index];
} MyString& MyString::operator+=(const MyString& other)
{
int len = strlen(m_str) + strlen(other.m_str);
char *newstr = new char[len + ];
strcpy(newstr, m_str);
strcat(newstr, other.m_str); if (m_str != NULL)
{
delete[]m_str;
}
m_str = newstr; return *this;
}
MyString operator+(const MyString& s1, const MyString& s2)
{
int len = strlen(s1.m_str) + strlen(s2.m_str);
char *newstr = new char[len + ];
strcpy(newstr, s1.m_str);
strcat(newstr, s2.m_str); MyString My(newstr); return My;
}
ostream& operator<<(ostream& os, const MyString& str)
{
os << str.m_str << endl;
return os;
}
istream& operator>>(istream& is, MyString& str)
{
char newstr[];
is >> newstr;
int len = strlen(newstr);
if (str.m_str != NULL)
{
delete []str.m_str;
}
str.m_str = new char[len + ];
strcpy(str.m_str, newstr); return is;
} //测试代码
void test11()
{
MyString m1;
MyString m2("nihao");
MyString m3(m2);
cout << m2;
cout << m3;
m1 = m2 + m3;
cout << m1;
m1 += m3;
cout << m1[-] << endl;
cin >> m3;
cout << m3; } int main()
{ test11(); system("pause");
return EXIT_SUCCESS;
}
自实现部分string类的功能的更多相关文章
- String类常用功能
String类常用功能 判断: boolean equals(Object obj) boolean equalsIgnoreCase(String str) //忽略大小写 boolean star ...
- String类的功能
String类 标红的为较少出现的 1.判断功能 boolean equals(Object obj) :比较字符串内容是否相同,区分大小写 boolean equalsIg ...
- 标准C++中的string类的用法总结
标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...
- 关于如何来构造一个String类
今天帮着一位大二的学弟写了一个String的类,后来一想这个技术点,也许不是什么难点,但是还是简单的记录一些吧! 为那些还在路上爬行的行者,剖析一些基本的实现..... 内容写的过于简单,没有涉及到其 ...
- VC++ 标准C++中的string类的用法总结
相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯 ...
- c++中string类的详解
,<时返回-1,==时返回0 string的子串:string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串strin ...
- 标准C++中string类的用法
转自博客园:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 相信使用过MFC编程的朋友对CString这个类的印象应该非 ...
- 标准C++中的string类的用法总结(转)
http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的 ...
- C++中的string类(2)
相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯 ...
随机推荐
- android 关于Make sure the plugin is properly configured问题的解决办法
这个问题引发的原因最初的报错是: [2013-10-14 10:01:58 - XXX] The connection to adb is down, and a severe error has o ...
- centos svnversion安装部署
第一步: yum install subversion; 第二步: mkdir /data/svn/conf mkdir /data/svn/library 第三步: svnadmin create ...
- 8VC Venture Cup 2016 - Elimination Round B. Cards 瞎搞
B. Cards 题目连接: http://www.codeforces.com/contest/626/problem/B Description Catherine has a deck of n ...
- HMAC结合“挑战/响应”保障数据传输安全
1.流程图: HMAC的一个典型应用是结合“挑战/响应”(Challenge/Response)来保障客户端和服务器传输数据的安全性 . 2.安全性分析: 使用的密钥是双方事先约定的,第三方不可能知道 ...
- NServiceBus入门:发送一个命令(Introduction to NServiceBus: Sending a command)
原文地址:https://docs.particular.net/tutorials/intro-to-nservicebus/2-sending-a-command/ 侵删. 能够发送和接收mess ...
- Nokitjs 系列-01 - HelloWorld
一.前言 本篇文章需要读者有一点 Node.js 基础的了解,并且已经安装了 Node.js (node.npm),但并不需要有 Nokit 的知识,本文将简单介绍 Nokitjs 的安装使用,并编写 ...
- jquer回显选中select下拉框
公司使用的框架比较旧,没有使用el等表达式. <% String context = request.getContextPath(); String index = (String)reque ...
- iOS:开发常用GitHub开源项目(持续更新)
IOS开发常用GitHub开源项目(持续更新) 数据类 开源库 作者 简介 AFNetworking Mattt 网络请求库 ASIHTTPRequest pokeb 网络请求库 Alamofire ...
- phpMyAdmin配置
一: (1):下载phpmyadmin,在官方最好 (2):个人建议最好将其安装在apache的htdocs文件中(apache的默认虚拟目录,或web访问目录) ...
- js 获取两位小数的方法
1. 最笨的办法 function get() { var s = 22.127456 + ""; var str = s.substring(0,s.indexOf(" ...