免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2669 | 回复: 9
打印 上一主题 下一主题

高手们来补充string类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-08-16 15:03 |只看该作者 |倒序浏览
本帖最后由 aoyang888 于 2011-08-18 19:42 编辑

#include <iostream>
using namespace std;
class String
{
public:               
        String(const char* cstr = NULL);      //构造函数
        ~String(void);                                 //析构函数
        String(const String &other);           //复制构造函数
        String &operator = (const String & other);  //复制运算符
        String *operator &();                                  //取地址运算符
        const String *operator &()const;                //取地址运算符const

       friend ostream &operator<<(ostream &output, const String &other);
       friend istream &operator>>(istream &input, const String &other);
private:
        char *str;
}

都来看看,进来了就别放过这个问题,锻炼一下基本功。
各位能补充的尽量补充,谢谢!

论坛徽章:
0
2 [报告]
发表于 2011-08-16 15:04 |只看该作者
本帖最后由 aoyang888 于 2011-08-16 20:59 编辑

ostream &operator<<(ostream &output, const String &other)      
{
        output<<other.str;
        return output;
}
istream &operator>>(istream &input, const String &other)
{
        delete []other.str;
        other.str = new char[100];
        input>>other.str;
        return input;
}

论坛徽章:
0
3 [报告]
发表于 2011-08-18 08:54 |只看该作者

  1. String & operator + (const String &other);
  2. String & operator + (const char* cstr);
复制代码

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:50:28
4 [报告]
发表于 2011-08-18 09:01 |只看该作者
重复造车轮?

论坛徽章:
0
5 [报告]
发表于 2011-08-18 16:43 |只看该作者
回复 1# aoyang888
  1.     String *operator &();
  2.         const String *operator &()const;

  3.        friend ostream &operator<<(ostream &output, const String &other);
  4.        friend istream &operator>>(istream &input, const String &other);
复制代码
这几个函数是干嘛的啊?

论坛徽章:
0
6 [报告]
发表于 2011-08-18 16:53 |只看该作者
回复 5# kim1128


    额。。。。这不是重载运算符嘛。。。。。{:2_170:}

论坛徽章:
0
7 [报告]
发表于 2011-08-18 17:19 |只看该作者
一般来说,不要重复造轮子,但不等于就要将就使用蹩脚的轮子

既然造轮子了,那么就要造一个能重复用的轮子。

不同的场合需要不同类型的轮子,积累一些不同特点的合适的轮子也是必要的,不能因为有了轮子就只用这一个轮子。


回到String类的问题,为什么要造这个轮子,别的轮子有什么问题,这个新轮子有什么特点?

不是接口越多越好,不是功能越强大越好,而是这个轮子要有值得造的特点。


我自己做的String类,增加了buflen、stringlen的信息,不支持istream、ostream(其他时候也少用这两个东西)。
增加了Format(类似sprintf),Append、AppendFormat等功能

论坛徽章:
0
8 [报告]
发表于 2011-08-18 19:46 |只看该作者
7楼的可以把buflen、stringlen、Forma、Append、AppendFormat代码贴出来看看
造更好的轮子

String毕竟是一个设计到很多知识的类,有数组、引用、运算符重载、友元、重载、对象的赋值...
很值得去研究研究,虽然实际作用不大

论坛徽章:
0
9 [报告]
发表于 2011-08-18 19:54 |只看该作者
回复 5# kim1128


    有取值运算符const版
    重载输出
    重载输入
    这样String str;
     cin>>str;
    cout<< str;
    操作对象就行了

论坛徽章:
0
10 [报告]
发表于 2011-08-18 21:54 |只看该作者
回复 1# aoyang888


    查找字符串 超找字符 从 右边查找

解析 token  ... N多个  都有了的  又搞这么干嘛 看下源代码就行
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP