资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

代码分析c++中string类

一:回顾

站在用户的角度思考问题,与客户深入沟通,找到库车网站设计与库车网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都做网站、成都网站制作、企业官网、英文网站、手机端网站、网站推广、空间域名、网页空间、企业邮箱。业务覆盖库车地区。

(1)c++中的string类是在面试中和笔试中经常考的题目; 工程代码免费下载 string类的自行实现

(2)c++中的string类和fstream类合起来是处理外部数据的利器;

(3)string类经常用到find find_first_of find_first_not_of find_last_of find_last_not_of substr replace等,以及联合使用来达到java中的split和trim

(4) 使用friend 仅仅是在类中进行声明的非内部 却可以访问内部成员的外部函数,而且在外部不再需要friend关键字;它与成员函数的区别是,friend和外部函数不含有this对象指针;本文用到了const 定义的全局最大值最小值变量(代替#define)

(5) 有些函数返回的是MyString& 、Char& 等(引用),MyString、Char 等(传值)这得看你返回的对象是函数的局部变量还是全局变量(或者类当前对象成员变量);前者只能返回一个MyString、Char 等;后者强烈建议返回MyString& 、Char& 等(引用);

(6)有些函数的参数是const MyString& ,有些是MyString& (引用);这是为什么?前者是把外部值传提到子函数内部,且不允许改变;后者是作为函数的返回值传递进去的,返回的结果为函数的处理结果(而不用函数自身返回值了)。

二:下面是简单的实现了一下string类,参照的是STL源码,但是自己理解的还是不够深,难免有一些错误,请各位指教

(1)MyString.h文件

#ifndef MYSTRING_H
#define MYSTRING_H
#include "MyExcept.h"
#include 
#include 
const int INI_MAX = 0x7fffffff;//2^32npos
const int INI_MIN = 0x80000000;// -2^32
const int npos = 0xffffffff;// npos
using namespace std;

class MyString
{
  public:
  // constructor
  MyString();//
  MyString(const MyString &);//
  MyString(const char *);
  MyString(const size_t,const char);
  // destructor
  ~MyString();
  // attributes

  size_t length();// 字符串长度
  bool isEmpty();// 返回字符串是否为空
  const char* c_str();// 返回c风格的trr的指针
  // friend funs
  // read writer operations
  friend ostream& operator<< (ostream&, const MyString&);
  friend istream& operator>> (istream&, MyString&);
  //add operation
  friend MyString operator+(const MyString&,const MyString&);
  // compare operations
  friend bool operator==(const MyString&,const MyString&);
  friend bool operator!=(const MyString&,const MyString&);
  friend bool operator<(const MyString&,const MyString&);
  friend bool operator<=(const MyString&,const MyString&);
  friend bool operator>(const MyString&,const MyString&);
  friend bool operator>=(const MyString&,const MyString&);
  // 成员函数实现运算符重载,其实一般需要返回自身对象的,成员函数运算符重载会好一些
  // index operation
  char& operator[](const size_t);
  const char& operator[](const size_t)const;
  // =
  MyString& operator=(const MyString&);
  // +=
  MyString& operator+=(const MyString&);
  // +=
  //MyString operator+=(const MyString&); cannot be overloaded
  // 成员操作函数
  // substr
  MyString substr(size_t pos,const size_t n);
  // append
  MyString& append(const MyString&);
  //insert
  MyString& insert(size_t,const MyString&);
  //assign 替换
  MyString& assign(MyString&,size_t,size_t);
  // erase 删除
  MyString& erase(size_t,size_t);
  //find_first_of 查找某一个字符 size_t 是非符号数的,重载
  // 查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。
  //搜索从index开始,如果没找到就返回string::npos
  int find_first_of(const char* str,size_t index=0);
  int find_first_of(const char ch,size_t index=0);
  int find_first_of(const MyString &,size_t index=0);
  // 在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
  int find_first_not_of(const char* str,size_t index=0);
  int find_first_not_of(const char ch,size_t index=0);
  int find_first_not_of(const MyString&,size_t index=0);
  // swap
  void swap(MyString& lhs,MyString& rhs);
  // replace_all
  MyString& replace_all(const char oldc,const char newc=NULL);
  MyString& replace(size_t index,size_t num1,size_t num2,const char ch);
  //find
  int find(const char* str,size_t index=0);
  int find(const MyString& str,size_t index=0);
  int find(const char ch,size_t index=0);


  //private
  private:
  char *p_str;
  size_t strLength;
};
#endif // MYSTRING_H

(2)MyString.cpp文件

#include "MyString.h"
#include 

// constructor
  MyString::MyString():p_str(NULL),strLength(0){}

  MyString::MyString(const MyString &str)//
  {
    if(NULL == str.p_str)
    {
      return;
    }
    strLength = str.strLength;
    p_str = new char[strLength+1];
    strcpy(p_str,str.p_str);
  }
  MyString::MyString(const char *str)
  {
    if(NULL == str)
    {
      return;
    }
    strLength = strlen(str);
    p_str = new char[strLength+1];
    strcpy(p_str,str);
  }
  MyString::MyString(const size_t len,const char ch)
  {
    if(NULL == ch)
    {
      return;
    }
    strLength = len;
    p_str = new char[strLength+1];
    for(size_t i=0;i> (istream& in, MyString& str)
  {
    char tmp[100];// 临时字符串
    if(in>>tmp)
    {
      delete[] str.p_str;
      str.strLength = strlen(tmp);
      str.p_str = new char[str.strLength+1];
      strcpy(str.p_str,tmp);
    }
    return in;
  }
  // + 加
  MyString operator+(const MyString& lhs,const MyString& rhs)
  {
    MyString ret;
    ret.strLength = lhs.strLength + rhs.strLength;
    ret.p_str = new char[ret.strLength+1];
    strcpy(ret.p_str,lhs.p_str);
    strcat(ret.p_str,rhs.p_str);
    return ret;
  }
  // compare operations
  bool operator==(const MyString& lhs,const MyString& rhs)
  {
    return strcmp(lhs.p_str,rhs.p_str)==0?true:false;
  }
  bool operator!=(const MyString& lhs,const MyString& rhs)
  {
    return strcmp(lhs.p_str,rhs.p_str)!=0?true:false;
  }
  bool operator<(const MyString& lhs,const MyString& rhs)
  {
    return strcmp(lhs.p_str,rhs.p_str)<0?true:false;
  }
  bool operator<=(const MyString& lhs,const MyString& rhs)
  {
    return strcmp(lhs.p_str,rhs.p_str)<=0?true:false;
  }
  bool operator>(const MyString& lhs,const MyString& rhs)
  {
    return strcmp(lhs.p_str,rhs.p_str)>0?true:false;
  }
  bool operator>=(const MyString& lhs,const MyString& rhs)
  {
    return strcmp(lhs.p_str,rhs.p_str)>=0?true:false;
  }
  // 成员函数实现运算符重载
  // index operation
  char& MyString::operator[](const size_t index)
  {
    if(index<0 || index>=strLength)
    {
      throw Outofbond() ;
    }
    return p_str[index];
  }
  const char& MyString::operator[](const size_t index)const
  {
    if(index<0 || index>=strLength)
    {
      throw Outofbond();
    }
    return p_str[index];
  }
   // = 赋值构造函数(判断是否是自身) 为什么要这样删除呢?
  MyString& MyString::operator=(const MyString& other)
  {
    if(this != &other)
    {
      if(strLength=strLength)
    {
      throw Outofbond();
    }
    MyString ret;
    ret.strLength = n;
    //ret.p_str = new char[n+1];
    ret.p_str = new char[ret.strLength+1]; //也可以
    for(size_t i=0;i=strLength)
    {
      throw Outofbond();
    }
    char *p_old = p_str;
    strLength += other.strLength;
    p_str = new char[strLength+1];
    for(size_t i=0;i=strLength)
//    {
//      throw Outofbond();
//    }
    assert(pos>0 && posstrLength)
//    {
//      throw Outofbond();
//    }
//    size_t index = pos + n;
//    while(*(p_str+index)!='\0')
//    {
//      *(p_str+index-n) = *(p_str+index);
//      ++index;
//    }
//    *(p_str+index-n) = '\0';
//    return *this;
//  }
  // erase 删除 从pos开始的n个字符
  MyString& MyString::erase(size_t pos,size_t n)
  {
    if((pos+n)>strLength)
    {
      throw Outofbond();
    }
    char *p_old = p_str;
    strLength -= n;
    p_str = new char[strLength+1];
    for(size_t i=0;i=strLength)
      return npos;
    int tmp_len = strlen(str),j;
    size_t flag,min_index = INI_MAX;
    for(j=0;j=strLength)
      return npos;
    int j;
    size_t flag = npos;
    for(size_t i=index;i=strLength)
      return npos;
    int j;
    size_t flag,min_index = INI_MAX;
    for(j=0;j=strLength)
      return npos;
    size_t i=0,j=0;
    size_t tmp_len = strlen(str);
    for(i=index;i=strLength)
      return npos;
    size_t i=0,j=0;
    for(i=index;i=strLength)
      return npos;
    size_t i=0;
    for(i=index;i

(3)测试函数main.cpp

#include "MyString.h"
#include 
using namespace std;

int main()
{
  int n;
  int choose = 1;
  int p,l;
  char cs[100];
  MyString s1;
  MyString s2("hello");
  MyString s3 = "HELLO";
  cout << "***** welcome *****\n";
  cout << "******* MADE BY zyp **********\n";
  cout << "s1= " << s1 << "s2= " << s2 << "s3= " << s3 << endl;
  cout << "请输入一个长度小于100的字符串:例如world\n";
  cin >> s1;
  s1 = s1;
  //s1 = s1+s1;
  s1 += s1;
  MyString s4(s1);
  s4.append(s1);
  s2.insert(2,s3);
  s1.erase(4,4);
  s1.assign(s2,1,7);
  cout << "s1= " << s1 << "s2= " << s2 << "s3= " << s3 << "s4= " << s4 << endl;
  s2 = s4.substr(2,7);
  cout << "s4[3]= " << s4[3] << s4.length() << (s1>=s2) << "s4.substr() " << s2 << endl;
  cout << "s1.find_first_of(beLE,2):" << s1.find_first_of("beLE",2) << ",s1.find_first_of(a,3):" << s1.find_first_of('a',3) << ",s1.find_first_of(s3,2):" << s1.find_first_of(s3,2) << endl;
  MyString s5(5,'b');
  s5 += s5;
  //s5.append(s5);// 不知道为什就是不能append
  cout << "s5 = " << s5 << "s5.find_first_not_of(aeHLEOl,2):" << s5.find_first_not_of("aeHLEOl",2) << "s5.find_first_not_of(aeHLEOl,0):" << s5.find_first_not_of("aeHLEOl") << endl;
  cout << "s5.find_first_not_of(s1,2):" << s5.find_first_not_of(s1,2) << "s5.find_first_not_of(b,2):" << s5.find_first_not_of('b',2) << endl;
  swap(s1,s5);
  s5.replace_all('a','J');
  MyString s6("LLO");
  cout << s1 << "," << s5 << "s5.find(LLO,0) " << s5.find("LLO",0) << "s5.find(s6,0) " << s5.find(s5) << endl;
  cout << npos << endl;
  return 0;
}

三:感悟

(1)耗时将近2天的实现了它,自己与其从中学到了很多,倒不如说是重新认识了string类;

(2)自己知道这个简单的string类,距离string源代码还差的很远很远;但是它帮助我更好的理解了string类,至少会简单的应用了。

(3)简单的实现了一下string类,参照的是STL源码,但是自己理解的还是不够深,难免有一些错误,请各位指教,万分感谢!

(4)下一步进军list


分享题目:代码分析c++中string类
文章URL:http://cdkjz.cn/article/jhodes.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220