stl 의 vector 나 list , vcl의 TList 등으로도 가능하구요
아래 처럼 realloc을 써서 Dynamic Array를 구현할수 있습니다.
class DIntArray
{
private:
int iRealCnt;
int iCnt;
int *IntArr;
public:
DIntArray()
{
iRealCnt=0;
iCnt=0;
IntArr=NULL;
}
DIntArray(int IniCount)
{
iCnt=0;
iRealCnt=IniCount;
IntArr=(int *)ReallocMemory(IntArr,(iRealCnt*4));
// 또는 IntArr=(int *)realloc(IntArr,(iRealCnt*4));
}
~DIntArray()
{
FreeMemory(IntArr);
// 또는 free(IntArr);
}
void __fastcall Add(int val)
{
if(iCnt>=iRealCnt)
{
iRealCnt+=100; //ReallocMemory 호출이 자주 일어나지 않도록..
IntArr=(int *)ReallocMemory(IntArr,(iRealCnt*4));
//또는 IntArr=(int *)realloc(IntArr,(iRealCnt*4));
}
IntArr[iCnt]=val;
iCnt++;
}
int __fastcall Get(int idx)
{
if(idx<0 || idx>=iCnt)
{
throw Exception("Out of Index");
}
return IntArr[idx];
}
void __fastcall Set(int idx,int Value)
{
if(idx<0 || idx>=iCnt)
{
throw Exception("Out of Index");
}
IntArr[idx]=Value;
}
__property int Count = {read=iCnt};
};
DIntArray DArr;
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int val=StrToInt(Edit1->Text);
DArr.Add(val);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Memo1->Lines->Clear();
for(int i=0;iLines->Add(IntToStr(DArr.Get(i)));
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
DArr.Set(10,10);
}
//---------------------------------------------------------------------------
그럼...
궁금한 사람 님이 쓰신 글 :
: 시험을 하면서 0.1초 마다 값을 저장한다고 합니다.
: 그런데 시험을 하는시간이 들쑥날쑥합니다.
:
: 10초, 1분, 3분 10분 등등....
:
: 시험후 값을 배열에 저장해야 하는데... 무턱대고 배열을 크게 잡을수도 없고.... 그렇다고 너무 작게
: 잡으면 배열을 크기를 오버하고...
:
: 포인터를 사용해야 하나요...
|