锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
涓轟簡鍏嬫湇array蹇呴』浜嬪厛瀹e憡澶у皬鐨勪笉渚匡紝STL鍜?NET閮芥湁瑙e喅鐨勬柟寮忥紝鍦⊿TL鐨刢ontainer褰撲腑錛岄熷害鏈蹇殑灝辨槸std::vector錛岃?NET褰撲腑灝辨槸ArrayList錛屾墍浠ユ垜鎯蟲祴璇曞湪鍚屾牱鐨勭▼搴忎腑錛岃嫢浣跨敤unmanaged 鐨剆td::vector鏄惁浼氭瘮managed ArrayList蹇?br />
/**/
/*
聽
(C)聽OOMusou聽2006聽
http://oomusou.cnblogs.com
Filename聽聽聽聽:聽VectorVsArrayList.cpp
Compiler聽聽聽聽:聽Visual聽C++聽8.0
Description聽:聽Demo聽std::vector聽and聽ArrayList聽in聽C++/CLI
*/
#include聽
"
stdafx.h
"
#include聽
<
vector
>
#include聽
<
ctime
>
#include聽
<
iostream
>
using
聽
namespace
聽System;
using
聽
namespace
聽System::Collections;
int
聽main()
{
聽聽聽聽
//
聽Use聽STL聽std::vector
聽聽聽聽clock_t聽t1聽
=
聽clock();
聽聽聽聽
聽聽聽聽std::vector
<
int
>
聽ivec;
聽聽聽聽
for
(
int
聽i
=
0
;聽i聽
!=
聽
10000000
;聽
++
i)聽
{
聽聽聽聽聽聽ivec.push_back(i);
聽聽聽聽}
聽聽聽聽t1聽
=
聽clock()聽
-
聽t1;
聽聽聽聽std::cout聽
<<
聽
"
std::vector聽:聽
"
聽
<<
聽(
double
)t1
/
CLOCKS_PER_SEC聽
<<
聽
"
聽sec
"
聽
<<
聽std::endl;
聽聽聽聽
//
聽Use聽.NET聽ArrayList
聽聽聽聽clock_t聽t2聽
=
聽clock();
聽聽聽聽ArrayList
^
聽al聽
=
聽gcnew聽ArrayList;
聽聽聽聽
for
(
int
聽i
=
0
;聽i聽
!=
聽
10000000
;聽
++
i)聽
{
聽聽聽聽聽聽al
->
Add(i);
聽聽聽聽}
聽聽聽聽t2聽
=
聽clock()聽
-
聽t2;
聽聽聽聽std::cout聽
<<
聽
"
.NET聽ArrayList聽:聽
"
聽
<<
聽(
double
)t2
/
CLOCKS_PER_SEC聽
<<
聽
"
聽sec
"
聽
<<
聽std::endl;
聽聽聽聽
return
聽
0
;
}
鎵ц緇撴灉
std::vector聽:聽
5.618
聽sec
.NET聽ArrayList聽:聽
4.336
聽sec
璜嬫寜浠繪剰閸電辜綰屄?聽.聽.
鎵ц緇撴灉浠ゆ垜璁跺紓錛屾垜鍘熸湰棰勬湡unmanaged鐨剆td::vector搴旇浼氭瘮杈冨揩錛屼絾緇撴灉鍗存槸managed鐨凙rrayList杈冨揩錛屾垜瑙夊緱鍙兘鐨勫師鍥犳槸錛宮anaged鐨勬暟鎹斁鍦–LR鍐咃紝鑰寀nmanaged鏁版嵁鏀懼湪涓嶅悓鐨勫湴鏂癸紝褰搖nmanaged鐨剆td::vector瑕佸湪managed鐨勬墽琛岀幆澧冧腑鏄劇ず鏃訛紝榪樿緇忚繃marshaling鐨勫姩浣滐紝鎵浠ユ氮璐逛簡涓浜涙椂闂達紝綾諱技鍦?NET涓嬌鐢–OM鐨刣ll鏃訛紝涔熷繀欏葷粡榪噈arshaling錛屾墍浠ラ熷害杈冩參銆?br />
鍥炲埌鎴戠涓涓棶棰橈紝鐢–++寮鍙慉SP.NET鏈変粈涔堝ソ澶?鍦ㄨ繖涓畝鍗曠殑紼嬪簭閲屽彲浠ュ彂鐜幫紝C++/CLI鐨勬渶澶х壒鑹詫紝璁╀綘鍙互灝唌anaged鍜寀nmanaged紼嬪簭鍐欏湪涓璧鳳紝鍚屾椂浣跨敤.NET Framework鍜屼紶緇烠++鐨刲ibrary錛屽STL銆乥oost錛岃繖鏄疌#鎵鍋氫笉鍒扮殑錛屽亣濡備綘鐨勫伐紼嬮鍩熷凡緇忔湁寰堝library錛屾槸鐢–/C++鍐欑殑錛屽彲鑳芥槸10鍑犲勾鍓嶇殑涓滆タ錛岀洰鍓嶆牴鏈病鏈塁#鐗堟湰鐨刲ibrary錛岃屼綘鍙堟ラ渶榪欎簺library鐨刦unction寮鍙?NET紼嬪簭錛孋++/CLI灝卞緢鏂逛究浜嗭紝璁╀綘鍚屾椂鎷ユ姳C/C++ library鍜?NET Framework錛岃繖涔熸槸Microsoft寮鍙慍++/CLI鐨勫師鍥犮?br />
緇撹
鍦–++/CLI寮鍙憁anaged code錛屾墽琛岄熷害涓嶄細姣擟#銆乂B蹇紝铏界劧C++/CLI鍙互寮鍙憉nmanaged code錛屼絾鑻?NET Framework鏈夌浉瀵瑰簲鐨勬敮鎸佹椂錛屽簲鍏堣冭檻.NET Framework錛岀劧鍚庡啀鑰冭檻鍏跺畠C++鑷繁鐨刲ibrary錛屽綋鐒禖++/CLI涓嶈鐨勯傚悎鎵鏈夊紑鍙戜漢鍛橈紝浣嗗浜庝竴浜涘紑鍙戜漢鍛樻槸寰堟柟渚跨殑銆?br />
聽



<%
@聽Page聽AutoEventWireup="true"聽Inherits="HelloWorld"聽%>
<!DOCTYPE聽html聽PUBLIC聽"-//W3C//DTD聽XHTML聽1.0聽Transitional//EN"聽"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html聽xmlns="http://www.w3.org/1999/xhtml">
<head聽runat="server">
聽聽<title>Untitled聽Page</title>
</head>
<body>
聽聽<form聽id="form1"聽runat="server">
聽聽聽聽<div>
聽聽聽聽聽聽Using聽C++/CLI聽in聽ASP.NET<br聽/>
聽聽聽聽聽聽<br聽/>
聽聽聽聽聽聽<asp:Button聽ID="Button1"聽runat="server"聽OnClick="Button1_Click"聽Text="Button"聽/>
聽聽聽聽聽聽<asp:Label聽ID="Label1"聽runat="server"聽Text="Label"></asp:Label></div>
聽聽</form>
</body>
</html>


#using聽<system.dll>
#using聽<mscorlib.dll>
#using聽<system.web.dll>
using聽namespace聽System;
using聽namespace聽System::Web::UI::WebControls;

public聽ref聽class聽HelloWorld聽:聽public聽System::Web::UI::Page聽
{
protected:
聽聽Button^聽Button1;
聽聽Label^聽聽Label1;
public:
聽聽void聽Button1_Click(Object^聽sender,聽EventArgs^聽e)聽
{
聽聽聽聽this->Label1->Text聽=聽"Hello聽World";
聽聽聽聽return;
聽聽}
};
cl聽/clr聽HelloWorld.aspx.cpp聽/link聽/dll聽/out:HelloWorld.dll
鏈鍚庡彧瑕佸皢HelloWorld.aspx鏀懼埌c:\Inetpub\wwwroot\涓嬶紝HelloWorld.dll鏀懼埌c:\Inetpub\wwwroot\bin\涓嬶紝灝卞畬鎴恉eployment銆?br />
緇撹
寰堝浜鴻C++鏃犳硶寮鍙慉SP.NET錛孉NSI C++鐨勭‘涓嶈兘錛屼絾C++/CLI鍒欏彲浠ワ紝浜嬪疄涓婏紝浠諱綍.NET涓嬬殑璇█閮藉彲浠ュ紑鍙慉SP.NET錛岃櫧鐒禫isual Studio 2005宸ュ叿涓嶈鐨勬敮鎸侊紝浣嗗彧瑕侀忚繃涓浜涘皬鎶宸э紝浣犱緷鐒跺彲浠ョ敤濡沖枩嬈㈢殑.NET璇█寮鍙慉SP.NET銆?br />
Reference
ASP.NET with Managed C++聽, Soliant, The聽 code project.