Cov txheej txheem:

C++ lambda yog dab tsi?
C++ lambda yog dab tsi?

Video: C++ lambda yog dab tsi?

Video: C++ lambda yog dab tsi?
Video: Lambda expressions in modern C++ (in depth step by step tutorial) 2024, Kaum ib hlis
Anonim

Hauv C++ 11 thiab tom qab ntawd, a lambda kev qhia-feem ntau hu ua a lambda -yog ib txoj hauv kev yooj yim ntawm kev txhais cov khoom siv tsis qhia npe (kaw) txoj cai ntawm qhov chaw uas nws tau hu los yog dhau los ua kev sib cav rau ib qho haujlwm.

Hais txog qhov no, hom twg yog Lambda C ++?

[C++ 11: 5.1. 2/3]: Ib hom ntawm lambda -expression (uas yog hom ntawm cov khoom kaw) yog ib qho tshwj xeeb, tsis muaj npe tsis yog koom haum chav kawm hom - hu ua kaw hom - nws cov khoom tau piav qhia hauv qab no. Chav kawm no hom tsis yog tag nrho (8.5.

Tsis tas li ntawd, vim li cas peb thiaj xav tau lambda kab lus hauv C ++? C++ qhia cov khoom muaj nuj nqi, lossis cov khoom siv. Functions yog cov chav kawm uas overload tus neeg teb xov tooj (). Lwm yam lus programming xws li Haskell, C#, Erlang lossis F# ua kom muaj nuj nqi txhais tau tias lawv siv. Cov no yog lub npe hu ua lambda lus vim nws syntax yog tshoov siab hauv lambda suav.

Tsis tas li cov lus nug yog, koj ua li cas dhau lub lambda muaj nuj nqi hauv C ++?

3 txoj hauv kev kom dhau lub lambda raws li kev sib cav rau ib qho haujlwm:

  1. Siv std::function tshaj tawm cov khoom lambda. void lambdaExample1()
  2. Siv typedef tshaj tawm hom kev ua haujlwm thiab muab nws ua haujlwm lambda. void lambdaExample2()
  3. Siv struct los tshaj tawm ib tug lambda.

Dab tsi yog lambda kaw?

A lambda yog qhov tseem ceeb ntawm kev ua haujlwm uas tau hais tseg hauv kab es tsis yog tus qauv ntawm kev tshaj tawm cov haujlwm. Lambdas nquag tuaj yeem dhau los ua khoom. A kaw yog ib txoj haujlwm uas kaw nws lub xeev ib puag ncig los ntawm kev xa tawm sab nraud rau nws lub cev.