锘??xml version="1.0" encoding="utf-8" standalone="yes"?> C璇█涓垽鏂暟鎹被鍨嬮暱搴︾ 涔熷彲浠ュ涓涓嚱鏁拌皟鐢ㄦ眰鍊鹼紝鍏剁粨鏋滄槸鍑芥暟榪斿洖綾誨瀷鐨勫ぇ灝忥紝鍑芥暟騫朵笉浼氳璋冪敤錛屾垜浠潵鐪嬩竴涓畬鏁寸殑渚嬪瓙錛?br>char foo() cin.getline()鏂規(guī)硶榪炵畫鍦頒粠鐢ㄦ埛緇堢鎺ュ彈瀛楃錛屽茍灝嗗瓧絎﹀瓨鍏ュ瓧絎﹀瀷鏁扮粍message涓紝鐩村埌杈撳叆浜嗭紙maxchars錛?/font>1錛変釜瀛楃錛堢maxchars涓瓧絎︾敤鏉ュ瓨鍌ㄥ瓧絎︿覆緇撳熬鐨?/font>NULL瀛楃'\0')鎴栬呮帴鍙楀埌浜嗗洖杞︿負姝紝榪欑粓绔敭鍏ュ洖杞﹂敭浜х敓涓涓崲琛?/font>'\n'錛屽畠琚?/font>cin.getline()璁や負鏄杈撳叆緇撳熬銆?/font>cin.getline()鑾峰緱鐨勫瓧絎?/font>(闄や簡鎹㈣絎﹀)琚瓨鍌ㄥ埌message鏁扮粍涓傚湪榪斿洖涔嬪墠錛?/font>cin.getline錛堬級鍑芥暟鍦ㄥ瓨鍌ㄧ殑榪欎簺瀛楃鍚庨潰娣誨姞涓涓?/font>NULL瀛楃'\0'銆?/span>
Cin.ignore錛堬級鏂規(guī)硶cin.ignore( 5, 'c' ) 鐨勬槸浠庤緭鍏ユ祦錛?span lang=EN-US>cin錛変腑鎻愬彇瀛楃錛屾彁鍙栫殑瀛楃琚拷鐣ワ紙ignore錛夛紝涓嶈浣跨敤銆傛瘡鎶涘純涓涓瓧絎︼紝瀹冮兘瑕佽鏁板拰姣旇緝瀛楃錛氬鏋滆鏁板艱揪鍒?span lang=EN-US>5 Cin.clear鐢ㄦ硶濡傛灉杈撳叆鍙戠敓閿欒鍙戠敓錛岄偅涔堟祦鐘舵佹棦琚爣璁頒負閿欒錛屼綘蹇呴』娓呴櫎榪欎簺閿欒鐘舵侊紝浠ヤ嬌浣犵殑紼嬪簭鑳芥紜傚綋鍦扮戶緇繍琛屻傝娓呴櫎閿欒鐘舵侊紝闇浣跨敤clear()鍑芥暟銆傛鍑芥暟甯︿竴涓弬鏁幫紝瀹冩槸浣犲皢瑕佽涓哄綋鍓嶇姸鎬佺殑鏍囧織鍊箋傦紝鍙灝?/font>ios::goodbit浣滀負瀹炲弬
while (!s.IsEmpty()) while ((NULL != root) || !s.empty()) 緇欏嚭涓涓敤綾誨皝瑁呯殑瀛楀吀鏍?wèi)浠g爜锛屽巹銆傘傘傚仛ACM鐨勬ā鏉跨敤鍙︿竴涓傘傚簲璇ユ斁鍦ㄤ簡“ACM妯℃澘”鏂囦歡澶逛笅浜嗐傘傘?/p>
#include
#include
#include
using namespace std;
錛?錛夊垱寤轟竴涓暟緇勶紝
v_Arry.pus
v_Arry.pus
v_Arry.pus
v_Arry.pus
v_Arry.pus
v_Arry.pus
v_Arry.pus
v_Arry.pus
v_Arry.pus
v_Arry.pus
v_Arry.pus
錛?錛塵ake_he
錛?錛夋鏃跺彲浠ヨ緭鍑烘煡
鍙樻垚浜屽弶鏍?wèi)灏辨槸濡備笅锛?br>
v_Arry.pop
浜屽弶鏍?wèi)濡備笅锛?
錛?錛塿_Arry.
姝ゆ椂杈撳嚭緇撴灉錛?5錛?wbr>10錛?錛?錛?錛?
浜屽弶鏍?wèi)濡備笅锛?br>
錛?錛塻ort_he
錛?錛塮ind ( v_Arry.beg
鍘熸枃鍦板潃錛?a >http://blog.zol.com.cn/1356/article_1355249.html
]]>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <cmath>
using namespace std;
void CountSort(int a[], int b[],int num,int d) //璁℃暟鎺掑簭

{
int* c = new int[10];
int index;
for (int i=0;i<=9;i++)
c[i]=0;
int size = num;
for (int j=0;j<size;j++)
{
index=a[j]%(int)pow(10.0,d)/(int)pow(10.0,d-1);
c[index]++;
}
//c[i]鍖呭惈絳変簬i鐨勫厓绱犱釜鏁?/span>
for (i=1;i<=9;i++)
c[i]=c[i]+c[i-1];
//c[i]鍖呭惈灝忎簬絳変簬i鐨勫厓绱犱釜鏁?/span>
for (j=size-1;j>=0;j--)
{
index=a[j]%(int)pow(10.0,d)/(int)pow(10.0,d-1);
b[c[index]-1]=a[j];
c[index]--;
}
for (j=0;j<size;j++) //鏇存柊涓嬈℃帓搴忓悗鐨刟鏁扮粍
{
a[j]=b[j];
}
delete [] c;
}
void RadixSort(int a[],int b[],int d,int num) //鍩烘暟鎺掑簭

{
for(int i=1;i<=d;i++)
CountSort(a,b,num,i);
}

void main()

{
int num,d;
cout<<"杈撳叆涓暟鍙婁綅鏁?/span>"<<endl;
cin>>num>>d;
int* a = new int[num];
int* b = new int[num];
cout<<"鎺掑簭鍓嶏細"<<endl;
for(int i=0;i<num;i++)
{
cin>>a[i];
}
RadixSort(a,b,d,num);
cout<<"鎺掑簭鍚庯細"<<endl;
for (int j=0;j<num;j++)
{
cout<<b[j]<<endl;
}
delete [] a;
delete [] b;
}
]]>
鐢ㄦ硶
鎴?br>
1. 瀹氫箟錛?br>sizeof鏄疌/C++涓殑涓涓搷浣滅錛坥perator錛夋槸涔燂紝綆鍗曠殑璇村叾浣滅敤灝辨槸榪斿洖涓涓璞℃垨鑰呯被鍨嬫墍鍗犵殑鍐呭瓨瀛楄妭鏁般?br>MSDN涓婄殑瑙i噴涓猴細
The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of type size_t.
鍏惰繑鍥炲肩被鍨嬩負size_t錛屽湪澶存枃浠秙tddef.h涓畾涔夈傝繖鏄竴涓緷璧栦簬緙栬瘧緋葷粺鐨勫鹼紝涓鑸畾涔変負
typedef unsigned int size_t;
涓栦笂緙栬瘧鍣ㄦ灄鏋楁繪伙紝浣嗕綔涓轟竴涓鑼冿紝瀹冧滑閮戒細淇濊瘉char銆乻igned char鍜寀nsigned
char鐨剆izeof鍊間負1錛屾瘯绔焎har鏄垜浠紪紼嬭兘鐢ㄧ殑鏈灝忔暟鎹被鍨嬨?br>2. 璇硶錛?br>sizeof鏈変笁縐嶈娉曞艦寮忥紝濡備笅錛?br>1) sizeof( object ); // sizeof( 瀵硅薄 );
2) sizeof( type_name ); // sizeof( 綾誨瀷 );
3) sizeof object; // sizeof 瀵硅薄;
鎵浠ワ紝
int i;
sizeof( i );
sizeof i;
sizeof( int ); // ok
sizeof int;
鏃㈢劧鍐欐硶3鍙互鐢ㄥ啓娉?浠f浛錛屼負姹傚艦寮忕粺涓浠ュ強鍑忓皯鎴戜滑澶ц剳鐨勮礋鎷咃紝絎?縐嶅啓娉曪紝蹇樻帀瀹冨惂錛佸疄闄呬笂錛宻izeof璁$畻瀵硅薄鐨勫ぇ灝忎篃鏄漿鎹㈡垚瀵瑰璞$被鍨嬬殑璁$畻錛屼篃灝辨槸璇達紝鍚岀綾誨瀷鐨勪笉鍚屽璞″叾sizeof鍊奸兘鏄竴鑷寸殑銆傝繖閲岋紝瀵硅薄鍙互榪涗竴姝ュ歡浼歌嚦琛ㄨ揪寮忥紝鍗硈izeof鍙互瀵逛竴涓〃杈懼紡姹傚鹼紝緙栬瘧鍣ㄦ牴鎹〃杈懼紡鐨勬渶緇堢粨鏋滅被鍨嬫潵紜畾澶у皬錛屼竴鑸笉浼氬琛ㄨ揪寮忚繘琛岃綆椼傚錛?br>sizeof( 2 );
sizeof( 2 + 3.14 );
{
printf("foo() has been called.\n");
return 'a';
}
int main()
{
size_t sz = sizeof( foo() ); // foo() 鐨勮繑鍥炲肩被鍨嬩負char錛屾墍浠z = sizeof(char )錛宖oo()騫朵笉浼氳璋冪敤
printf("sizeof( foo() ) = %d\n", sz);
}
C99鏍囧噯瑙勫畾錛屽嚱鏁般佷笉鑳界‘瀹氱被鍨嬬殑琛ㄨ揪寮忎互鍙婁綅鍩燂紙bit-field錛夋垚鍛樹笉鑳借璁$畻sizeof鍊鹼紝鍗充笅闈㈣繖浜涘啓娉曢兘鏄敊璇殑錛?br>sizeof( foo );// error
void foo2() { }
sizeof( foo2() );// error
struct S
{
};
sizeof( S.f1 );// error
3. sizeof鐨勫父閲忔?br>sizeof鐨勮綆楀彂鐢熷湪緙栬瘧鏃跺埢錛屾墍浠ュ畠鍙互琚綋浣滃父閲忚〃杈懼紡浣跨敤錛屽錛?br>char ary[ sizeof( int ) * 10 ]; // ok
鏈鏂扮殑C99鏍囧噯瑙勫畾sizeof涔熷彲浠ュ湪榪愯鏃跺埢榪涜璁$畻錛屽涓嬮潰鐨勭▼搴忓湪Dev-C++涓彲浠ユ紜墽琛岋細
int n;
n = 10; // n鍔ㄦ佽祴鍊?br>char ary[n]; // C99涔熸敮鎸佹暟緇勭殑鍔ㄦ佸畾涔?br>printf("%d\n", sizeof(ary)); // ok. 杈撳嚭10
浣嗗湪娌℃湁瀹屽叏瀹炵幇C99鏍囧噯鐨勭紪璇戝櫒涓氨琛屼笉閫氫簡錛屼笂闈㈢殑浠g爜鍦╒C6涓氨閫氫笉榪囩紪璇戙傛墍浠ユ垜浠渶濂借繕鏄涓簊izeof鏄湪緙栬瘧鏈熸墽琛岀殑錛岃繖鏍蜂笉浼氬甫鏉ラ敊璇紝璁╃▼搴忕殑鍙Щ妞嶆у己浜涖?br>4. 鍩烘湰鏁版嵁綾誨瀷鐨剆izeof
榪欓噷鐨勫熀鏈暟鎹被鍨嬫寚short銆乮nt銆乴ong銆乫loat銆乨ouble榪欐牱鐨勭畝鍗曞唴緗暟鎹被鍨嬶紝鐢變簬瀹冧滑閮芥槸鍜岀郴緇熺浉鍏崇殑錛屾墍浠ュ湪涓嶅悓鐨勭郴緇熶笅鍙栧煎彲鑳戒笉鍚岋紝榪欏姟蹇呭紩璧鋒垜浠殑娉ㄦ剰錛屽敖閲忎笉瑕佸湪榪欐柟闈㈢粰鑷繁紼嬪簭鐨勭Щ妞嶉犳垚楹葷儲銆?br>涓鑸殑錛屽湪32浣嶇紪璇戠幆澧冧腑錛宻izeof(int)鐨勫彇鍊間負4銆?br>5. 鎸囬拡鍙橀噺鐨剆izeof
瀛﹁繃鏁版嵁緇撴瀯鐨勪綘搴旇鐭ラ亾鎸囬拡鏄竴涓緢閲嶈鐨勬蹇碉紝瀹冭褰曚簡鍙︿竴涓璞$殑鍦板潃銆傛棦鐒舵槸鏉ュ瓨鏀懼湴鍧鐨勶紝閭d箞瀹冨綋鐒剁瓑浜庤綆楁満鍐呴儴鍦板潃鎬葷嚎鐨勫搴︺傛墍浠ュ湪32浣嶈綆楁満涓紝涓涓寚閽堝彉閲忕殑榪斿洖鍊煎繀瀹氭槸4錛堟敞鎰忕粨鏋滄槸浠ュ瓧鑺備負鍗曚綅錛夛紝鍙互棰勮錛屽湪灝嗘潵鐨?4浣嶇郴緇熶腑鎸囬拡鍙橀噺鐨剆izeof緇撴灉涓?銆?br>char* pc = "abc";
int* pi;
string* ps;
char** ppc = &pc;
void (*pf)();// 鍑芥暟鎸囬拡
sizeof( pc ); // 緇撴灉涓?
sizeof( pi ); // 緇撴灉涓?
sizeof( ps ); // 緇撴灉涓?
sizeof( ppc ); // 緇撴灉涓?
sizeof( pf );// 緇撴灉涓?
鎸囬拡鍙橀噺鐨剆izeof鍊間笌鎸囬拡鎵鎸囩殑瀵硅薄娌℃湁浠諱綍鍏崇郴錛屾鏄敱浜庢墍鏈夌殑鎸囬拡鍙橀噺鎵鍗犲唴瀛樺ぇ灝忕浉絳夛紝鎵浠FC娑堟伅澶勭悊鍑芥暟浣跨敤涓や釜鍙傛暟WPARAM銆丩PARAM灝辮兘浼犻掑悇縐嶅鏉傜殑娑堟伅緇撴瀯錛堜嬌鐢ㄦ寚鍚戠粨鏋勪綋鐨勬寚閽堬級銆?br>6. 鏁扮粍鐨剆izeof
鏁扮粍鐨剆izeof鍊肩瓑浜庢暟緇勬墍鍗犵敤鐨勫唴瀛樺瓧鑺傛暟錛屽錛?br>char a1[] = "abc";
int a2[3];
sizeof( a1 ); // 緇撴灉涓?錛屽瓧絎?鏈熬榪樺瓨鍦ㄤ竴涓狽ULL緇堟絎?br>sizeof( a2 ); // 緇撴灉涓?*4=12錛堜緷璧栦簬int錛?br>涓浜涙湅鍙嬪垰寮濮嬫椂鎶妔izeof褰撲綔浜嗘眰鏁扮粍鍏冪礌鐨勪釜鏁幫紝鐜板湪錛屼綘搴旇鐭ラ亾榪欐槸涓嶅鐨勶紝閭d箞搴旇鎬庝箞姹傛暟緇勫厓绱犵殑涓暟鍛asy錛岄氬父鏈変笅闈袱縐嶅啓娉曪細
int c1 = sizeof( a1 ) / sizeof( char ); // 鎬婚暱搴?鍗曚釜鍏冪礌鐨勯暱搴?br>int c2 = sizeof( a1 ) / sizeof( a1[0] ); // 鎬婚暱搴?絎竴涓厓绱犵殑闀垮害
鍐欏埌榪欓噷錛屾彁涓闂紝涓嬮潰鐨刢3錛宑4鍊煎簲璇ユ槸澶氬皯鍛?br>void foo3(char a3[3])
{
int c3 = sizeof( a3 ); // c3 ==
}
void foo4(char a4[])
{
int c4 = sizeof( a4 ); // c4 ==
}
涔熻褰撲綘璇曞浘鍥炵瓟c4鐨勫兼椂宸茬粡鎰忚瘑鍒癱3絳旈敊浜嗭紝鏄殑錛宑3!=3銆傝繖閲屽嚱鏁板弬鏁癮3宸蹭笉鍐嶆槸鏁扮粍綾誨瀷錛岃屾槸铚曞彉鎴愭寚閽堬紝鐩稿綋浜巆har* a3錛屼負浠涔堜粩緇嗘兂鎯沖氨涓嶉毦鏄庣櫧錛屾垜浠皟鐢ㄥ嚱鏁癴oo1鏃訛紝紼嬪簭浼氬湪鏍堜笂鍒嗛厤涓涓ぇ灝忎負3鐨勬暟緇勫悧涓嶄細錛佹暟緇勬槸“浼犲潃”鐨勶紝璋冪敤鑰呭彧闇灝嗗疄鍙傜殑鍦板潃浼犻掕繃鍘伙紝鎵浠3鑷劧涓烘寚閽堢被鍨嬶紙char*錛夛紝c3鐨勫間篃灝變負4銆?br>7. 緇撴瀯浣撶殑sizeof
榪欐槸鍒濆鑰呴棶寰楁渶澶氱殑涓涓棶棰橈紝鎵浠ヨ繖閲屾湁蹇呰澶氳垂鐐圭瑪澧ㄣ傝鎴戜滑鍏堢湅涓涓粨鏋勪綋錛?br>struct S1
{
char c;
int i;
};
闂畇izeof(s1)絳変簬澶氬皯鑱槑鐨勪綘寮濮嬫濊冧簡錛宑har鍗?涓瓧鑺傦紝int鍗?涓瓧鑺傦紝閭d箞鍔犺搗鏉ュ氨搴旇鏄?銆傛槸榪欐牱鍚椾綘鍦ㄤ綘鏈哄櫒涓婅瘯榪囦簡鍚椾篃璁鎬綘鏄鐨勶紝浣嗗緢鍙兘浣犳槸閿欑殑錛乂C6涓寜榛樿璁劇疆寰楀埌鐨勭粨鏋滀負8銆?br>Why涓轟粈涔堝彈浼ょ殑鎬繪槸鎴?br>璇蜂笉瑕佹伯涓э紝鎴戜滑鏉ュソ濂界悽紓ㄤ竴涓媠izeof鐨勫畾涔夆斺攕izeof鐨勭粨鏋滅瓑浜庡璞℃垨鑰呯被鍨嬫墍鍗犵殑鍐呭瓨瀛楄妭鏁幫紝濂藉惂錛岄偅灝辮鎴戜滑鏉ョ湅鐪婼1鐨勫唴瀛樺垎閰嶆儏鍐碉細
S1 s1 = { 'a', 0xFFFFFFFF };
瀹氫箟涓婇潰鐨勫彉閲忓悗錛屽姞涓婃柇鐐癸紝榪愯紼嬪簭錛岃瀵焥1鎵鍦ㄧ殑鍐呭瓨錛屼綘鍙戠幇浜嗕粈涔?br>浠ユ垜鐨刅C6.0涓轟緥錛宻1鐨勫湴鍧涓?x0012FF78錛屽叾鏁版嵁鍐呭濡備笅錛?br>0012FF78: 61 CC CC CC FF FF FF FF
鍙戠幇浜嗕粈涔堟庝箞涓棿澶規(guī)潅浜?涓瓧鑺傜殑CC鐪嬬湅MSDN涓婄殑璇存槑錛?br>When applied to a structure type or variable, sizeof returns the actual size, which may include padding bytes inserted for alignment.
鍘熸潵濡傛錛岃繖灝辨槸浼犺涓殑瀛楄妭瀵歸綈鍟婏紒涓涓噸瑕佺殑璇濋鍑虹幇浜嗐?br>涓轟粈涔堥渶瑕佸瓧鑺傚榻愯綆楁満緇勬垚鍘熺悊鏁欏鎴戜滑榪欐牱鏈夊姪浜庡姞蹇綆楁満鐨勫彇鏁伴熷害錛屽惁鍒欏氨寰楀鑺辨寚浠ゅ懆鏈熶簡銆備負姝わ紝緙栬瘧鍣ㄩ粯璁や細瀵圭粨鏋勪綋榪涜澶勭悊錛堝疄闄呬笂鍏跺畠鍦版柟鐨勬暟鎹彉閲忎篃鏄姝わ級錛岃瀹藉害涓?鐨勫熀鏈暟鎹被鍨嬶紙short絳夛級閮戒綅浜庤兘琚?鏁撮櫎鐨勫湴鍧涓婏紝璁╁搴︿負4鐨勫熀鏈暟鎹被鍨嬶紙int絳夛級閮戒綅浜庤兘琚?鏁撮櫎鐨勫湴鍧涓婏紝浠ユ綾繪帹銆傝繖鏍鳳紝涓や釜鏁頒腑闂村氨鍙兘闇瑕佸姞鍏ュ~鍏呭瓧鑺傦紝鎵浠ユ暣涓粨鏋勪綋鐨剆izeof鍊煎氨澧為暱浜嗐?br>璁╂垜浠氦鎹竴涓婼1涓璫har涓巌nt鐨勪綅緗細
struct S2
{
int i;
char c;
};
鐪嬬湅sizeof(S2)鐨勭粨鏋滀負澶氬皯錛屾庝箞榪樻槸8鍐嶇湅鐪嬪唴瀛橈紝鍘熸潵鎴愬憳c鍚庨潰浠嶇劧鏈?涓~鍏呭瓧鑺傦紝榪欏張鏄負浠涔堝晩鍒潃鎬ワ紝涓嬮潰鎬葷粨瑙勫緥銆?br>瀛楄妭瀵歸綈鐨勭粏鑺傚拰緙栬瘧鍣ㄥ疄鐜扮浉鍏籌紝浣嗕竴鑸岃█錛屾弧瓚充笁涓噯鍒欙細
1) 緇撴瀯浣撳彉閲忕殑棣栧湴鍧鑳藉琚叾鏈瀹藉熀鏈被鍨嬫垚鍛樼殑澶у皬鎵鏁撮櫎錛?br>2) 緇撴瀯浣撴瘡涓垚鍛樼浉瀵逛簬緇撴瀯浣撻鍦板潃鐨勫亸縐婚噺錛坥ffset錛夐兘鏄垚鍛樺ぇ灝忕殑鏁存暟鍊嶏紝濡傛湁闇瑕佺紪璇戝櫒浼氬湪鎴愬憳涔嬮棿鍔犱笂濉厖瀛楄妭錛坕nternal adding錛夛紱
3) 緇撴瀯浣撶殑鎬誨ぇ灝忎負緇撴瀯浣撴渶瀹藉熀鏈被鍨嬫垚鍛樺ぇ灝忕殑鏁存暟鍊嶏紝濡傛湁闇瑕佺紪璇戝櫒浼氬湪鏈鏈竴涓垚鍛樹箣鍚庡姞涓婂~鍏呭瓧鑺傦紙trailing padding錛夈?br>瀵逛簬涓婇潰鐨勫噯鍒欙紝鏈夊嚑鐐歸渶瑕佽鏄庯細
1) 鍓嶉潰涓嶆槸璇寸粨鏋勪綋鎴愬憳鐨勫湴鍧鏄叾澶у皬鐨勬暣鏁板嶏紝鎬庝箞鍙堣鍒板亸縐婚噺浜嗗憿鍥犱負鏈変簡絎?鐐瑰瓨鍦紝鎵浠ユ垜浠氨鍙互鍙冭檻鎴愬憳鐨勫亸縐婚噺錛岃繖鏍鋒濊冭搗鏉ョ畝鍗曘傛兂鎯充負浠涔堛?br>緇撴瀯浣撴煇涓垚鍛樼浉瀵逛簬緇撴瀯浣撻鍦板潃鐨勫亸縐婚噺鍙互閫氳繃瀹弌ffsetof()鏉ヨ幏寰楋紝榪欎釜瀹忎篃鍦╯tddef.h涓畾涔夛紝濡備笅錛?br>
渚嬪錛屾兂瑕佽幏寰桽2涓璫鐨勫亸縐婚噺錛屾柟娉曚負
size_t pos = offsetof(S2, c);// pos絳変簬4
2) 鍩烘湰綾誨瀷鏄寚鍓嶉潰鎻愬埌鐨勫儚char銆乻hort銆乮nt銆乫loat銆乨ouble榪欐牱鐨勫唴緗暟鎹被鍨嬶紝榪欓噷鎵璇寸殑“鏁版嵁瀹藉害”灝辨槸鎸囧叾sizeof鐨勫ぇ灝忋傜敱浜庣粨鏋勪綋鐨勬垚鍛樺彲浠ユ槸澶嶅悎綾誨瀷錛屾瘮濡傚彟澶栦竴涓粨鏋勪綋錛屾墍浠ュ湪瀵繪壘鏈瀹藉熀鏈被鍨嬫垚鍛樻椂錛屽簲褰撳寘鎷鍚堢被鍨嬫垚鍛樼殑瀛愭垚鍛橈紝鑰屼笉鏄妸澶嶅悎鎴愬憳鐪嬫垚鏄竴涓暣浣撱備絾鍦ㄧ‘瀹氬鍚堢被鍨嬫垚鍛樼殑鍋忕Щ浣嶇疆鏃跺垯鏄皢澶嶅悎綾誨瀷浣滀負鏁翠綋鐪嬪緟銆?br>榪欓噷鍙欒堪璧鋒潵鏈夌偣鎷楀彛錛屾濊冭搗鏉ヤ篃鏈夌偣鎸犲ご錛岃繕鏄鎴戜滑鐪嬬湅渚嬪瓙鍚э紙鍏蜂綋鏁板間粛浠C6涓轟緥錛屼互鍚庝笉鍐嶈鏄庯級錛?br>struct S3
{
char c1;
S1 s;
char c2;
};
S1鐨勬渶瀹界畝鍗曟垚鍛樼殑綾誨瀷涓篿nt錛孲3鍦ㄨ冭檻鏈瀹界畝鍗曠被鍨嬫垚鍛樻椂鏄皢S1“鎵撴暎”鐪嬬殑錛屾墍浠3鐨勬渶瀹界畝鍗曠被鍨嬩負int錛岃繖鏍鳳紝閫氳繃S3瀹氫箟鐨勫彉閲忥紝鍏跺瓨鍌ㄧ┖闂撮鍦板潃闇瑕佽4鏁撮櫎錛屾暣涓猻izeof(S3)鐨勫間篃搴旇琚?鏁撮櫎銆?br>c1鐨勫亸縐婚噺涓?錛宻鐨勫亸縐婚噺鍛㈣繖鏃秙鏄竴涓暣浣擄紝瀹冧綔涓虹粨鏋勪綋鍙橀噺涔熸弧瓚沖墠闈笁涓噯鍒欙紝鎵浠ュ叾澶у皬涓?錛屽亸縐婚噺涓?錛宑1涓巗涔嬮棿渚塊渶瑕?涓~鍏呭瓧鑺傦紝鑰宑2涓巗涔嬮棿灝變笉闇瑕佷簡錛屾墍浠2鐨勫亸縐婚噺涓?2錛岀畻涓奵2鐨勫ぇ灝忎負13錛?3鏄笉鑳借4鏁撮櫎鐨勶紝榪欐牱鏈熬榪樺緱琛ヤ笂3涓~鍏呭瓧鑺傘傛渶鍚庡緱鍒皊izeof(S3)鐨勫間負16銆?br>閫氳繃涓婇潰鐨勫彊榪幫紝鎴戜滑鍙互寰楀埌涓涓叕寮忥細
緇撴瀯浣撶殑澶у皬絳変簬鏈鍚庝竴涓垚鍛樼殑鍋忕Щ閲忓姞涓婂叾澶у皬鍐嶅姞涓婃湯灝劇殑濉厖瀛楄妭鏁扮洰錛屽嵆錛?br>sizeof( struct ) = offsetof( last item ) + sizeof( last item ) + sizeof( trailing padding )
鍒拌繖閲岋紝鏈嬪弸浠簲璇ュ緇撴瀯浣撶殑sizeof鏈変簡涓涓叏鏂扮殑璁よ瘑錛屼絾涓嶈楂樺叴寰楀お鏃╋紝鏈変竴涓獎鍝峴izeof鐨勯噸瑕佸弬閲忚繕鏈鎻愬強錛岄偅渚挎槸緙栬瘧鍣ㄧ殑pack鎸囦護銆傚畠鏄敤鏉ヨ皟鏁寸粨鏋勪綋瀵歸綈鏂瑰紡鐨勶紝涓嶅悓緙栬瘧鍣ㄥ悕縐板拰鐢ㄦ硶鐣ユ湁涓嶅悓錛孷C6涓氳繃#pragma pack瀹炵幇錛屼篃鍙互鐩存帴淇敼/Zp緙栬瘧寮鍏熾?pragma pack鐨勫熀鏈敤娉曚負錛?pragma pack( n )錛宯涓哄瓧鑺傚榻愭暟錛屽叾鍙栧間負1銆?銆?銆?銆?6錛岄粯璁ゆ槸8錛屽鏋滆繖涓兼瘮緇撴瀯浣撴垚鍛樼殑sizeof鍊煎皬錛岄偅涔?br>璇ユ垚鍛樼殑鍋忕Щ閲忓簲璇ヤ互姝ゅ間負鍑嗭紝鍗蟲槸璇達紝緇撴瀯浣撴垚鍛樼殑鍋忕Щ閲忓簲璇ュ彇浜岃呯殑鏈灝忓鹼紝
鍏紡濡備笅錛?br>offsetof( item ) = min( n, sizeof( item ) )
鍐嶇湅紺轟緥錛?br>#pragma pack(push)
#pragma pack(2) // 蹇呴』鍦ㄧ粨鏋勪綋瀹氫箟涔嬪墠浣跨敤
struct S1
{
char c;
int i;
};
struct S3
{
char c1;
S1 s;
char c2;
};
#pragma pack(pop) // 鎭㈠鍏堝墠鐨刾ack璁劇疆
璁$畻sizeof(S1)鏃訛紝min(2, sizeof(i))鐨勫間負2錛屾墍浠鐨勫亸縐婚噺涓?錛屽姞涓妔izeof(i)絳変簬6錛岃兘澶熻2鏁撮櫎錛屾墍浠ユ暣涓猄1鐨勫ぇ灝忎負6銆?br>鍚屾牱錛屽浜巗izeof(S3)錛宻鐨勫亸縐婚噺涓?錛宑2鐨勫亸縐婚噺涓?錛屽姞涓妔izeof(c2)絳変簬9錛屼笉鑳借2鏁撮櫎錛屾坊鍔犱竴涓~鍏呭瓧鑺傦紝鎵浠izeof(S3)絳変簬10銆?br>
鐜板湪錛屾湅鍙嬩滑鍙互杞繪澗鐨勫嚭涓鍙f皵浜嗭紝:)
榪樻湁涓鐐硅娉ㄦ剰錛?#8220;絀虹粨鏋勪綋”錛堜笉鍚暟鎹垚鍛橈級鐨勫ぇ灝忎笉涓?錛岃屾槸1銆傝瘯鎯充竴涓?#8220;涓嶅崰絀洪棿”鐨勫彉閲忓浣曡鍙栧湴鍧銆佷袱涓笉鍚岀殑“絀虹粨鏋勪綋”鍙橀噺鍙堝浣曞緱浠ュ尯鍒嗗憿浜庢槸錛?#8220;絀虹粨鏋勪綋”鍙橀噺涔熷緱琚瓨鍌紝榪欐牱緙栬瘧鍣ㄤ篃灝卞彧鑳戒負鍏跺垎閰嶄竴涓瓧鑺傜殑絀洪棿鐢ㄤ簬鍗犱綅浜嗐傚涓嬶細
struct S5 { };
sizeof( S5 ); // 緇撴灉涓?
8. 鍚綅鍩熺粨鏋勪綋鐨剆izeof
鍓嶉潰宸茬粡璇磋繃錛屼綅鍩熸垚鍛樹笉鑳藉崟鐙鍙杝izeof鍊鹼紝鎴戜滑榪欓噷瑕佽璁虹殑鏄惈鏈変綅鍩熺殑緇撴瀯浣撶殑sizeof錛屽彧鏄冭檻鍒板叾鐗規(guī)畩鎬ц屽皢鍏朵笓闂ㄥ垪浜嗗嚭鏉ャ?br>C99瑙勫畾int銆乽nsigned int鍜宐ool鍙互浣滀負浣嶅煙綾誨瀷錛屼絾緙栬瘧鍣ㄥ嚑涔庨兘瀵規(guī)浣滀簡鎵╁睍錛屽厑璁稿叾瀹冪被鍨嬬被鍨嬬殑瀛樺湪銆備嬌鐢ㄤ綅鍩熺殑涓昏鐩殑鏄帇緙╁瓨鍌紝鍏跺ぇ鑷磋鍒欎負錛?br>1) 濡傛灉鐩擱偦浣嶅煙瀛楁鐨勭被鍨嬬浉鍚岋紝涓斿叾浣嶅涔嬪拰灝忎簬綾誨瀷鐨剆izeof澶у皬錛屽垯鍚庨潰鐨勫瓧孌靛皢绱ч偦鍓嶄竴涓瓧孌靛瓨鍌紝鐩村埌涓嶈兘瀹圭撼涓烘錛?br>2) 濡傛灉鐩擱偦浣嶅煙瀛楁鐨勭被鍨嬬浉鍚岋紝浣嗗叾浣嶅涔嬪拰澶т簬綾誨瀷鐨剆izeof澶у皬錛屽垯鍚庨潰鐨勫瓧孌靛皢浠庢柊鐨勫瓨鍌ㄥ崟鍏冨紑濮嬶紝鍏跺亸縐婚噺涓哄叾綾誨瀷澶у皬鐨勬暣鏁板嶏紱
3) 濡傛灉鐩擱偦鐨勪綅鍩熷瓧孌電殑綾誨瀷涓嶅悓錛屽垯鍚勭紪璇戝櫒鐨勫叿浣撳疄鐜版湁宸紓錛孷C6閲囧彇涓嶅帇緙╂柟寮忥紝Dev-C++閲囧彇鍘嬬緝鏂瑰紡錛?br>4) 濡傛灉浣嶅煙瀛楁涔嬮棿絀挎彃鐫闈炰綅鍩熷瓧孌碉紝鍒欎笉榪涜鍘嬬緝錛?br>5) 鏁翠釜緇撴瀯浣撶殑鎬誨ぇ灝忎負鏈瀹藉熀鏈被鍨嬫垚鍛樺ぇ灝忕殑鏁存暟鍊嶃?br>榪樻槸璁╂垜浠潵鐪嬬湅渚嬪瓙銆?br>紺轟緥1錛?br>struct BF1
{
char f1 : 3;
char f2 : 4;
char f3 : 5;
};
鍏跺唴瀛樺竷灞涓猴細
|_f1__|__f2__|_|____f3___|____|
|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
0 3 7 8 1316
浣嶅煙綾誨瀷涓篶har錛岀1涓瓧鑺備粎鑳藉綰充笅f1鍜宖2錛屾墍浠2琚帇緙╁埌絎?涓瓧鑺備腑錛岃宖3鍙?br>鑳戒粠涓嬩竴涓瓧鑺傚紑濮嬨傚洜姝izeof(BF1)鐨勭粨鏋滀負2銆?br>紺轟緥2錛?br>struct BF2
{
char f1 : 3;
short f2 : 4;
char f3 : 5;
};
鐢變簬鐩擱偦浣嶅煙綾誨瀷涓嶅悓錛屽湪VC6涓叾sizeof涓?錛屽湪Dev-C++涓負2銆?br>紺轟緥3錛?br>struct BF3
{
char f1 : 3;
char f2;
char f3 : 5;
};
闈炰綅鍩熷瓧孌電┛鎻掑湪鍏朵腑錛屼笉浼氫駭鐢熷帇緙╋紝鍦╒C6鍜孌ev-C++涓緱鍒扮殑澶у皬鍧囦負3銆?br>9. 鑱斿悎浣撶殑sizeof
緇撴瀯浣撳湪鍐呭瓨緇勭粐涓婃槸欏哄簭寮忕殑錛岃仈鍚堜綋鍒欐槸閲嶅彔寮忥紝鍚勬垚鍛樺叡浜竴孌靛唴瀛橈紝鎵浠ユ暣涓仈鍚堜綋鐨剆izeof涔熷氨鏄瘡涓垚鍛榮izeof鐨勬渶澶у箋傜粨鏋勪綋鐨勬垚鍛樹篃鍙互鏄鍚堢被鍨嬶紝榪欓噷錛屽鍚堢被鍨嬫垚鍛樻槸琚綔涓烘暣浣撹冭檻鐨勩?br>鎵浠ワ紝涓嬮潰渚嬪瓙涓紝U鐨剆izeof鍊肩瓑浜巗izeof(s)銆?br>union U
{
int i;
char c;
S1 s;
};
杞嚜錛?a >http://blog.sina.com.cn/s/blog_5af743940100ctd9.html
]]>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
void CountSort(int a[], int b[],int k,int num)

{
int* c = new int[k+1];
for (int i=0;i<=k;i++)
c[i]=0;
int size = num;
for (int j=0;j<size;j++)
c[a[j]]++;
//c[i]鍖呭惈絳変簬i鐨勫厓绱犱釜鏁?/span>
for (i=1;i<=k;i++)
c[i]=c[i]+c[i-1];
//c[i]鍖呭惈灝忎簬絳変簬i鐨勫厓绱犱釜鏁?/span>
for (j=size-1;j>=0;j--)
{
b[c[a[j]]-1]=a[j];
c[a[j]]--;
}
delete [] c;
}
void main()

{
int num,max;
cout<<"杈撳叆鏈澶ф暣鏁板強杈撳叆涓暟"<<endl;
cin>>max;
cin>>num;
int* a = new int[num];
int* b = new int[num];
cout<<"鎺掑簭鍓嶏細"<<endl;
for(int i=0;i<num;i++)
{
cin>>a[i];
if (a[i]>max)
i--;
}
CountSort(a,b,max,num);
cout<<"鎺掑簭鍚庯細"<<endl;
for (int j=0;j<num;j++)
{
cout<<b[j]<<endl;
}
delete [] a;
delete [] b;
}
]]>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int Partition(int a[],int p,int r)

{
int ran=rand()%(r-p+1)+p; //闅忓嵆閫夊彇鍗叺
swap(a[ran],a[r]);
int x=a[r];
int i=p-1;
for (int j=p;j<r;j++)
{
if (a[j]<=x) //灝忎簬鍗叺鐨勫艱繘琛屽鎹?/span>
{
i++;
swap(a[i],a[j]);
}
}
swap(a[i+1],a[r]);
return i+1;
} 
void QuickSort(int a[],int p,int r)

{
int q;
if (p<r)
{
q=Partition(a,p,r);
QuickSort(a,p,q-1);
QuickSort(a,q+1,r);
}
}
int BinarySearch(int a[],int min,int max,int x)

{
int mid;
while (min<max)
{
mid=min+(max-min)/2;
if (a[mid]>=x)
max=mid;
else
min=mid+1; //鑻ヤ笉鍔犱竴鍙兘瀛樺湪鏃犻檺寰幆鐨勭粨鏋?/span>
}
if (a[min]==x)
return min;
else if(a[max]==x)
return max;
else
return -1;
}
void main()

{
int num;
cin>>num;
int* a = new int[num];
cout<<"鎺掑簭鍓嶏細"<<endl;
for(int i=0;i<num;i++)
cin>>a[i];
QuickSort(a,0,num-1);
cout<<"鎺掑簭鍚庯細"<<endl;
for (int j=0;j<num;j++)
{
cout<<a[j]<<endl;
}
cout<<"杈撳叆瑕佹煡鎵劇殑鏁?/span>"<<endl;
int x;
cin>>x;
int result=BinarySearch(a,0,num-1,x);
if (result>=0)
cout<<"鐩爣浣嶇疆涓?"<<result+1<<endl;
else
cout<<"鐩爣涓嶅湪鏁扮粍涓?/span>"<<endl;
}
]]>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
using namespace std;
template<class T> void Max_Heap(T a[],int i,int size)

{
int r = 2*i + 2;
int l = 2*i + 1;
int largest;
if (l<size && a[l]>=a[i])
{
largest = l;
}
else
largest = i;
if (r<size && a[r] >=a[largest])
{
largest = r;
}
if (largest != i)
{
swap(a[largest],a[i]);
Max_Heap(a,largest,size);
}
}
template<class T> void Build_Max_Heap(T a[],int size)

{
for (int i=size/2;i>=0;i--)
{
Max_Heap(a,i,size);
}
}
template<class T> void HeapSort(T a[],int size)

{
Build_Max_Heap(a,size);
for (int i=size-1;i>=1;i--)
{
swap(a[i],a[0]); //灝嗘渶澶ф暟鎹㈠埌鏁扮粍鏈熬
//show(a,size);
size--;
Max_Heap(a,0,size);
}
}
template<class T> void show(T a[],int size)

{
cout<<"鎺掑簭鍚庣粨鏋滀負"<<endl;
for (int i=0;i<size;i++)
{
cout<<a[i]<<endl;
}
}
int main()

{
int num;
cin>>num;
char* input = new char[num];
for (int i=0;i<num;i++)
{
cin>>input[i];
}
// Build_Max_Heap(input,num); //寤虹珛鏈澶у爢
HeapSort(input,num); //鍫嗘帓搴?/span>
show(input,num);
}
]]>
]]>
鍚堝茍榪囩▼涓殑鍙橀噺瀹規(guī)槗鎼為敊錛岀壒鍒笉鑳藉拷鐣ュ悎騫惰繃紼嬩腑鏁扮粍杈圭晫鐨勬敼鍙橈紒錛侊紒
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
void Merge(int a[],int p,int k,int q)

{
int num1=k-p+1;
int num2=q-k;
int i;
int* b1=new int[num1+1];
int* b2=new int[num2+1];
for (i=0;i<num1;i++)
{
b1[i]=a[p+i];
}
b1[i]=9999;
for (i=0;i<num2;i++)
{
b2[i]=a[k+i+1];
}
b2[i]=9999;
int j=0;i=0;
for(int kk=p;kk<=q;kk++) //娉ㄦ剰錛侊紒錛?br>
{
if (b1[i]<=b2[j])
a[kk]=b1[i++];
else
a[kk]=b2[j++];
}
delete [] b1;
delete [] b2;
}
void MergeSort(int a[],int p,int q)

{
if (p<q)
{
int k=(p+q)/2;
MergeSort(a,p,k);
MergeSort(a,k+1,q);
Merge(a,p,k,q);
}
}
int main()

{
int n;
scanf("%d",&n);
int* input=new int[n];
for (int i=0;i<n;i++)
{
scanf("%d",&input[i]);
}
MergeSort(input,0,n-1);
for (int i=0;i<n;i++)
{
printf("%d ",input[i]);
}
delete [] input;
}

]]>
]]>
void PreOrderUnrec(Bitree *t)
{
Stack s;
StackInit(s);
Bitree *p=t;
while (p!=NULL || !StackEmpty(s))
{
while (p!=NULL) //閬嶅巻宸﹀瓙鏍?br> {
visite(p->data);
push(s,p);
p=p->lchild;
}
if (!StackEmpty(s)) //閫氳繃涓嬩竴嬈″驚鐜腑鐨勫唴宓寃hile瀹炵幇鍙沖瓙鏍?wèi)閬嶅?br> {
p=pop(s);
p=p->rchild;
}//endif
}//endwhile
}
2.涓簭閬嶅巻闈為掑綊綆楁硶
void InOrderUnrec(Bitree *t)
{
Stack s;
StackInit(s);
Bitree *p=t;
while (p!=NULL || !StackEmpty(s))
{
while (p!=NULL) //閬嶅巻宸﹀瓙鏍?br> {
push(s,p);
p=p->lchild;
}
if (!StackEmpty(s))
{
p=pop(s);
visite(p->data); //璁塊棶鏍圭粨鐐?br> p=p->rchild; //閫氳繃涓嬩竴嬈″驚鐜疄鐜板彸瀛愭爲(wèi)閬嶅巻
}//endif
}//endwhile
}
3.鍚庡簭閬嶅巻闈為掑綊綆楁硶
typedef enum{L,R} tagtype;
typedef struct
{
Bitree ptr;
tagtype tag;
}stacknode;
typedef struct
{
stacknode Elem[maxsize];
int top;
}SqStack;
void PostOrderUnrec(Bitree t)
{
SqStack s;
stacknode x;
StackInit(s);
p=t;
do
{
while (p!=null) //閬嶅巻宸﹀瓙鏍?br> {
x.ptr = p;
x.tag = L; //鏍囪涓哄乏瀛愭爲(wèi)
push(s,x);
p=p->lchild;
}
while (!StackEmpty(s) && s.Elem[s.top].tag==R)
{
x = pop(s);
p = x.ptr;
visite(p->data); //tag涓篟錛岃〃紺哄彸瀛愭爲(wèi)璁塊棶瀹屾瘯錛屾晠璁塊棶鏍圭粨鐐?nbsp;
}
if (!StackEmpty(s))
{
s.Elem[s.top].tag =R; //閬嶅巻鍙沖瓙鏍?br> p=s.Elem[s.top].ptr->rchild;
}
}while (!StackEmpty(s));
}//PostOrderUnrec
浜屻傚墠搴忔渶綆媧佺畻娉?br>void PreOrderUnrec(Bitree *t)
{
Bitree *p;
Stack s;
s.push(t);
{
s.pop(p);
visit(p->data);
if (p->rchild != NULL) s.push(p->rchild);
if (p->lchild != NULL) s.push(p->lchild);
}
}
涓夈傚悗搴忕畻娉曚箣浜?/strong>
void BT_PostOrderNoRec(pTreeT root)
{
stack<treeT *> s;
pTreeT pre=NULL;
{
if (NULL != root)
{
s.push(root);
root = root->left;
}
else
{
root = s.top();
if (root->right!=NULL && pre!=root->right){
root=root->right;
}
else{
root=pre=s.top();
visit(root);
s.pop();
root=NULL;
}
}
}
}
]]>
涓句釜綆鍗曠殑渚嬪瓙銆?/p>
緇欎綘100000涓暱搴︿笉瓚呰繃10鐨勫崟璇嶃傚浜庢瘡涓涓崟璇嶏紝鎴戜滑瑕佸垽鏂粬鍑烘病鍑虹幇榪囷紝濡傛灉鍑虹幇浜嗭紝絎竴嬈″嚭鐜扮鍑犱釜浣嶇疆銆?br>榪欓褰撶劧鍙互鐢╤ash鏉ワ紝浣嗘槸鎴戣浠嬬粛鐨勬槸trie鏍?wèi)銆傚湪鏌愪簺鏂歸潰瀹冪殑鐢ㄩ旀洿澶с傛瘮濡傝瀵逛簬鏌愪竴涓崟璇嶏紝鎴戣璇㈤棶瀹冪殑鍓嶇紑鏄惁鍑虹幇榪囥傝繖鏍穐ash灝變笉濂芥悶浜嗭紝鑰岀敤trie榪樻槸寰堢畝鍗曘?br>鐜板湪鍥炲埌渚嬪瓙涓紝濡傛灉鎴戜滑鐢ㄦ渶鍌?cè)潥勬栆?guī)硶錛屽浜庢瘡涓涓崟璇嶏紝鎴戜滑閮借鍘繪煡鎵懼畠鍓嶉潰鐨勫崟璇嶄腑鏄惁鏈夊畠銆傞偅涔堣繖涓畻娉曠殑澶嶆潅搴﹀氨鏄疧(n^2)銆傛樉鐒跺浜?00000鐨勮寖鍥撮毦浠ユ帴鍙椼傜幇鍦ㄦ垜浠崲涓濊礬鎯熾傚亣璁炬垜瑕佹煡璇㈢殑鍗曡瘝鏄痑bcd錛岄偅涔堝湪浠栧墠闈㈢殑鍗曡瘝涓紝浠錛宑錛宒錛宖涔嬬被寮澶寸殑鎴戞樉鐒朵笉蹇呰冭檻銆傝屽彧瑕佹壘浠寮澶寸殑涓槸鍚﹀瓨鍦╝bcd灝卞彲浠ヤ簡銆傚悓鏍風(fēng)殑錛屽湪浠寮澶翠腑鐨勫崟璇嶄腑錛屾垜浠彧瑕佽冭檻浠浣滀負絎簩涓瓧姣嶇殑……榪欐牱涓涓爲(wèi)鐨勬ā鍨嬪氨娓愭笎娓呮櫚浜?#8230;…
鍋囪鏈塨錛宎bc錛宎bd錛宐cd錛宎bcd錛宔fg錛宧ii榪?涓崟璇嶏紝鎴戜滑鏋勫緩鐨勬爲(wèi)灝辨槸榪欐牱鐨勩?br>
瀵逛簬姣忎竴涓妭鐐癸紝浠庢牴閬嶅巻鍒頒粬鐨勮繃紼嬪氨鏄竴涓崟璇嶏紝濡傛灉榪欎釜鑺傜偣琚爣璁頒負綰㈣壊錛屽氨琛ㄧず榪欎釜鍗曡瘝瀛樺湪錛屽惁鍒欎笉瀛樺湪銆?br>閭d箞錛屽浜庝竴涓崟璇嶏紝鎴戝彧瑕侀『鐫浠栦粠璺熻蛋鍒板搴旂殑鑺傜偣錛屽啀鐪嬭繖涓妭鐐規(guī)槸鍚﹁鏍囪涓虹孩鑹插氨鍙互鐭ラ亾瀹冩槸鍚﹀嚭鐜拌繃浜嗐傛妸榪欎釜鑺傜偣鏍囪涓虹孩鑹詫紝灝辯浉褰撲簬鎻掑叆浜嗚繖涓崟璇嶃?br>榪欐牱涓鏉ユ垜浠闂拰鎻掑叆鍙互涓璧峰畬鎴愶紝鎵鐢ㄦ椂闂翠粎浠呬負鍗曡瘝闀垮害錛屽湪榪欎竴涓牱渚嬶紝渚挎槸10銆?br>鎴戜滑鍙互鐪嬪埌錛宼rie鏍?wèi)姣忎竴灞傜殑鑺傜偣鏁版槸26^i綰у埆鐨勩傛墍浠ヤ負浜嗚妭鐪佺┖闂淬傛垜浠敤鍔ㄦ侀摼琛紝鎴栬呯敤鏁扮粍鏉ユā鎷熷姩鎬併傜┖闂寸殑鑺辮垂錛屼笉浼氳秴榪囧崟璇嶆暟×鍗曡瘝闀垮害銆?/p>
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

const int num_chars = 26;


class Trie
{
public:
Trie():root(NULL)
{};
Trie(Trie& tr);
int search(const char* word, char* entry ) const;
int insert(const char* word, const char* entry);
int remove(const char* word, char* entry);
private:
struct Trie_node
{
char* data;
Trie_node* branch[num_chars];
Trie_node();
}* root;
};
Trie::Trie_node::Trie_node() 

{
data = NULL;
for (int i=0; i<num_chars; ++i)
branch[i] = NULL;
}
int Trie::search(const char* word, char* entry ) const 

{
int position = 0;
char char_code;
Trie_node *location = root;
while( location!=NULL && *word!=0 ) 
{
if (*word>='A' && *word<='Z')
char_code = *word-'A';
else if (*word>='a' && *word<='z')
char_code = *word-'a';
else return 0;

location = location->branch[char_code];
position++;
word++;
}
if ( location != NULL && location->data != NULL ) 
{
strcpy(entry,location->data);
return 1;
}
else return 0;
}
int Trie::insert(const char* word, const char* entry) 

{
int result = 1, position = 0;
if ( root == NULL ) root = new Trie_node;
char char_code;
Trie_node *location = root;
while( location!=NULL && *word!=0 )
{
if (*word>='A' && *word<='Z')
char_code = *word-'A';
else if (*word>='a' && *word<='z')
char_code = *word-'a';
else return 0;

if( location->branch[char_code] == NULL )
location->branch[char_code] = new Trie_node;

location = location->branch[char_code];
position++;
word++;
}
if (location->data != NULL)
result = 0;
else
{
location->data = new char[strlen(entry)+1];
strcpy(location->data, entry);
}
return result;
}
int main()

{
Trie t;
char entry[100];
t.insert("aa", "DET");
t.insert("abacus","NOUN");
t.insert("abalone","NOUN");
t.insert("abandon","VERB");
t.insert("abandoned","ADJ");
t.insert("abashed","ADJ");
t.insert("abate","VERB");
t.insert("this", "PRON");
if (t.search("this", entry))
cout<<"'this' was found. pos: "<<entry<<endl;
if (t.search("abate", entry))
cout<<"'abate' is found. pos: "<<entry<<endl;
if (t.search("baby", entry))
cout<<"'baby' is found. pos: "<<entry<<endl;
else
cout<<"'baby' does not exist at all!"<<endl;
if (t.search("aa", entry))
cout<<"'aa was found. pos: "<<entry<<endl;
}

PS:瀹炵幇鏂規(guī)硶http://met.fzu.edu.cn/eduonline/web/web/resources/articleContent.asp?id=346
]]>
紼嬪簭浠g爜 紼嬪簭浠g爜
int num[100];
Sample:
int cmp ( const void *a , const void *b )
{
return *(int *)a - *(int *)b;
}
qsort(num,100,sizeof(num[0]),cmp);
浜屻佸char綾誨瀷鏁扮粍鎺掑簭錛堝悓int綾誨瀷錛?br>紼嬪簭浠g爜 紼嬪簭浠g爜
char word[100];
Sample:
int cmp( const void *a , const void *b )
{
return *(char *)a - *(char*)b;
}
qsort(word,100,sizeof(word[0]),cmp)
涓夈佸double綾誨瀷鏁扮粍鎺掑簭錛堢壒鍒娉ㄦ剰錛?br>紼嬪簭浠g爜 紼嬪簭浠g爜
double in[100];
int cmp( const void *a , const void *b )
{
return *(double *)a > *(double *)b ? 1 : -1;
} qsort(in,100,sizeof(in[0]),cmp)錛?br>
鍥涖佸緇撴瀯浣撲竴綰ф帓搴?
紼嬪簭浠g爜 紼嬪簭浠g爜
struct In {
double data;
int other;
}s[100]
//鎸夌収data鐨勫間粠灝忓埌澶у皢緇撴瀯浣撴帓搴?鍏充簬緇撴瀯浣撳唴鐨勬帓搴忓叧閿暟鎹甦ata鐨勭被鍨嬪彲浠ュ緢澶氱錛?br>鍙傝冧笂闈㈢殑渚嬪瓙鍐?
int cmp( const void *a ,const void *b)
{
return (*(In *)a).data > (*(In *)b).data ? 1 : -1;
}
qsort(s,100,sizeof(s[0]),cmp);
浜斻佸緇撴瀯浣撲簩綰ф帓搴?
紼嬪簭浠g爜 紼嬪簭浠g爜
struct In {
int x; int y;
}s[100];
//鎸夌収x浠庡皬鍒板ぇ鎺掑簭錛屽綋x鐩哥瓑鏃舵寜鐓浠庡ぇ鍒板皬鎺掑簭
int cmp( const void *a , const void *b )
{
struct In *c = (In *)a;
struct In *d = (In *)b;
if(c->x != d->x) return c->x - d->x;
else return d->y - c->y;
}
qsort(s,100,sizeof(s[0]),cmp);
鍏佸瀛楃涓茶繘琛屾帓搴?
紼嬪簭浠g爜 紼嬪簭浠g爜
struct In {
int data; char str[100];
}s[100];
//鎸夌収緇撴瀯浣撲腑瀛楃涓瞫tr鐨勫瓧鍏擱『搴忔帓搴?
int cmp ( const void *a , const void *b )
{
return strcmp( (*(In *)a)->str , (*(In *)b)->str );
}
qsort(s,100,sizeof(s[0]),cmp);
涓冦佽綆楀嚑浣曚腑姹傚嚫鍖呯殑cmp
紼嬪簭浠g爜 紼嬪簭浠g爜
int cmp(const void *a,const void *b)
//閲嶇偣cmp鍑芥暟錛屾妸闄や簡1鐐瑰鐨勬墍鏈夌偣錛屾棆杞搴︽帓搴?
{
struct point *c=(point *)a;
struct point *d=(point *)b;
if( calc(*c,*d,p[1]) < 0) return 1;
else if( !calc(*c,*d,p[1])
&& dis(c->x,c->y,p[1].x,p[1].y) < dis(d->x,d->y,p[1].x,p[1].y))
//濡傛灉鍦ㄤ竴鏉$洿綰夸笂錛屽垯鎶婅繙鐨勬斁鍦ㄥ墠闈?
return 1; else return -1;
}
PS: 鍏朵腑鐨剄sort鍑芥暟鍖呭惈鍦?lt;stdlib.h>鐨勫ご鏂囦歡閲岋紝strcmp鍖呭惈鍦?lt;string.h>鐨勫ご鏂囦歡閲?
]]>
灝嗘墍鏈夌悆緙栧彿涓?-12錛?br>絎竴姝ワ紝灝?-4鍙鋒斁宸﹁竟錛?-8鍙鋒斁鍙寵竟錛?br> 鑻ュ炬枩錛屽亣璁懼彸閲嶏紙宸﹂噸闆峰悓錛夛細
絎簩姝0]錛屽皢2-4縐昏蛋錛?-8縐誨叆宸﹁竟錛?-11縐誨叆鍙寵竟錛?br> 鑻ヤ粛鍊炬枩錛屽垯涓嶅悓鐨勯偅涓負緙栧彿1鎴?鍙鳳細
絎笁姝0][0]錛屽皢1鍙蜂笌2鍙鋒瘮杈冿紝鑻ョ浉絳夛紝鍒?鍙峰亸閲嶏紝鍙嶄箣錛?鍙風(fēng)悆鍋忚交錛?br> 鑻ヤ笉鍊炬枩錛屽垯涓嶅悓鐨勯偅涓湪緙栧彿2-4涓紝涓斿亸杞伙紱
絎笁姝0][1]錛屼換鍙?-4縐嶄袱鐞冨彲寰楃粨鏋滐紱
鑻ヤ笉鍊炬枩錛屽垯涓嶅悓鐨勫湪9-12涓細
絎簩姝1]錛岀Щ璧?-8鐞冿紝縐誨叆9-11鐞冿細
鑻ュ炬枩錛屽亣璁懼彸閲嶏紙宸﹂噸闆峰悓錛夛細
絎笁姝1][0]錛屼換鍙?-11縐嶄袱鐞冨彲寰楃粨鏋滐紱
鑻ヤ笉鍊炬枩錛屽垯12鍙風(fēng)悆寮傚父錛?br> 絎笁姝1][1]錛屽皢12鍙風(fēng)悆涓庡叾瀹冧換鎰忕悆姣旇緝鍙煡鏄交鏄噸錛?/p>
濡傦細
0050 鎵姹傛暟涓?104
112 鎵姹傛暟涓?21