求助:用C++Builder写的一个关于STL基本操作,如何用Delphi来实现相应的功能。

creativezj 2004-12-16 10:50:43
刚刚接触Delphi,原来用BCB时,由于有STL,所以用链表(list)、向量(vector)等来实现某些功能很方便,现在转到Delphi下,不知用什么方法来实现类似的功能呢?看以下一个简单的示例:

------Unit.h:

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <list.h>

//---------------------------------------------------------------------------
struct PollTable
{
AnsiString IPAddr;
AnsiString OID;
int ID;
} ;

typedef list<PollTable*> PollTableList ;

class TForm1 : public TForm
{
__published: // IDE-managed Components
TEdit *Edit1;
TButton *Button1;
void __fastcall FormCreate(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
PollTableList mPollTableList;
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif



------Unit.cpp:

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------


void __fastcall TForm1::FormCreate(TObject *Sender)
{
PollTable *Poll = new PollTable;
Poll->IPAddr = "192.168.0.1";
Poll->OID = "1.3.6.1.4.2";
Poll->ID = 1;
mPollTableList.push_back(Poll);

PollTable *Poll2 = new PollTable;
Poll2->IPAddr = "192.168.0.1";
Poll2->OID = "1.3.6.1.4.2";
Poll2->ID = 2;
mPollTableList.push_back(Poll2);

PollTable *Poll3 = new PollTable;
Poll3->IPAddr = "192.168.0.1";
Poll3->OID = "1.3.6.1.4.2";
Poll3->ID = 3;
mPollTableList.push_back(Poll3);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
list<PollTable*>:: iterator first = mPollTableList.begin();
list<PollTable*>:: iterator last = mPollTableList.end();
while(first != last)
{
if ( (*first)->ID == 3 )
{
mPollTableList.erase(first);
delete *first;
break;
}
first++;
}
Edit1->Text = IntToStr(mPollTableList.size());
}


...全文
62 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
creativezj 2004-12-16
  • 打赏
  • 举报
回复
谢谢!
那上面的bcb的代码利用TList该如何写呢?
beyondtkl 2004-12-16
  • 打赏
  • 举报
回复
C++ 之所以強大 就是有很多的類庫支持
Delphi 之所以方便 就是有很多的第三方控件支持

Delphi中沒有stl之類的了 有類似的 應該可以代替

比如 list <-> TList...
creativezj 2004-12-16
  • 打赏
  • 举报
回复

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧