c++ string的实现。

时间:2022-01-12 14:25:30

第三次做了。只是做个复习。偶然发现之前的版本有内存泄露。基本功还是不过关。这次应该没有内存泄漏了。虽然是个简单版本。

1)了解堆,栈,值copy。

2)几个常用的c的字符函数和c中的char 如何表示串。和c++的string不同。

3)string。自动有‘\0’,  。 "hi.",这样一个常字符串,编译器也是会给'\0'的。char [3]={xxx,'\0'} 必须自己加。

main.cpp

#include <iostream>
#include "Mystring.h"
using namespace std; void main_mystring();
int main()
{
main_mystring(); return ;
} //mystring g_hi("hia");
//void main_mystring()
//{
// mystring a;
// mystring b("hi");
// mystring c=mystring("linson");
// mystring d=c;
// d=b;
// d[2]='a';
//} void main_mystring()
{
Mystring hi("hi");
Mystring hi2="h2";
Mystring c=hi;
c=c;
cout<<c<<endl; c[]='x'; cout<<c<<endl; Mystring emptystr;
cout<<emptystr<<endl;
emptystr=hi;
cout<<emptystr<<endl; Mystring add=hi+hi2;
cout<<add<<endl; }
Mystring.h
#ifndef MYSTRING_H_INCLUDED
#define MYSTRING_H_INCLUDED #include <stdio.h>
#include <iostream>
using namespace std; class Mystring
{
public:
Mystring(char* const);
Mystring();
Mystring(const Mystring&);
Mystring& operator=(const Mystring&);
char& operator[](unsigned int);
Mystring operator+(const Mystring& right);
~Mystring();
private:
Mystring(char * const,unsigned int);//专给+操作符使用.之前的版本应该内存泄漏了.
unsigned int CharSize;
char* pChar;
friend ostream& operator<<(ostream& os,const Mystring& mys);
}; ostream& operator<<(ostream& os,const Mystring& mys); #endif // MYSTRING_H_INCLUDED
Mystring.cpp
#include "Mystring.h"
#include "malloc.h"
#include "string.h"
#include <stdexcept> using namespace std; Mystring::Mystring(char* const _pchar)
{
int Length=;
char* pflag=_pchar;

if(_pchar==0)
{
  throw runtime_error("null pointer");
}

while(*pflag!=0x0 && Length<*)////strlen还是感觉不安全.如果本来形参就是一个没有\0结尾的符号呢.长度怕出错.随便假设最大为1m长度的字符串吧.
{
++Length;
++pflag;
}
pChar=(char *)malloc(Length+);
//cout<<"new"<<(void *)pChar<<endl;
if(pChar!=)
{
strncpy(pChar,_pchar,Length);
}
else
{
throw runtime_error("alloc error.");
}
CharSize=Length;
pChar[CharSize]='\0'; } Mystring::Mystring(const Mystring& source)
{
pChar=(char *)malloc(source.CharSize+);
if(pChar==)
{
throw runtime_error("alloc error.");
}
//cout<<"new"<<(void *)pChar<<endl;
strncpy(pChar,source.pChar,source.CharSize);
CharSize=source.CharSize;
pChar[CharSize]='\0';
} Mystring::Mystring()
{
pChar=(char *)malloc();
//cout<<"new"<<(void *)pChar<<endl;
if(pChar==)
{
throw runtime_error("alloc error.");
}
CharSize=;
pChar[CharSize]='\0'; } Mystring& Mystring::operator=(const Mystring& source)
{
  string temp=string(source);   
  std::swap(pChar,temp.pChar); return *this;
    return *this;
}

char& Mystring::operator[](unsigned int index)
{
if(index>=&& index<CharSize)
{
return pChar[index];
}
else
{
throw runtime_error("over range!");
}
} Mystring Mystring::operator+(const Mystring& right)
{ char * temppChar=(char *)malloc(this->CharSize+right.CharSize+);
//cout<<"new"<<(void *)temppChar<<endl;
if(pChar==)
{
throw runtime_error("alloc error.");
}
strncpy(temppChar,this->pChar,this->CharSize);
strncpy(temppChar+this->CharSize,right.pChar,right.CharSize);
int tempCharSize=this->CharSize+right.CharSize;
temppChar[tempCharSize]='\0';
return Mystring(temppChar,tempCharSize);
//Mystring()
} Mystring::Mystring(char * const _p,unsigned int _size)
{
pChar=_p;
CharSize=_size;
} Mystring::~Mystring()
{
//cout<<"del"<<(void *)pChar<<endl;
free(pChar);
} ostream& operator<<(ostream& os,const Mystring& mys)
{
return os<<mys.pChar;
}