<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">本文实现一个简单的string类,参考自c++ primer plus这本书,源码如下:</span>
//======================================================
// FileName: MyString.h
// Author: sjhuangx
// Date: 2016-03-03
// Desc: a single string class
//=======================================================
#ifndef MYSTRING_H_INCLUDED
#define MYSTRING_H_INCLUDED
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using std::ostream;
using std::istream;
class MyString
{
private:
char *str; // pointer to string
int len; // the length of string
static int num_strings; // the number of objects
static const int CINLIM = 80; // the limit of cin number
public:
MyString(const char* st); // constructor from C string
MyString(); // default construtor
MyString(const MyString& st); // copy constructor
~MyString(); // destructor
int Length() const { return len; } // return the length of string
// overload operator methods
MyString& operator=(const MyString& st);
MyString& operator=(const char* s);
char& operator[](int i);
const char& operator[](int i) const;
// overload friend operator
friend bool operator<(const MyString& str1, const MyString& str2);
friend bool operator>(const MyString& str1, const MyString& str2);
friend bool operator==(const MyString& str1, const MyString& str2);
friend ostream& operator<<(ostream& os, const MyString& st);
friend istream& operator>>(istream& is, MyString& st);
// static function
int HowMany();
};
#endif // MYSTRING_H_INCLUDED
以下是类的实现部分:
//======================================================
// FileName: MyString.h
// Author: sjhuangx
// Date: 2016-03-03
// Desc: a single string class
//=======================================================
#include "MyString.h"
// static member
int MyString::num_strings = 0;
// static function
int MyString::HowMany()
{
return num_strings;
}
// class methods
MyString::MyString(const char * s) // construct string from C string
{
if (s == NULL)
{
len = 0; // set size
str = new char[1]; // allot storage
str[0] = '\0'; // initialize pointer
}
else
{
len = std::strlen(s); // set size
str = new char[len + 1]; // allot storage
std::strcpy(str, s); // initialize pointer
}
num_strings++; // set object count
}
// default constructor
MyString::MyString()
{
len = 0;
str = new char[1];
str[0] = '\0'; // default string
num_strings++;
}
// necessary destructor
MyString::~MyString()
{
--num_strings; // update static member
delete[] str; // delete the space
len = 0; // reset len
}
// copy constructor
MyString::MyString(const MyString& st)
{
len = st.len; // same length
str = new char[len + 1]; // allot space
std::strcpy(str, st.str); // copy date to new location
++num_strings; // handle static member update
}
//============ overload operator methods =============
// assign String to String
MyString& MyString::operator=(const MyString& st)
{
if (this == &st)
{
return *this;
}
delete[] str;
len = st.len;
str = new char[len + 1];
std::strcpy(str, st.str);
return *this;
}
// assign a C string to MyString
MyString& MyString::operator=(const char* st)
{
if (st == NULL)
{
return *this;
}
delete[] str;
len = std::strlen(st);
str = new char[len + 1];
std::strcpy(str, st);
return *this;
}
// read-write char access for non-const Strings
char& MyString::operator[](int i)
{
if (i > len) // if i large than length of string, then return the last of string
{
i = len;
}
if (i < 0) // if i less then 0, than return the first of string
{
i = 0;
}
return str[i];
}
// read-only char access for const String
const char& MyString::operator[](int i) const
{
if (i > len) // if i large than length of string, then return the last of string
{
i = len;
}
if (i < 0) // if i less then 0, than return the first of string
{
i = 0;
}
return str[i];
}
//============== overload friend operators ===============
// overload less operator
bool operator<(const MyString& str1, const MyString& str2)
{
return (std::strcmp(str1.str, str2.str) < 0);
}
bool operator>(const MyString& str1, const MyString& str2)
{
return (std::strcmp(str1.str, str2.str) > 0);
}
bool operator==(const MyString& str1, const MyString& str2)
{
return (std::strcmp(str1.str, str2.str) == 0);
}
// simple string outpout
ostream& operator<<(ostream& os, const MyString& st)
{
os << st.str;
return os;
}
// simple string input
istream& operator>>(istream& is, MyString& st)
{
char temp[MyString::CINLIM];
is.get(temp, MyString::CINLIM);
if (is)
{
st = temp;
}
while (is && is.get() != '\n')
{
continue;
}
return is;
}
简单测试如下:
#include "MyString.h"
using namespace std;
int main(int argc, char** argv)
{
MyString mystr;
mystr = "hello world";
cout << mystr << endl;
system("pause");
return 0;
}