📄 core.h
字号:
//====================================================================// 文件名: core.h//// 文件描述:// ------------------------------------------------------------------// 跨平台通用接口头文件// ------------------------------------------------------------------//// 时间: 2002.9// 编程: 喻宜// ------------------------------------------------------------------// 修改说明(请按格式说明)...//====================================================================#ifndef _Y_CORE_H_#define _Y_CORE_H_#include <time.h>#include <stdio.h>#include <string.h>#include <iostream.h>
#include <assert.h>//===========================================// 字符串处理类//-------------------------------------------class C_String
{ // 重载操作符"+"号 friend C_String operator + (const C_String& i_ysStr1, const C_String& i_ysStr2); friend C_String operator + (const C_String& i_ysStr, const char* i_pStr); friend C_String operator + (const char *i_pStr, const C_String& i_ysStr); friend C_String operator + (const C_String& i_ysStr, char i_mChar); friend C_String operator + (char i_mChar, const C_String& i_ysStr); // 重载操作符"<<"号 friend ostream& operator << (ostream& i_os, const C_String& i_ysStr);public: // 构造函数及析构函数 C_String(); C_String(int i_nInitSize); C_String(int i_nInitSize, const char* i_pStr); C_String(char i_mChar, int i_nCount); C_String(const char* i_pStr); C_String(const char* i_pStr, int i_nLen); // 只拷贝部分字符串 C_String(const C_String& i_ysStr); // 复制构造函数 ~C_String(); // 强制类型转换 operator const char* () const; bool addSize(int i_nSizeAdd); // 增加缓冲区大小 bool setSize(int i_nSize); // 设置缓冲区大小 const char* cstr() const; // 获取字符串指针 char* str(); // 获取字符串指针 int length() const; // 获取字符串长度 bool isEmpty() const; // 是否为空 void toUpper(); // 全部转换为大写形式 void toLower(); // 全部转换为小写形式 void toReverse(); // 全部转换为相反形式 C_String left(int i_len) const; // 从字符串左开始向右取len长度的字符串 C_String right(int i_len) const; // 从字符串右开始向左取len长度的字符串 C_String mid(int i_index, int i_len=-1) const; // 从字符串中index处开始向右取len长度的字符串 void trimLeft(); void trimRight(); void trimAll(); char operator[](int i_nIndex) const; char& operator[](int i_nIndex); int toInt(); // 将字符串转换为整型数 double toReal(); // 将字符串转换为浮点数 // 在index处插入字符串s bool addBefore(int i_nIndex, const char* i_pStr); bool addAfter(int i_nIndex, const char* i_pStr); // 从index处开始删除长度为len的字符串 bool removeAt(int i_nIndex, int i_nLen); // 在index处用字符串s代替长度为len的字符串 bool replaceAt(int i_nIndex, int i_nLen, const char* i_pStr); void clear(); // 清空 void freeExtra(); // 清掉多余内存 // 从Index处开始搜索第num次出现字符串s的位置 int find(const char* i_pStr, int i_nNum=1, int i_nIndex=0) const; // 从Index处开始不区分大小写搜索第num次出现字符串s的位置 int findNoCase(const char* i_pStr, int i_nNum=1, int i_nIndex=0) const; // 从index处开始总共有出现字符串s的个数 int strCount(const char* i_pStr, int i_nIndex=0) const; // 从index处开始不区分大小写总共有出现字符串s的个数 int strCountNoCase(const char* i_pStr, int i_nIndex=0) const; int replaceFind(const char* i_pSrc, const char *i_pDst, int i_nIndex=0); C_String& format(const char* i_pFormat, ...); // 格式化字符串 // 重载操作符"="号 C_String& operator = (const C_String& i_ysStr); C_String& operator = (const char* i_pStr); C_String& operator = (char i_mChar); C_String& operator = (int i_nVal); // 重载操作符"+="号 C_String& operator += (const C_String& i_ysStr); C_String& operator += (const char* i_pStr); C_String& operator += (char i_mChar); C_String& operator += (int i_nVal); // 重载判断操作符号 bool operator == (const char* i_pStr); bool operator == (const C_String& i_ysStr); bool operator != (const char* i_pStr); bool operator != (const C_String& i_ysStr); bool operator >= (const char* i_pStr); bool operator >= (const C_String& i_ysStr); bool operator <= (const char* i_pStr); bool operator <= (const C_String& i_ysStr); bool operator > (const char* i_pStr); bool operator > (const C_String& i_ysStr); bool operator < (const char* i_pStr); bool operator < (const C_String& i_ysStr);protected: bool addNum(int i_nNumAdd);private: char* m_pBuf; // 字符串指针 int m_nLen; // 字符个数 int m_nSize; // 缓冲区长度};/**重载"<<"*/inline ostream& operator << (ostream& i_os, const C_String& i_ysStr)
{ return i_os << i_ysStr.cstr();}/**强制类型转换*/inline C_String::operator const char* () const
{ return m_pBuf;}/**获取字符串指针*/inline const char* C_String::cstr() const
{ return m_pBuf;}/**获取字符串指针*/inline char* C_String::str()
{ return m_pBuf;}/**获取字符串长度*/inline int C_String::length() const
{ return m_nLen;}/**是否为空*/inline bool C_String::isEmpty() const
{ return m_nLen<=0;}/**重载"=="*/inline bool C_String::operator == (const char* i_pStr)
{ if (i_pStr == NULL) return false; return (strcmp(m_pBuf, i_pStr) == 0);}/**重载"=="*/inline bool C_String::operator == (const C_String& i_ysStr)
{ return (strcmp(m_pBuf, i_ysStr.cstr()) == 0);}/**重载"!="*/inline bool C_String::operator != (const char* i_pStr)
{ if (i_pStr == NULL) return true; return (strcmp(m_pBuf, i_pStr) != 0);}/**重载"!="*/inline bool C_String::operator != (const C_String& i_ysStr)
{ return (strcmp(m_pBuf, i_ysStr.cstr()) != 0);}/**重载">="*/inline bool C_String::operator >= (const char* i_pStr)
{ if (i_pStr == NULL) return true; return (strcmp(m_pBuf, i_pStr) >= 0);}/**重载">="*/inline bool C_String::operator >= (const C_String& i_ysStr)
{ return (strcmp(m_pBuf, i_ysStr.cstr()) >= 0);}/**重载"<="*/inline bool C_String::operator <= (const char* i_pStr)
{ if (i_pStr == NULL) return false; return (strcmp(m_pBuf, i_pStr) <= 0);}/**重载"<="*/inline bool C_String::operator <= (const C_String& i_ysStr)
{ return (strcmp(m_pBuf, i_ysStr.cstr()) <= 0);}/**重载">"*/inline bool C_String::operator > (const char* i_pStr)
{ if (i_pStr == NULL) return true; return (strcmp(m_pBuf, i_pStr) > 0);}/**重载">"*/inline bool C_String::operator > (const C_String& i_ysStr)
{ return (strcmp(m_pBuf, i_ysStr.cstr()) > 0);}/**重载"<"*/inline bool C_String::operator < (const char* i_pStr)
{ if (i_pStr == NULL) return false; return (strcmp(m_pBuf, i_pStr) < 0);}/**重载"<"*/inline bool C_String::operator < (const C_String& i_ysStr)
{ return (strcmp(m_pBuf, i_ysStr.cstr()) < 0);}//=============================================================// 动态数组模板类//-------------------------------------------------------------template <class T>class C_Array{public: C_Array(); C_Array(int i_nSize); C_Array(const C_Array<T>& i_yArray); C_Array<T>& operator= (const C_Array<T>& i_yArray); ~C_Array(); bool isEmpty() const; int size() const; int count() const; bool addSize(int i_nSizeAdd); bool addCount(int i_nNumAdd = 1); // 默认为1个 bool setAt(int i_nIndex, const T& i_yElement); bool setFirst(const T& i_yElement); bool setLast(const T& i_yElement); T& operator[](int i_nIndex); T operator[](int i_nIndex) const; bool at(int i_nIndex, T& o_yElement); T& at(int i_nIndex); T at(int i_nIndex) const; T* pAt(int i_nIndex); bool first(T& o_yElement); T& first(); T first() const; T* pFirst(); bool last(T& o_yElement); T& last(); T last() const; T* pLast(); bool addBefore(int i_nIndex, const T& i_yElement); // 插入一个 bool addAfter(int i_nIndex, const T& i_yElement); // 插入一个 bool addFirst(const T& i_yElement); // 在最前增加一个 bool addLast(const T& i_yElement); // 在最后增加一个 bool removeAt(int i_nIndex); // 删除其中一个 bool removeFirst(); // 删除最前一个 bool removeLast(); // 删除最后一个 void clear(); // 清空 bool freeExtra(); // 清掉多余内存private: T *m_pBuf; int m_nCount; // 实际元素个数 int m_nSize; // 最多容纳元素个数};/**\class C_Array ytempl.h\brief 动态数组类*//**默认构造函数,默认增长率为50%.\sa setRate*/template <class T>inline C_Array<T>::C_Array():m_pBuf(NULL),m_nCount(0),m_nSize(0){}/**类似定义数组方式的构造函数\param i_nSize 创建个数*/template <class T>inline C_Array<T>::C_Array(int i_nSize):m_nSize(i_nSize),m_nCount(0){ m_pBuf = new T[i_nSize];}/**复制构造函数*/template <class T>C_Array<T>::C_Array(const C_Array<T>& i_yArray){ m_nSize = i_yArray.m_nSize; m_nCount = i_yArray.m_nCount; m_pBuf = new T[m_nSize]; for (int i=0; i<m_nSize; ++i) { m_pBuf[i] = i_yArray.m_pBuf[i]; }}/**重载=号*/template <class T>C_Array<T>& C_Array<T>::operator=(const C_Array<T> &i_yArray){ if (this != &i_yArray) { m_nSize = i_yArray.m_nSize; m_nCount = i_yArray.m_nCount; m_pBuf = new T[m_nSize]; for (int i=0; i<m_nSize; ++i) { m_pBuf[i] = i_yArray.m_pBuf[i]; } } return *this;}/**析构函数*/template <class T>inline C_Array<T>::~C_Array(){ if (m_pBuf != NULL) delete []m_pBuf;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -