| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1739 人关注过本帖
标题:我写的mystring 类,字符串长度超过int,size_t范围,顺便帮我看看这个类有啥 ...
取消只看楼主 加入收藏
陈紫文
Rank: 1
等 级:新手上路
帖 子:14
专家分:4
注 册:2018-9-5
结帖率:62.5%
收藏
已结贴  问题点数:5 回复次数:0 
我写的mystring 类,字符串长度超过int,size_t范围,顺便帮我看看这个类有啥问题
我发现了一个问题,就是当我用mystring 对象去读取一个文本对象时,如果文本对象特别大,比如10m,那么求出来的字符串 的长度不对,
我感觉是字符串的长度超出了int 的 范围,后面把int 改为了size_t 也不对,求问大家咋个办,如果有空的话,能不能帮我看看我写的这个类有啥问题。谢谢

//下面是mystring类的定义
//file mystring。h begin
#ifndef MYSTRING_H_
#define MYSTRING_H_
#include <stddef.h>
#include <string>
#include <iostream>
using namespace std;
/*
这是自定义的string类库
Mystring 类
*/

class Mystring
{
public:
    Mystring();//默认构造函数
    Mystring(const Mystring& str);//拷贝构造函数
    Mystring(const char *str);
    ~Mystring();//析构函数
    //重置运算符=
    Mystring& operator=(const Mystring &str);
    Mystring& operator=(const char* str);
    //一般赋值运算符
    Mystring& assign(const Mystring& str);
    Mystring& assign(const char *str);
    //处理字符串的函数
    size_t get_sizr()const;//得到字符串的长度
    void clear();//清空字符串
    bool empty() const;//判断字符串是不是空
    int compare(const Mystring& str)const;//比较字符串
    int compare(const char * str) const;
    //字符串尾追加函数
    //运算符重置+=
    Mystring& operator+=(const Mystring& str);
    Mystring& operator+=(const char *str);
    //一般追加函数
    Mystring& append(const Mystring& str);
    Mystring& append(const char *str);
    //生成子串,从0开始,长度为n
    Mystring substr(size_t pos = 0, size_t n = string::npos);
    //关系运算符重载,友元函数
    friend bool operator ==(const Mystring& str1,const Mystring& str2);
    friend bool operator ==(const Mystring& str1, const char *str2);
    friend bool operator ==(const char *str1, const Mystring& str2);

    friend bool operator <(const Mystring& str1, const Mystring& str2);
    friend bool operator <(const Mystring& str1, const char *str2);
    friend bool operator <(const char *str1, const Mystring& str2);

    friend bool operator >(const Mystring& str1, const Mystring& str2);
    friend bool operator >(const Mystring& str1, const char *str2);
    friend bool operator >(const char *str1, const Mystring& str2);
   
    friend Mystring operator+(const Mystring& str1, const Mystring& str2);
    friend Mystring operator+(const Mystring& str1, const char *str2);
    friend Mystring operator+(const char *str1, const Mystring& str2);

    //重载输出<<运算符
    friend ostream& operator<<(ostream &out, Mystring &str);
//    friend istream& operator>>(istream &in, Mystring& str);
private:
    char* m_string;//指向字符串的指针
    size_t m_length;//字符串的长度
    static  int m_num;//创建的字符串的个数
};


#endif MYSTRING_H_


//file mystring.h end
//下面是mystring 的实现
file mystring.cpp begin
#include "Mystring.h"
#include <new>
#include <iostream>
using namespace std;

 int Mystring::m_num = 0;
//构造函数
Mystring::Mystring()
{
    ++m_num;
    m_length = 0;
    m_string = new char[1];
    char *str = "\0";
    memcpy(m_string, str, 1);
}

Mystring::Mystring(const Mystring& str)
{
    m_length = str.m_length;
    ++m_num;
    m_string = new char[m_length+1];
    memcpy(m_string, str.m_string, m_length);
    m_string[m_length] = '\0';
}
Mystring::Mystring(const char *str)
{
    size_t len = strlen(str);
    m_length = len;
    m_string = new char[len+1];
    memcpy(m_string, str, len);
    m_string[len ] = '\0';
    m_num++;
}
Mystring::~Mystring()
{
    delete [] m_string;
    m_string = nullptr;
    m_length = 0;
    --m_num;
    cout << "对象析构完成" << endl;
    cout << "程序现在包含的对象数:" << m_num << endl;
}

//重置运算符=
Mystring& Mystring::operator=(const Mystring &str)
{
    if (*this==str)
    {
        return *this;
    }
    m_length = str.m_length;
    delete[] m_string;
    m_string = nullptr;
    m_string = new char[m_length+1];
    memcpy(m_string, str.m_string, str.m_length);
    m_string[m_length] = '\0';
    return *this;
}

Mystring& Mystring::operator=(const char* str)
{
    size_t len = strlen(str);
    m_length = len;
    delete[] m_string;
    m_string = nullptr;
    m_string = new char[len+1];
    memcpy(m_string, str, len);
    m_string[len] = '\0';
    return *this;
}

//一般赋值运算符
Mystring& Mystring::assign(const Mystring& str)
{
    if (*this == str)
    {
        return *this;
    }
    m_length = str.m_length;
    delete[] m_string;
    m_string = nullptr;
    m_string = new char[m_length];
    memcpy(m_string, str.m_string, str.m_length);
    return *this;
}
Mystring& Mystring::assign(const char *str)
{
    size_t len = strlen(str);
    m_length = len;
    delete[] m_string;
    m_string = nullptr;
    m_string = new char[len];
    memcpy(m_string, str, len);
    return *this;
}

//处理字符串的函数
size_t Mystring:: get_sizr()const//得到字符串的长度
{
    return m_length;
}
void Mystring::clear()//清空字符串
{
    m_length = 0;
    while (*m_string!='\0')
    *m_string = '\0';
}
bool Mystring::empty() const//判断字符串是不是空
{
    return m_length > 0 ? false : true;
}
int Mystring::compare(const Mystring& str)const//比较字符串
{
    return strcmp(m_string, str.m_string);
   
}
int Mystring::compare(const char * str) const
{
    return strcmp(m_string, str);
}

//字符串尾追加函数
//运算符重置+=
Mystring& Mystring::operator+=(const Mystring& str)
{
    size_t len = m_length + str.m_length;
    char * ch = new char[len+1];
    memcpy(ch, m_string, m_length);
    memcpy(ch + m_length, str.m_string, str.m_length);
    ch[len] = '\0';
    delete[] m_string;
    m_string = nullptr;
    m_string = new char[len+1];
    memcpy(m_string, ch, len);
    m_string[len] = '\0';
    m_length = len;
    return *this;
}
Mystring& Mystring::operator+=(const char *str)
{
    size_t len = m_length + strlen(str);
    char * ch = new char[len+1];
    memcpy(ch, m_string, m_length);
    memcpy(ch + m_length, str, strlen(str));
    ch[len] = '\0';
    delete[] m_string;
    m_string = nullptr;
    m_string = new char[len+1];
    memcpy(m_string, ch, len);
    m_string[len] = '\0';
    m_length = len;
    return *this;
}

Mystring& Mystring::append(const Mystring& str)
{
    size_t len = m_length + str.m_length;
    char * ch = new char[len+1];
    memcpy(ch, m_string, m_length);
    memcpy(ch + m_length, str.m_string, str.m_length);
    ch[len] = '\0';
    delete[] m_string;
    m_string = nullptr;
    m_string = new char[len+1];
    memcpy(m_string, ch, len);
    m_string[len] = '\0';
    m_length = len;
    return *this;
}
Mystring& Mystring::append(const char *str)
{
    size_t len = m_length + strlen(str);
    char * ch = new char[len+1];
    memcpy(ch, m_string, m_length);
    memcpy(ch + m_length, str, strlen(str));
    ch[len] = '\0';
    delete[] m_string;
    m_string = nullptr;
    m_string = new char[len];
    memcpy(m_string, ch, len);
    m_string[len] = '\0';
    m_length = len;
    return *this;
}

 bool operator ==(const Mystring& str1, const Mystring& str2)
{
     return strcmp(str1.m_string, str2.m_string) == 0;
}
 bool operator ==(const Mystring& str1, const char *str2)
{
     return strcmp(str1.m_string, str2) == 0;
}
 bool operator ==(const char *str1, const Mystring& str2)
{
     return strcmp(str1, str2.m_string)==0;
}

  bool operator <(const Mystring& str1, const Mystring& str2)
 {
      return strcmp(str1.m_string, str2.m_string) < 0;
 }
  bool operator <(const Mystring& str1, const char *str2)
  {
      return strcmp(str1.m_string, str2) < 0;
  }
  bool operator <(const char *str1, const Mystring& str2)
  {
      return strcmp(str1, str2.m_string) < 0;
  }

  bool operator >(const Mystring& str1, const Mystring& str2)
  {
      return strcmp(str1.m_string, str2.m_string) > 0;
  }

  bool operator >(const Mystring& str1, const char *str2)
  {
      return strcmp(str1.m_string, str2) < 0;
  }
  bool operator >(const char *str1, const Mystring& str2)
  {
      return strcmp(str1, str2.m_string) < 0;
  }

  Mystring  operator+(const Mystring& str1, const Mystring& str2)
  {
      size_t len = str1.m_length + str2.m_length;
      Mystring ch;
      delete[] ch.m_string;
      ch.m_string = nullptr;
      ch.m_string = new char[len+1];
      ch.m_length = len;
      memcpy(ch.m_string, str1.m_string, str1.m_length);
      memcpy(ch.m_string + str1.m_length, str2.m_string, str2.m_length);
      ch.m_string[len] = '\0';
      return ch;

  }
  Mystring operator+(const Mystring& str1, const char *str2)
  {
      size_t len = str1.m_length + strlen(str2);
      Mystring ch;
      delete [] ch.m_string;
      ch.m_string = nullptr;
      ch.m_string = new char[len+1];
      ch.m_length = len;
      memcpy(ch.m_string, str1.m_string, str1.m_length);
      memcpy(ch.m_string+str1.m_length , str2,strlen(str2));
      ch.m_string[len] = '\0';
      return ch;
  }
  Mystring operator+(const char *str1, const Mystring& str2)
  {
      size_t len = str2.m_length + strlen(str1);
      Mystring ch;
      delete[] ch.m_string;
      ch.m_string = nullptr;
      ch.m_string = new char[len+1];
      ch.m_length = len;
      memcpy(ch.m_string, str1, strlen(str1));
      memcpy(ch.m_string + strlen(str1), str2.m_string, str2.m_length);
      ch.m_string[len] = '\0';
      return ch;
  }
  ostream& operator<<(ostream &out, Mystring &str)
  {
      out << "str.m_string:" << str.m_string << ";" << endl;
      out << "str.m_length:" << str.m_length << endl;
      out << "创建的对象数:" << Mystring::m_num << endl;
      return out;
  }

  Mystring Mystring:: substr(size_t pos , size_t n )
  {
      Mystring ch;
      delete[] ch.m_string;
      if (n>m_length)
      {
          ch.m_string = new char[m_length-pos+1];
          memcpy(ch.m_string, m_string+pos, m_length - pos );
          ch.m_string[m_length + pos] = '\0';
          ch.m_length = m_length + pos;
          return ch;
      }
      ch.m_string = new char[n+1];
      memcpy(ch.m_string, m_string + pos, n);
      ch.m_length = n;
      ch.m_string[n] = '\0';
      return ch;

  }
  /*
  istream& operator>>(istream &in, Mystring& str)
  {
      delete[] str.m_string;
      str.m_string = new char[1000000000];
      in >> str.m_string;
      str.m_length = strlen(str.m_string);
      return in;
  }
  */
//file mystring.cpp end
搜索更多相关主题的帖子: str const char operator return 
2018-10-20 20:24
快速回复:我写的mystring 类,字符串长度超过int,size_t范围,顺便帮我看看这个 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039882 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved