Yuav Ua Li Cas Rov Qab Ib Qho Array

Cov txheej txheem:

Yuav Ua Li Cas Rov Qab Ib Qho Array
Yuav Ua Li Cas Rov Qab Ib Qho Array

Video: Yuav Ua Li Cas Rov Qab Ib Qho Array

Video: Yuav Ua Li Cas Rov Qab Ib Qho Array
Video: FBI / KUB - Ntawv Caw (Official Full Song + Lyrics) 2024, Hlis ntuj nqeg
Anonim

Arrays yog ib qho ntawm cov qauv yooj yim tshaj plaws ntawm txheej txheem cov ntaub ntawv khaws cia. Txij li thaj chaw nco tau raug faib rau khaws cia ib qho ntawm cov kab, thiab feem ntau cov lus sau tau muaj kev tsim kho rau lawv cov lus tshaj tawm, lawv siv zog ua cov tsis zoo rau xa cov ntaub ntawv ntau mus rau ntau yam (suav nrog lub tsev qiv ntawv) kev khiav dej num. Nyeg, kev khiav dej num qee zaum xav tau rov ua ib qho array.

Yuav ua li cas rov qab ib qho array
Yuav ua li cas rov qab ib qho array

Tsim nyog

  • - cov ntawv nyeem editor;
  • - C lossis C ++ compiler thiab linker.

Cov Lus Qhia

Kauj ruam 1

Rov qab cov ntaub ntawv los ntawm txoj haujlwm lossis txoj kev los ntawm qhov muab nws tso rau hauv qhov ntsuas kom ntev, faib los ntawm tus neeg hu, thiab dhau los ntawm kev siv. Txhais cov hom ntaub ntawv tsim nyog, piv txwv li: typedef int t_array10 [10]; Siv nws hauv kev tshaj tawm thiab kev siv ntawm txoj haujlwm xav tau lossis txoj hauv kev: tsis muaj dab tsi ReturnArray (t_array10 & raOut) {raOut [0] = 10;} Hu rau qhov tsim nyog: int aNumbers [10]; ReturnArray (aNumbers); Qhov tsis zoo ntawm hom qauv no yog qhov ntev ntawm kev ua haujlwm yog tsau.

Kauj ruam 2

Xa rov qab cov ntaub ntawv array los ntawm muab nws tso rau hauv tus neeg hu-faib cov zis tsis tso cai los ntawm pointer. Cov qauv ntawm cov kev ua haujlwm lossis cov qauv tuaj yeem muaj qhov ntsuas tsis tau tshaj tawm ob qho tib si hauv kev cim tsis muaj qhov loj me: tsis muaj dab tsi xa rov qab (tsis muaj kev cuam tshuam , int nSize); cov teeb meem, tus cim parameter tuaj yeem siv rau hauv cov cim sau cia: tsis muaj dab tsi ReturnArray (int * pnArray, int nSize) {rau (nSize--; nSize> -1; pnArray [nSize] = nSize--);} // hu rau int aNumbers [10]; ReturnArray (aNumbers, sizeof (aNumbers) / sizeof (aNumbers [0])) Qhov no ntawm kev xa cov qauv mus rau cov haujlwm thiab xa rov qab cov ntaub ntawv los ntawm lawv tau siv feem ntau ntawm cov neeg mob thaum txhim kho C cov kev pabcuam. cov haujlwm thiab cov hau kev uas lees txais thiab xa cov ntaub ntawv hauv txoj kev no, raws li txoj cai, yuav tsum muaj qhov ntsuas ntxiv los ntawm kev dhau los ntawm qhov tseeb qhov loj ntawm tsis tawm.

Kauj ruam 3

Rov qab qhov uas tsim los hauv txoj haujlwm lossis hom. Raws li txoj cai, qhov no, tsis yog ib qho cuab yeej yuav tsum tau rov qab, tab sis tus taw qhia rau nws thawj lub ntsiab lus: int * ReturnArray () {rau cov menyuam * pnArray = tshiab rau cov menyuam [10]; // ua haujlwm nrog pnArray rov qab pnArray;} // tau txais ib lub pointer rau cov tsim array rau menyuam * pnNumbers = ReturnArray (); // siv rho tawm pnNumbers; // delete Cov qauv no muaj qee qhov kev txwv. Yog li, tus array yuav tsum nyob hauv qhov heap. Ib qho ntxiv, nws txoj kev tshem tawm yuav tsum tau ua nrog tus qauv sib haum rau kev tsim (rho tawm thaum siv qhov tshiab, pub dawb rau malloc, thiab lwm yam).

Kauj ruam 4

Siv cov thawv ntim khoom kawm kom yooj yim xa rov qab arrays thaum tsim C ++ cov kev zov me nyuam. Ib qho piv txwv ntawm kev tsim thiab xa rov qab ib qho array uas yog khoom siv ntawm chav kawm vector qauv ntawm C ++ Cov Qauv Qiv Cov qauv teev rau rau int yuav zoo li no: #include std:: vector ReturnArray () {std:: vector oArray; oArray.resize (10); oArray [0] = 100; oArray [1] = 200; rov qab oArray;} std:: vector oArray = ReturnArray (); Nco ntsoov tias feem ntau ntawm cov haujlwm nrov yog muab cov chav kawm muaj zog lossis cov chav kawm sib txawv ntawm cov khoom siv. Lawv feem ntau ua rau lub tswv yim ntawm kev siv cov ntaub ntawv nrog kev suav nrog suav thiab luam-rau-sau. Yog li ntawd, rov qab los ntawm tus nqi ntawm xws li arrays los ntawm kev ua haujlwm tau ua tau zoo heev thiab yog cuam tshuam nrog kev siv tsawg kawg ntawm cov peev txheej.

Pom zoo: