Borland C++ 语法摘要

常用抽象数据类型:
包括TList(链表)类、AnsiString类、Set(集合)类、DynamicArray(动态数组)类和 TStream(流)类。
 
TList类
实现了线性链表,可以存储任意类的对象。
虽然它是链表,但是它实际上是一个存放指针的数组,可以通过其 Items 属性象访问一个数组那样实现对 List对象的每一个元素的访问。
 
主要属性:
Capacity 容量
Count    长度
需要注意的是 Count 属性并不一定是 TList 对象中元素的个数,它也包含 Items 数组中的空指针的数目。如果要删除数组中的空指针,可以使用 List的Pack方法。
Items   通过此属性可以象数组那样访问链表中的元素。 
List    获取直接进入列表中元素数组的指针。
 
主要方法:
 
int __fastcall Add(void * Item);
需要注意的是,不管Items数组中有没有NULL元素,Add方法都只是在数组的末尾添加元素。
 
virtual void __fastcall Clear(void); 
清除链表中的所有元素。释放内存,并将Capacity清0.
 
void __fastcall Delete(int Index); 
提示:不会释放内存,也不会修改Capacity。
 
void __fastcall Exchange(int Index1, int Index2); 
交换两个元素的位置。
 
TList* __fastcall Expand(void); 
需要时(Count=Capacity)扩容到2的X次方倍。
 
int __fastcall IndexOf(void * Item);
查找元素。
 
void __fastcall Insert(int Index, void * Item);  
插入元素 到指定位置。
 
void __fastcall Move(int CurIndex, int NewIndex); 
移动元素 到指定位置。
 
void __fastcall Pack(void); 
删除Items列表中所有指针值为NULL的元素。 
此操作并不释放内存, 即只会改变Count。
 
int __fastcall Remove(void * Item); 
删除指定值的元素。
返回元素先前的位置。
 
Add方法和Remove方法的参数都是列表的元素对象。
示例:

TList *pList = new TList(); 

AnsiString TheObject = "This is an object." 

pList->Add(TheObject);

MessageDlg("列表中有 " + IntToStr(pList->Count) + "个元素", 

    mtInformation, TMsgDlgButtons() << mbOK, 0); 

pList->Remove(TheObject); 

delete pList;

pList=NULL;

 
typedef int __fastcall (*TListSortCompare)(void * Item1, void * Item2); 
void __fastcall Sort(TListSortCompare Compare); 
通过自定义的比较函数Compare来对列表中的元素进行排序。 
示例:

int __fastcall CompareNames(void *Item1, void *Item2) 

{ 

     return CompareText(((TComponent*)Item1)->Name, ((TComponent *)Item2)->Name); 

} 

void __fastcall TForm1::Button1Click(TObject *Sender) 

{ 

    List1->Sort(CompareText); 

}


2.1.2 AnsiString 类 
AnsiString是C++Builder中仿照Object Pascal语言中的长字符串类型设计的一个字符串类 siString类型。
本质上,AnsiString 仍然以’’作为字符串的结尾标志,只是在字符串的前面添加了几个字节的信息头。 
比较C++中提供的串类型,AnsiString 提供了更多实用、方便的方法。
 
赋值:=
连接:+、+=
比较:<、<=、==、>、>=、!=
字符索引:[] (注:串中字符索引从1开始)
 
char* __fastcall c_str() const;  
返回一个C/C++标准字符串指针。
 
AnsiString& __fastcall Delete(int index, int count);
删除index开始的count个字符。
提示:count可以超过剩余长度,则删除所有剩余。
 
static AnsiString __fastcall FormatFloat(const AnsiString& format,const long double& value); 
将value用format指定的格式转换为字符串形式。
 
示例:
Format string   1234      -1234      0.5        0   
#,##0.00        1,234.00  -1,234.00  0.50       0.00
0.000E+00       1.234E+03 -1.234E+03 5.000E-01  0.000E+00 
提示:
# 表示此位置可空缺。
0 表示此位置以0填充。
 
AnsiString& __fastcall Insert(const AnsiString& str, int index); 
插入。
 
static AnsiString __fastcall IntToHex(int value, int digits); 
Hex格式的串。
 
bool __fastcall IsEmpty() const;
判断空串。
 
int __fastcall Length() const; 
长度。
 
static AnsiString __fastcall LoadStr(HINSTANCE hInstance, int ident); 
static AnsiString __fastcall LoadStr(int ident); 
从指定句柄中读取标志为ident的字符资源。
如果没有指定句柄,则从当前模块(如可执行程序)中读取。
 
AnsiString __fastcall LowerCase() const; 
AnsiString __fastcall UpperCase() const; 
大小写转换。
 
int __fastcall Pos(const AnsiString& subStr) const; 
定位子串。
注意:无子串时返回0(因为 bc++串中字符索引从1开始).
 
AnsiString& __fastcall SetLength(int newLength); 
修改串的Length属性。
 
static AnsiString __fastcall StringOfChar(char ch, int count); 
字符重复成串。
 
AnsiString __fastcall SubString(int index, int count) const;  
截取子串。
 
double __fastcall ToDouble() const;
int __fastcall ToInt() const; 
int __fastcall ToIntDef(int defaultValue) const; 
ToInt 和 ToIntDef 方法都是将字符串转换为一个整型数,不同之处在于如果字符串没有可用的字符,前者会抛出一个异常,后者则会返回默认值defaultValue。
 
AnsiString __fastcall TrimLeft() const; 
AnsiString __fastcall TrimRight() const;
AnsiString __fastcall Trim() const;
截去首尾空格。

2.1.3 Set(集合)
Set<type, minval, maxval>是一个C++类模板,用来实现集合这个抽象数据类型。
 
  此模板使用时有三个参数,其中type用来指定集合元素的类型(如int、char、枚举变量等) ;
minval为集合元素的最小值,minval不能小于0;maxval为集合元素的最大值,它不能大于255。
对集合变量的声明并不会对它的值初始化,可以在声明变量之后使用<<操作符赋值,如下: 

Set <char, 'A', 'C'> s1; // 直接定义集体对象


s1 << 'A' << 'B' << 'C'; //初始化 


typedef Set <char, 'A','Z'> UPPERCASESet; // 使用typedef


UPPERCASESet s2; 

s2 << 'A' << 'B' << 'C';

 
Set常用方法:
 
Set& __fastcall Clear(); 
清空集合。
 
bool __fastcall Contains(const T el) const; 
查询集合对象中是否含有指定的元素el。
 
bool __fastcall Empty() const;
判断空集。
 
比较:==、!=
判断集合是否相等。
注意:只有类型和包含元素都相等的两个集体才相等。前面的例子中 s1!=s2, 虽然它们含有相同的元素。
 
operator +/*/- 
分别为集合之间的并集、交集、并集中交集的补集运算的运算符。 // 并、交、差集?
 
operator <</>> 
添加或删除元素。
 
集合示例:
如判断键盘事件和鼠标事件时,需要处理的Shift参数就是一个集合类型
typedef Set<Classes__1,ssShift,ssDouble>TShiftState。
 
2.1.4 DynamicArray(动态数组)
DynamicArray<type> ArrayName; 
这是一个模板类,它的元素可以为任意类型。
 
大小 Length。
索引 []。
比较和赋值:
赋值要使用 Copy/CopyRange 方法。
注意:不要在动态数据上使用 ==、=,那操作的对象是指针!
多维动态数组,如 

typedef DynamicArray< DynamicArray < AnsiString > > T2DStringArray;

T2DStringArray d2a;

d2a.Length = 3;

for(int i=0; i<d2a.Length; i++)

{

 d2a[i].Length = 4;

}


2.1.5 TStream(流)
流类用于进行各种存储媒介之间的数据操作。
TStream 类的几种派生类分别实现与某种特殊的存储媒介之间的数据交换,如磁盘数据、内存数据等。除了数据的读取、写入和复制之外, 在TStream类实例中还可以用Seek到流中的任意位置。 
TStream 是一个抽象类,其派生类有 TFileStream、TStringStream、TMemoryStream等。
 
属性:
Position 流文件当前指针的位置。以字节为单位。
Size     流文件大小。以字节为单位。
 
方法:
__int64 __fastcall CopyFrom(TStream* Source, __int64 Count);
__int64 __fastcall CopyFrom(TStream* Source, __int64 Count);  
从Source流文件中读取Count字节的内容到当前流文件中,函数返回值为实际拷贝的字节数。
 
示例:
将指定文件复制到工程所在目录。

void __fastcall TForm1::Button1Click(TObject *Sender)

{

 AnsiString NewFileName = ExtractFilePath(Application->ExeName)

    + ExtractFileName(Edit1->Text);

 AnsiString msg = Format("Copy %s to %s", ARRAYOFCONST((Edit1->Text, NewFileName)));

 if(MessageDlg(msg, mtInformation, mbOKCancel, 0) == mrOk)

 {

  TFileStream *OldFile = new TFileStream(Edit1->Text, fmOpenRead);

  try

  {

   TFileStream *NewFile = new TFileStream(NewFileName, fmCreate);

   try

   {

    NewFile->CopyFrom(OldFile, OldFile->Size);

   }

   __finally

   {

    FreeAndNil(NewFile);

   }

  }

  __finally

  {

   FreeAndNil(OldFile);

  }

 }

}

virtual int __fastcall Read(void *Buffer, int Count) = 0; 
  此方法用于从当前流文件的当前指针所在位置开始,读取Count字节的内容到Buffer内
存位置,返回值为实际读取的字节数。如果返回值小于 Count,则意味着还没有读够 Count
个字节的内容,指针位置已经到了流文件的末尾。其它的数据读取的方法,如ReadBuffer和
ReadComponent,都是通过对Read方法的调用来实现的。
 
virtual int __fastcall Seek ffset, (int O Word Origin); 
virtual __int64 __fastcall Seek(const __int64 Offset, TSeekOrigin Origin); 
读取流。
返回值为流文件中新的当前位置(Position 属性)。
Origin 参数的取值可以为 soFromBeginning(流文件的开始位置) 、soFromCurrent (流文件的当前位置)或soFromEnd(流文件的结尾位置) 。 
 
virtual int  fastcall Write(const void *Buffer, int Count) = 0; 
写入流。
返回值为写入的字节数。
所有其它的数据写入方法,如 WriteBuffer 和WriteComponent都是靠调用Write来实现的。
 
TMemoryStream 是 TStream 的派生类,它有两个比较重要的方法,SaveToStream 和
LoadFromStream,下面是它们的一个示例:

void __fastcall TForm1::Button1Click(TObject *Sender) 

{ 

 TMemoryStream* pms = new TMemoryStream(); 

 //将列表框的内容写入流文件


 ListBox1->Items->SaveToStream(pms); 

 //重置流文件的当前位置到文件的开头 


 pms->Position = 0;

 //将流文件的内容写入RichEdit组件 


 RichEdit1->Lines->LoadFromStream(pms);. 

 delete pms; 

}


2.2 函数 
2.2.1 系统函数
 
随机函数
 
extern PACKAGE Extended __fastcall RandG(Extended Mean, Extended StdDev); 
产生服从高斯分布的随机数
 
extern PACKAGE double __fastcall RandomFrom(const double *AValues, int AValues_Size); 
extern PACKAGE int __fastcall RandomFrom(const int *AValues, int AValues_Size); 
extern PACKAGE __int64 __fastcall RandomFrom(const __int64 *AValues, int AValues_Size);
RandomFrom 函数用于从一个数组中随即取得其元素.
 
extern PACKAGE int RandSeed; 
随机数种子.
extern PACKAGE void __fastcall Randomize(void);
初始化随机数产生器.
 
extern PACKAGE int __fastcall RandomRange(cost int AFrom, const int ATo);  
此函数返回一个从 AFrom 到 ATo(包含此值)的整数,如果 AFrom 大于 ATo,可以返回负数。
 
时间函数
 
void gettime(struct time *timep);
void settime(struct time *timep); 
获取/设置系统时间。
 
其中time结构
struct time { 
unsigned char ti_min; /* minutes */
unsigned char ti_hour; /* hours */ 
unsigned char ti_hund; /* hundredths of seconds */ 
unsigned char ti_sec; /* seconds */
};
 
clock_t clock(void);    typedef long clock_t;
示例:计算程序中某段代码的执行时间

#include <time.h>

#include <stdio.h>

#include <dos.h>

int main(void)

{ 

   clock_t start, end;

   start = clock(); 

   delay(2000);

   end = clock(); 

      printf("The time was: %fn", (end - start) / CLK_TCK); 

   return 0;

}

 
内联函数
对于频繁调用的简短函数,为了省去函数调用和返回的时间,在函数之前添加Inline声明它为内联。
inline char * X::func(void){return i;}   
此函数是定义类X中的成员函数func为Inline函数。
如果在类的定义中直接编写函数代码,则跟在函数前添加Inline标志的作用相同。
 
2.2.3 参数个数不定的函数 
可以使用va_arg、va_end、 和va_start三个宏来处理不定长的函数参数列表,它们定义在 stdarg.h 中。
void va_start(va_list ap, lastfix); 
type va_arg(va_list ap, type);
void va_end(va_list ap);
 示例:求所有入口参数的代数和

#include <stdio.h> 

#include <stdarg.h> 

/* calculate sum of a 0 terminated list */ 

void sum(char *msg, ...) 

{ 

   int total = 0; 

   a_list ap; 

   int arg;

   va_start(ap, msg);

   while((arg=va_arg(ap, int))!= 0)

        total += arg;

   

   // MessageDlg(AnsiString("sum is ")+total, mtInformation, TMsgDlgButtons()<<mbOK, 0);


}

void main()

{

 sum("The total of 1+2+3 is %dn", 1,2,3,0);

}


posted @ 2013-01-25 12:47  Phyking  阅读(454)  评论(0编辑  收藏  举报