📄 strclass.h
字号:
#ifndef STRING_CLASS_lkbk
#define STRING_CLASS_lkbk
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#ifndef NULL
const int NULL = 0;
#endif // NULL
const int outOfMemory = 0, indexError = 1;
class String
{
private:
// pointer to dynamically allocated string. length
// of the string includes the NULL character
char *str;
int size;
// error reporting function
void Error(int errorType, int badIndex = 0) const;
public:
// constructors
String(char *s = "");
String(const String& s);
// destructor
~String(void);
// assignment operators
// String = String, String = C++String
String& operator= (const String& s);
String& operator= (char *s);
// relational operators
// String==String,String==C++String,C++String==String
int operator== (const String& s) const;
int operator== (char *s) const;
friend int operator== (char *str, const String& s);
// String!=String,String!=C++String,C++String!=String
int operator!= (const String& s) const;
int operator!= (char *s) const;
friend int operator!= (char *str, const String& s);
// String<String,String<C++String,C++String<String
int operator< (const String& s) const;
int operator< (char *s) const;
friend int operator< (char *str, const String& s);
// String<=String,String<=C++String,C++String<=String
int operator<= (const String& s) const;
int operator<= (char *s) const;
friend int operator<= (char *str, const String& s);
// String>String,String>C++String,C++String>String
int operator> (const String& s) const;
int operator> (char *s) const;
friend int operator> (char *str, const String& s);
// String>=String,String>=C++String,C++String>=String
int operator>= (const String& s) const;
int operator>= (char *s) const;
friend int operator>= (char *str, const String& s);
// String concatenation operators
// String+String, String+C++String, C++String+String
// String += String, String += C++String
String operator+ (const String& s) const;
String operator+ (char *s) const;
friend String operator+ (char *str,const String& s);
void operator+= (const String& s);
void operator+= (char *s);
// String functions
// beginning at index start, find c
int Find(char c, int start) const;
// find last occurrence of c
int FindLast(char c) const;
// extract a substring
String Substr(int index, int count) const;
// insert a String into String
void Insert(const String& s, int index);
// insert a C++String into String
void Insert(char *s, int index);
// delete a substring
void Remove(int index, int count);
// String indexing
char& operator[] (int n);
// convert String to C++String
operator char* (void) const;
// String I/O
friend ostream& operator<< (ostream& ostr,
const String& s);
friend istream& operator>> (istream& istr,
String& s);
// read characters up to delimiter
int ReadString(istream& is=cin,
char delimiter='\n');
// additional methods
int Length(void) const;
int IsEmpty(void) const;
void Clear(void);
};
void String::Error(int errorType, int badIndex) const
{
if (errorType == outOfMemory)
cerr << "Memory exhausted!" << endl;
else
cerr << "Index " << badIndex
<< " out of range" << endl;
exit(1);
}
// constructor. allocate memory and copy in a C++String
String::String(char *s)
{
// length includes the NULL character
size = strlen(s) + 1;
// make room for string and NULL char and copy s.
str = new char [size];
// terminate program if memory is exhausted.
if (str == NULL)
Error(outOfMemory);
strcpy(str,s);
}
// copy constructor
String::String(const String& s)
{
// current object as length of s
size = s.size;
// allocate same amount of space as s uses. copy string
str = new char [size];
if (str == NULL)
Error(outOfMemory);
strcpy(str,s.str);
}
// destructor
String::~String(void)
{
delete [] str;
}
// assignment operator. String to String
String& String::operator= (const String& s)
{
// if sizes differ, delete current string and reallocate
if (s.size != size)
{
delete [] str;
str = new char [s.size];
if(str == NULL)
Error(outOfMemory);
// assign size to be size of s
size = s.size;
}
// copy s.str and return reference to current object
strcpy(str,s.str);
return *this;
}
// assignment operator. C++String to String
String& String::operator= (char *s)
{
int slen = strlen(s) + 1;
// if sizes differ, delete current string and reallocate
if (slen != size)
{
delete [] str;
str = new char [slen];
if (str == NULL)
Error(outOfMemory);
size = slen;
}
// copy s and return reference to current object
strcpy(str,s);
return *this;
}
// all relational operators use C++ string function strcmp
// String == String
int String::operator== (const String& s) const
{
return strcmp(str,s.str) == 0;
}
// String == C++String
int String::operator== (char *s) const
{
return strcmp(str,s) == 0;
}
// C++String == String. this is a friend function, since
// the left operand is a C++String.
int operator== (char *str, const String& s)
{
return strcmp(str,s.str) == 0;
}
// String != String
int String::operator!= (const String& s) const
{
return strcmp(str,s.str) != 0;
}
// String != C++String
int String::operator!= (char *s) const
{
return strcmp(str,s) != 0;
}
// C++String != String
int operator!= (char *str, const String& s)
{
return strcmp(str,s.str) != 0;
}
// String < String
int String::operator< (const String& s) const
{
return strcmp(str,s.str) < 0;
}
// String < C++String
int String::operator< (char *s) const
{
return strcmp(str,s) < 0;
}
// C++String < String
int operator< (char *str, const String& s)
{
return strcmp(str,s.str) < 0;
}
// String <= String
int String::operator<= (const String& s) const
{
return strcmp(str,s.str) <= 0;
}
// String <= C++String
int String::operator<= (char *s) const
{
return strcmp(str,s) <= 0;
}
// C++String <= String
int operator<= (char *str, const String& s)
{
return strcmp(str,s.str) <= 0;
}
// String > String
int String::operator> (const String& s) const
{
return strcmp(str,s.str) > 0;
}
// String > C++String
int String::operator> (char *s) const
{
return strcmp(str,s) > 0;
}
// C++String > String
int operator> (char *str, const String& s)
{
return strcmp(str,s.str) > 0;
}
// String >= String
int String::operator>= (const String& s) const
{
return strcmp(str,s.str) >= 0;
}
// String >= C++String
int String::operator>= (char *s) const
{
return strcmp(str,s) >= 0;
}
// C++String >= String
int operator>= (char *str, const String& s)
{
return strcmp(str,s.str) >= 0;
}
// concatention: String + String
String String::operator+ (const String& s) const
{
// build the new string with length len in temp
String temp;
int len;
// delete the NULL string created when temp declared
delete [] temp.str;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -