strclass.h

来自「经典c++程序的实现」· C头文件 代码 · 共 124 行

H
124
字号

#include <iostream.h>
#include <string.h>
#include <stdlib.h>

#include "..\include\book.h"

const int outOFMemory = 0, indexError = 1;

class String {
  private :  // pointer to the 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 {
		switch(errorType)  {
		  case outOFMemory : cout << "Out of Memory!\n";
		  				     break;
		  case indexError : cout << "Index error!\n";
						    cout << "bad index : " << badIndex << "\n";
						    break;
		  default : break;
		}
		exit(-1);
	}

  public:
    String(char *s="");     // constructors
    String(const String& s);

    ~String(void);          // destructor

    // assignment operators

    String& operator = (const String& s);    // String = String
    String& operator = (char *s);            // String = C++ String

    // relational operators

    // String == C++ String, String == String, C++ String == String
    int operator == (char *s) const;
    int operator == (const String& 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 + C++ String, String + String, C++ String + String
    // String += String, String += C++ String
    String operator + (char *s) const;
    String operator + (const String& s) const;
    friend String operator + (char *str, const String& s);
    void operator += (const String& s);
    void operator += (char *s);

    // String functions

    // begin at 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 to String
    void Insert(const String& s, int index);

    // insert a C++ String
    void Insert(char *s, int index);

    // delet 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& istr=cin, char delimiter='\n');

    // additional methods
    Length(void) const;
    int IsEmpty(void) const;
    void Clear(void);
};

void Get_NextArr(const String pat, int next[]);
void Findpat(const String target, const String pat, int& index);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?