Arrays yog ib qho ntawm feem ntau siv cov ntaub ntawv ntawm cov kev ceev cov ntaub ntawv khaws cia hauv cov khoos phis tawj. Lawv ua tiav tuaj yeem ua los ntawm ntau cov kev siv algorithms uas tau siv hauv chav kawm cov kev thiab cov haujlwm. Raws li, nws yog feem ntau yuav tsum kom dhau ib qho array rau ib txoj haujlwm. Cov lus C thiab C ++ muaj kev ywj pheej zoo hauv kev xaiv cov hau kev ntawm kev ua qhov haujlwm no.
Nws yog qhov tsim nyog
compilers ntawm C thiab C ++ hom lus
Cov Lus Qhia
Kauj ruam 1
Tshaj qhov ntsuas qhov loj me rau cov haujlwm. Hloov cov qauv ntawm kev ua haujlwm kom muaj kev sib cav ntawm hom tsim nyog. Piv txwv, cov lus tshaj tawm ntawm kev ua haujlwm uas siv ib qho kev ua lej ntawm cov naj npawb ntawm peb lub ntsiab raws li ib qho ntsuas yuav zoo li no:
tsis muaj dab tsi ArrayFunction (rau cov aNumbers [3]);
Xws li lub luag haujlwm yog hu los ntawm kev dhau ib qho ncaj qha rau nws raws li kev sib cav:
tsis muaj dab tsi SomeFunction ()
{
rau cov menyuam aNumbers = {1, 2, 3};
ArrayFunction (aNumbers);
}
Cov ntaub ntawv hloov pauv tau theej tawm rau pawg. Hloov kho qhov array hauv qhov hu ua haujlwm tsis hloov lub hauv paus.
Kauj ruam 2
Kis rau cov kuj sib txawv thiab ntev mus rau lub muaj nuj nqi. Ua li no, tsuas yog tsis qhia meej txog qhov loj me ntawm qhov sib cav sib ceg:
tsis muaj dab tsi ArrayFunction (rau cov aNumbers );
Multimimensional arrays kuj tuaj yeem dhau mus zoo ib yam (tsuas yog thawj "dimension" tuaj yeem hloov tau):
tsis muaj tseeb ArrayFunction (rau cov aNumbers [3] [2]);
Cov haujlwm no hu tau zoo ib yam li thawj kauj ruam.
Txhawm rau kom muaj peev xwm ua kom raug cov txheej txheem ntawm cov paub sib txawv ntev hauv ib qho kev ua haujlwm, koj yuav tsum yog qhia tseeb dhau tus lej ntawm lawv lub ntsiab los ntawm ib qho kev ntsuas ntxiv, lossis siv cov kev sib cog lus uas txwv kev txiav txim siab rau qhov tseem ceeb ntawm cov khoom lawv tus kheej (qee yam muaj nuj nqis yuav tsum yog ib qho cim ntawm qhov kawg ntawm kab ntawv).
Kauj ruam 3
Txheeb tus xov ntawm lub pointer. Qhov ua haujlwm sib cav yuav tsum yog lub taw taw rau cov nqi nrog ib hom coj ua rau cov ntsiab ntawm array. Piv txwv li:
tsis xws li ArrayFunction (int * pNumbers);
Kev nkag mus rau cov ntaub ntawv hauv ib qho kev ua haujlwm tuaj yeem nqa tawm ob qho tib si hauv kev sau cia rau kev ua haujlwm nrog array cov ntsiab lus, thiab siv qhov chaw nyob lej:
tsis muaj dab tsi ArrayFunction (rau cov menyuam * pNumbers)
{
pNumbers [0] = 10; // nkag tau rau cov khoom 0
* (pNumbers + 1) = 20; // nkag mus rau yam khoom 1
}
Ceevfaj! Txij li cov haujlwm tau dhau los tsis yog daim ntawv theej ntawm cov ntaub ntawv, tab sis tus pointer rau nws, cov thawj array yuav raug hloov kho.
Qhov kom zoo dua ntawm hom no yog kev nrawm, kev lag luam ntawm cov khoom siv suav nyiaj thiab ib qho kev hloov yooj yim. Yog li, koj tuaj yeem hu lub hom phiaj ua haujlwm los ntawm kev dhau nws tus taw qhia mus rau kev ntsuas lub ntsiab lus ntawm cov khoom:
tsis muaj dab tsi SomeFunction ()
{
rau cov menyuam aNumbers = {1, 2, 3};
ArrayFunction (aNumbers); // tag nrho array
ArrayFunction (& aNumbers [1]); // pib ntawm lub caij thib ob
}
Hom no ib txwm tseem cuam tshuam cov naj npawb ntawm cov khoom muaj nyob hauv ib qho ntsuas ntxiv lossis siv ib qho array terminator.
Kauj ruam 4
Xa cov ntaub ntawv mus rau ib qho kev ua haujlwm nrog qhov parameter uas yog ib qho khoom siv lossis kev siv rau cov khoom hauv chav kawm uas coj los ua txoj haujlwm array. Xws li cov chav kawm lossis cov qauv chav kawm yog feem ntau pom nyob hauv cov tsev qiv ntawv nrov thiab kev coj ua (QVector hauv Qt, CArray hauv MFC, std:: vector hauv STL, thiab lwm yam).
Feem ntau cov chav kawm no siv lub tswv yim sib piv cov ntaub ntawv nrog suav suav sau, ua ib daim sib sib zog nqus tsuas yog thaum cov ntaub ntawv raug hloov kho (theej ntawm sau). Qhov no tso cai rau koj kom tsawg dua kev noj haus ntawm cov khoom siv kev suav tab txawm tias dhau ntawm cov khoom siv dhau los ntawm tus nqi los ntawm kev sib cav ntawm kev ua haujlwm thiab cov hau kev:
khoob ArrayFunction (QVector oArray)
{
rau cov menyuam nItemCount = oArray.count ();
rau cov menyuam nemem = oArray [0];
}
tsis muaj dab tsi SomeFunction ()
{
QVector oArray (10);
rau (rau cov menyuam = =; i