[此贴子已经被作者于2005-11-10 18:18:19编辑过]
以下这个文件为美国AP考试所用的,可以很好地对C++字符串进行处理.
#ifndef _PSTRING_H_
#define _PSTRING_H_
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
template <class T>
class pstringT
{
public:
pstringT<T>(); //default constructor
pstringT<T>(const pstringT<T> &); //copy constructor
pstringT<T>(const T *copy); //copy constructor from C-style string
pstringT<T>(T ch); //copy constructor from single character
virtual ~pstringT<T>(); //destructor
inline T * c_str() const; //returns a null-terminated, C-style string
inline int length() const; //returns the number of characters in the string
int find(const pstringT<T> &str) const; //return index of str or -1 if not found
int find(const T ch) const; //return index of ch or -1 if not found
pstringT<T> substr(int pos, int len) const; //returns substring from pos of length len
T & operator [] (int n); //access a character (mutable)
const T operator [] (int n) const; //access a character (immutable)
const pstringT<T> & operator = (const pstringT<T> &); //assignment operator
const pstringT<T> & operator = (const T * const); //assignment operator from C-style string
const pstringT<T> & operator = (const T); //assignment operator from single character
const pstringT<T> & operator += (const pstringT<T> &); //concatenation operator
const pstringT<T> & operator += (const T * const); //concatenation operator from C-style string
const pstringT<T> & operator += (const T); //concatenation operator from single character
protected:
T *mystring;
};
//typedef for regular pstrings
typedef pstringT<char> pstring;
//concatenation operators
template <class T> pstringT<T> operator + (const pstringT<T> &, const pstringT<T> &);
template <class T> pstringT<T> operator + (const pstringT<T> &, T);
template <class T> pstringT<T> operator + (T, const pstringT<T> &);
template <class T> pstringT<T> operator + (const pstringT<T> &, const T * const);
template <class T> pstringT<T> operator + (const T * const, const pstringT<T> &);
//stream operators
template <class T> inline ostream & operator << (ostream &, const pstringT<T> &);
template <class T> istream & operator >> (istream &, pstringT<T> &);
template <class T> istream & getline(istream &, pstringT<T> &);
//comparison operators
template <class T> inline bool operator == (const pstringT<T> &, const pstringT<T> &);
template <class T> inline bool operator != (const pstringT<T> &, const pstringT<T> &);
template <class T> inline bool operator < (const pstringT<T> &, const pstringT<T> &);
template <class T> inline bool operator <= (const pstringT<T> &, const pstringT<T> &);
template <class T> inline bool operator > (const pstringT<T> &, const pstringT<T> &);
template <class T> inline bool operator >= (const pstringT<T> &, const pstringT<T> &);
template <class T>
ostream & operator << (ostream &os, const pstringT<T> &out)
{
return os << out.c_str() << flush;
}
template <class T>
istream & operator >> (istream &is, pstringT<T> &in)
{
fflush(stdin);
T input_buffer[4096];
is >> input_buffer;
in = input_buffer;
return is;
}
template <class T>
istream & getline(istream &is, pstringT<T> &to_get)
{
fflush(stdin);
T getline_buffer[4096];
is.getline(getline_buffer, 4095);
to_get = getline_buffer;
return is;
}
template <class T>
pstringT<T>::pstringT()
{
mystring = new T[1];
mystring[0] = 0;
}
template <class T>
pstringT<T>::pstringT(const T *copy)
{
mystring = new T[strlen(copy) + 1]; //allocate memory
strcpy(mystring, copy); //copy string
}
template <class T>
pstringT<T>::pstringT(T ch)
{
mystring = new T[2];
mystring[0] = ch;
mystring[1] = 0;
}
template <class T>
pstringT<T>::pstringT(const pstringT<T> & to_create_from)
{
mystring = new T[to_create_from.length()+1];
strcpy(mystring, to_create_from.c_str()); //copy string
}
template <class T>
pstringT<T>::~pstringT()
{
delete[] mystring;
}
template <class T>
T* pstringT<T>::c_str() const
{
return mystring;
}
template <class T>
int pstringT<T>::length() const
{
return strlen(mystring);
}
template <class T>
int pstringT<T>::find(const pstringT<T> & str) const
{
int i, j, endsearch = length() - str.length() + 1;
for(i = 0; i < endsearch; i++)
{
for(; i < endsearch && mystring[i] != str[0]; i++);
if(i == endsearch)
break;
for(j = 0; j < str.length() && mystring[i+j] == str[j]; j++);
if(j == str.length())
return i;
}
return -1;
}
template <class T>
int pstringT<T>::find(const T ch) const
{
for(int i = 0; i < length(); i++)
if(mystring[i] == ch)
return i;
return -1;
}
template <class T>
pstringT<T> pstringT<T>::substr(int pos, int len) const
{
if(pos < 0 || len < 0 || pos >= length())
{
cerr << "\nError: substring (" << pos << "," << len
<< ") out of bounds for string \"" << mystring << '\"' << endl;
exit(1);
}
if(pos + len > length())
len = length() - pos;
T *result = new T[len + 1];
memcpy(result, mystring + pos, len * sizeof(T));
result[len] = 0;
pstringT<T> to_return(result);
delete[] result;
return to_return;
}
template <class T>
const pstringT<T> & pstringT<T>::operator = (const T * const to_copy)
{
delete[] mystring; //deallocate mem
mystring = new T[strlen(to_copy)+1];
strcpy(mystring, to_copy); //copy string
return *this;
}
template <class T>
const pstringT<T> & pstringT<T>::operator = (const T ch)
{
delete[] mystring; //deallocate memory
mystring = new T[2];
mystring[0] = ch;
mystring[1] = 0;
return *this;
}
template <class T>
const pstringT<T> & pstringT<T>::operator = (const pstringT<T> ©)
{
return *this = copy.c_str(); //call T pointer copier
}
template <class T>
const pstringT<T> & pstringT<T>::operator += (const T * const to_append)
{
T *newbuffer = new T[length() + strlen(to_append) + 1];
strcpy(newbuffer, mystring);
strcat(newbuffer, to_append);
delete[] mystring;
mystring = newbuffer;
return *this;
}
template <class T>
const pstringT<T> & pstringT<T>::operator += (const pstringT<T> &to_append)
{
return *this += to_append.c_str(); //append T pointer
}
template <class T>
const pstringT<T> & pstringT<T>::operator += (const T to_append)
{
T *newstring = new T[length()+2];
strcpy(newstring, mystring);
delete[] mystring;
mystring = newstring; //points to new string
mystring[length()+1] = 0; //null terminator
mystring[length()] = to_append;
return *this;
}
template <class T>
pstringT<T> operator + (const pstringT<T> & lval, const pstringT<T> & rval)
{
pstringT<T> to_return(lval);
return to_return += rval;
}
template <class T>
pstringT<T> operator + (const pstringT<T> & lval, const T & rval)
{
pstringT<T> to_return(lval);
return to_return += rval;
}
template <class T>
pstringT<T> operator + (T lval, const pstringT<T> & rval)
{
pstringT<T> to_return(lval);
return to_return += rval;
}
template <class T>
pstringT<T> operator + (const pstringT<T> & lval, const T * const rval)
{
pstringT<T> to_return(lval);
return to_return += rval;
}
template <class T>
pstringT<T> operator + (const T * const lval, const pstringT<T> & rval)
{
pstringT<T> to_return(lval);
return to_return += rval;
}
template <class T>
T & pstringT<T>::operator [] (int n)
{
if(n<0 || n>=length())
{
cerr << "\nError: index out of range: " << n << " in string \""
<< mystring << "\" of length " << length() << endl;
exit(1);
}
return mystring[n];
}
template <class T>
const T pstringT<T>::operator [] (int n) const
{
if(n<0 || n>=length())
{
cerr << "\nError: index out of range: " << n << " in string \""
<< mystring << "\" of length " << length() << endl;
exit(1);
}
return mystring[n];
}
template <class T>
bool operator == (const pstringT<T> &lval, const pstringT<T> &rval)
{
return strcmp(lval.c_str(), rval.c_str()) == 0;
}
template <class T>
bool operator != (const pstringT<T> &lval, const pstringT<T> &rval)
{
return strcmp(lval.c_str(),rval.c_str()) != 0;
}
template <class T>
bool operator < (const pstringT<T> &lval, const pstringT<T> &rval)
{
return strcmp(lval.c_str(), rval.c_str()) < 0;
}
template <class T>
bool operator <= (const pstringT<T> &lval, const pstringT<T> &rval)
{
return strcmp(lval.c_str(), rval.c_str()) <= 0;
}
template <class T>
bool operator > (const pstringT<T> &lval, const pstringT<T> &rval)
{
return strcmp(lval.c_str(), rval.c_str()) > 0;
}
template <class T>
bool operator >= (const pstringT<T> &lval, const pstringT<T> &rval)
{
return strcmp(lval.c_str(), rval.c_str()) >= 0;
}
#endif