自己实现 String 类
实现Stirng类:普通构造、复制构造、赋值函数、重载输出函数 <<(友元)
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std; class String
{
public:
String (const char *str=NULL); //普通构造函数
String (const String &other); //复制构造函数
~String (void); //析构函数
String &operator=(const String &other); //赋值函数
friend ostream &operator<<(ostream &output, const String &other); //输出函数
private:
char *data; //保存字符串
}; //普通构造函数
String::String(const char *str)
{
if (str == NULL)
{
data = new char[];
*data = '\0';
}
else
{
int len = strlen(str);
data = new char[len+];
strcpy(data, str);
}
} //复制构造函数
String::String(const String &other)
{
int len = strlen(other.data);
data = new char[len+];
strcpy(data, other.data);
} //析构函数
String::~String()
{
delete[] data;
} //赋值函数
String &String::operator=(const String &other)
{
if (this == &other)
{
return *this;
}
delete[] data; int len = strlen(other.data);
data = new char[len+];
strcpy(data, other.data); return *this;
} //输出函数
ostream &operator<<(ostream &output, const String &other)
{
output<<other.data;
return output;
} int main()
{
String s1("hello");
cout << s1 << endl;
String s2=s1;
cout << s2 << endl;
String s3;
s3=s1;
cout << s3 << endl;
}
自己实现 String 类的更多相关文章
- 标准库String类
下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数. [cpp] view plain copy #include<iostream> #include ...
- 自己实现简单的string类
1.前言 最近看了下<C++Primer>,觉得受益匪浅.不过纸上得来终觉浅,觉知此事须躬行.今天看了类类型,书中简单实现了String类,自己以前也学过C++,不过说来惭愧,以前都是用C ...
- C++ string类的实现
c++中string类的实现 今天面试被考到了, 全给忘记了!!! //string类的实现 #include <iostream> #include <string.h> ...
- String类的功能
String类 标红的为较少出现的 1.判断功能 boolean equals(Object obj) :比较字符串内容是否相同,区分大小写 boolean equalsIg ...
- java基础复习:final,static,以及String类
2.final 1)为啥String是final修饰的呢? 自己答: 答案: 主要是为了“效率” 和 “安全性” 的缘故.若 String允许被继承, 由于它的高度被使用率, 可能会降低程序的性能,所 ...
- String类和StringBuffer类的区别
首先,String和StringBuffer主要有2个区别: (1)String类对象为不可变对象,一旦你修改了String对象的值,隐性重新创建了一个新的对象,释放原String对象,StringB ...
- 05_整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明
Question: 整理String类的Length().charAt(). getChars().replace(). toUpperCase(). toLowerCase().trim().toC ...
- 标准C++中的string类的用法总结
标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...
- String类常用方法
1.String类的特点,字符串一旦被初始化就不会被改变. 2.String对象定义的两种方式 ①String s = "affdf";这种定义方式是在字符串常量池中创建一个Str ...
- 运用String类实现一个模拟用户登录程序
package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...
随机推荐
- Android Volley完全解析(四),带你从源码的角度理解Volley
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/17656437 经过前三篇文章的学习,Volley的用法我们已经掌握的差不多了,但是 ...
- 【转】Python获取当前系统时间
转自:https://www.cnblogs.com/-ldzwzj-1991/p/5889629.html 取得时间相关的信息的话,要用到python time模块,python time模块里面有 ...
- memcached数据库
Python-memcached的基本使用
- BZOJ - 2142 礼物 (扩展Lucas定理)
扩展Lucas定理模板题(貌似这玩意也只能出模板题了吧~~本菜鸡见识鄙薄,有待指正) 原理: https://blog.csdn.net/hqddm1253679098/article/details ...
- 开发mis系统用到的技术
1. b/s架构:就broser/server,浏览器/服务器的说法.服务器端要运行tomcat,提供链接数据库服务供java代码读写数据,这个可以在eclipse中配置运行.浏览器则解释jsp或ht ...
- LeetCode Distribute Candies
原题链接在这里:https://leetcode.com/problems/distribute-candies/#/description 题目: Given an integer array wi ...
- luogu P4848 崂山白花蛇草水
https://www.luogu.org/problemnew/show/P4848 我的数据结构大概已经废了. 外层权值线段树内层kdtree,外层线段树上二分答案. 码数据结构一时爽,码完deb ...
- openwrt 按下回车才能显示图标信息
如题所示,openwrt启动后,手动才能按下系统图标和信息. 如何却掉这个手动选项呢? 修改文件/SISP-L26.7.8-OpenWrt/build_dir/target-arm_uClibc-0. ...
- DBUtils使用BeanListHandler及BeanHandler时返回null
一.使用Bean相关方法时返回null 问题描述: 使用DBUtils查询数据,如果使用ArrayListHandler等都能够返回正确值,但使用BeanListHandler 和 BeanHandl ...
- 基于OpenCV的火焰检测(一)——图像预处理
博主最近在做一个基于OpenCV的火焰检测的项目,不仅可以检测图片中的火焰,还可以检测视频中的火焰,最后在视频检测的基础上推广到摄像头实时检测.在做这个项目的时候,博主参考了很多相关的文献,用了很多种 ...