面试常常用到string类的实现,自己总结了一下:
#pragma once #include <iostream>
#include <cassert>
#include <iomanip>
using namespace std; class string{ friend ostream& operator <<(ostream&,string&);
friend istream& operator >>(istream&,string&); public:
string(const char* str = NULL);
//assignment constructor
string(const string& str);
~string(){
delete [] m_data;
} //get the length of string
int size(){
return mystrlen(m_data);
}
//return substring 这里存在一些问题待改善
//当字符串的长度稍大一些时会出错;
string substr(int pos);
string substr(int pos,int len);
//overload operator =
string& operator =(const string& other);
//overload operator +
string operator +(const string& other)const;
//overload operator +=
string& operator +=(const string& other);
//overload operator ==
bool operator ==(const string& other)const;
//overload operator []
char& operator [](unsigned int); private:
char* m_data;
};
cpp
#include "string.h" string::string(const char* str /* = NULL */)
{
if (!str)
{
m_data = new char[];
*m_data = '\0';
}
else
{
int nLen = strlen(str);
m_data = new char[nLen + ];
strcpy(m_data,str);
}
} string::string(const string& str)
{ m_data = new char[strlen(str.m_data)+];
strcpy(m_data,str.m_data);
} ostream& operator <<(ostream& oscout,string& str)
{
oscout<<str.m_data;
return oscout;
} istream& operator >>(istream& iscin,string& str)
{
char temp[];
iscin>>setw()>>temp;
str = temp;
return iscin;
} string string::substr(int pos)
{
/*
int nLen = strlen(m_data);
char* res = new char(nLen - pos +1);
char* src = m_data + pos;
strncpy(res,src,nLen-pos);
string strres(res);
return strres;
*/ int nLen = strlen(m_data);
if (nLen < pos)
{
pos = nLen;
}
int nCopyLen = nLen - pos;
char* res = new char(nLen - pos +);
char* dest = res;
char* src = m_data + pos; for (int i = ;i < nCopyLen; i++)
{
*dest++ = *src++;
} *dest = '\0';
string strres(res);
return strres;
} string string::substr(int pos,int len)
{
int maxlen = strlen(m_data)-pos;
if (maxlen < ) maxlen = ;
int newLen = maxlen < len ? maxlen : len;
char* res = new char(newLen+);
char* src = m_data + pos;
strncpy(res,src,newLen);
*(res+newLen) = '\0';
string strres(res);
return strres;
} string string::operator +(const string& other)const
{
string newString;
if(!other.m_data)
newString = *this;
else if(!m_data)
newString = other;
else
{
newString.m_data = new char[strlen(m_data)+strlen(other.m_data)+];
strcpy(newString.m_data,m_data);
strcat(newString.m_data,other.m_data);
}
return newString;
} string& string::operator =(const string& other)
{
if (this==&other)
return *this; delete [] m_data; m_data = new char[strlen(other.m_data)+];
strcpy(m_data,other.m_data); return *this;
} string& string::operator +=(const string& other)
{ char* data = new char[strlen(m_data)+strlen(other.m_data)+];
strcpy(data,m_data);
delete [] m_data;
m_data = data;
strcat(m_data,other.m_data); return *this;
} //overload operator ==
bool string::operator ==(const string& other)const
{
if ( strlen(other.m_data) != strlen(m_data) )
return false;
return strcmp(m_data,other.m_data)?false:true;
}
//overload operator []
char& string::operator [](unsigned int index)
{
if (index >= && index <= strlen(m_data))
return m_data[index]; return m_data[strlen(m_data)+];
}
另外转置输出string例子
#include <iostream>
#include <string>
using namespace std; //只限英文字符
void ReverseString(string& str)
{
int nLen = str.size(); for (int i = ; i < nLen/; i++)
{
char cTmp = str[nLen-i-];
str[nLen-i-] = str[i];
str[i] = cTmp;
}
}
//中英字符
void ReverseString1(string& str)
{
int nLen = str.size(); string strTmp;
strTmp.resize(nLen,' '); for (int i =; i < nLen; i++)
{
if ( str[i] < && str[i] > )
{
strTmp[nLen-i-] = str[i];
}
else
{
strTmp[nLen-i-] = str[i];
strTmp[nLen-i-] = str[i+];
++i;
}
}
str = strTmp;
} int main(int argc, char* argv[])
{
int nflag = ; while (nflag)
{
cout<<"enter a string including both lowercase and upercase letters!"<<endl;
string str;
cin.clear();
cin.sync();
getline(cin,str);
ReverseString1(str);
cout<<str<<endl;
cout<<"enter 0 to exit or 1 to continue"<<endl;
cin>>nflag;
} return ;
}