C++11中的stoi和stod怎么使用(C++,开发技术)

时间:2024-04-28 18:49:15 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

    C%2B%2B11%E4%B8%AD%E7%9A%84stoi%E5%92%8Cstod%E6%80%8E%E4%B9%88%E4%BD%BF%E7%94%A8

接下来,请跟着小编一起来学习吧!

}

c++11新特性集合

#C++11

Seealso:std::move,std::forward,forwardingreferences.

Templatetypeparameterdeductionwithlvaluesandrvalues:

Seealso:std::move,std::forward,rvaluereferences.

The...syntaxcreatesa_parameterpack_orexpandsone.Atemplate_parameterpack_isatemplateparameterthatacceptszeroormoretemplatearguments(non-types,types,ortemplates).Atemplatewithatleastoneparameterpackiscalledavariadictemplate.

Aninterestinguseforthisiscreatingan_initializerlist_froma_parameterpack_inordertoiterateovervariadicfunctionarguments.

Alightweightarray-likecontainerofelementscreatedusinga"bracedlist"syntax.Forexample,{1,2,3}createsasequencesofintegers,thathastypestd::initializer_list<int>.Usefulasareplacementtopassingavectorofobjectstoafunction.

Assertionsthatareevaluatedatcompile-time.

auto-typedvariablesarededucedbythecompileraccordingtothetypeoftheirinitializer.

Extremelyusefulforreadability,especiallyforcomplicatedtypes:

Functionscanalsodeducethereturntypeusingauto.InC++11,areturntypemustbespecifiedeitherexplicitly,orusingdecltypelikeso:

Thetrailingreturntypeintheaboveexampleisthedeclaredtype(seesectionondecltype)oftheexpressionx+y.Forexample,ifxisanintegerandyisadouble,decltype(x+y)isadouble.Therefore,theabovefunctionwilldeducethetypedependingonwhattypetheexpressionx+yyields.Noticethatthetrailingreturntypehasaccesstoitsparameters,andthiswhenappropriate.

Bydefault,value-capturescannotbemodifiedinsidethelambdabecausethecompiler-generatedmethodismarkedasconst.Themutablekeywordallowsmodifyingcapturedvariables.Thekeywordisplacedaftertheparameter-list(whichmustbepresentevenifitisempty).

decltypeisanoperatorwhichreturnsthe_declaredtype_ofanexpressionpassedtoit.cv-qualifiersandreferencesaremaintainediftheyarepartoftheexpression.Examplesofdecltype:

Seealso:decltype(auto)(C++14).

Semanticallysimilartousingatypedefhowever,typealiaseswithusingareeasiertoreadandarecompatiblewithtemplates.

C++11introducesanewnullpointertypedesignedtoreplaceC'sNULLmacro.nullptritselfisoftypestd::nullptr_tandcanbeimplicitlyconvertedintopointertypes,andunlikeNULL,notconvertibletointegraltypesexceptbool.

Type-safeenumsthatsolveavarietyofproblemswithC-styleenumsincluding:implicitconversions,inabilitytospecifytheunderlyingtype,scopepollution.

Attributesprovideauniversalsyntaxover__attribute__(...),__declspec,etc.

Constantexpressionsareexpressionsevaluatedbythecompileratcompile-time.Onlynon-complexcomputationscanbecarriedoutinaconstantexpression.Usetheconstexprspecifiertoindicatethevariable,function,etc.isaconstantexpression.

constexprvaluesarethosethatthecompilercanevaluateatcompile-time:

Constantexpressionswithclasses:

Constructorscannowcallotherconstructorsinthesameclassusinganinitializerlist.

User-definedliteralsallowyoutoextendthelanguageandaddyourownsyntax.Tocreatealiteral,defineaToperator""X(...){...}functionthatreturnsatypeT,withanameX.Notethatthenameofthisfunctiondefinesthenameoftheliteral.Anyliteralnamesnotstartingwithanunderscorearereservedandwon'tbeinvoked.Therearerulesonwhatparametersauser-definedliteralfunctionshouldaccept,accordingtowhattypetheliteraliscalledon.

ConvertingCelsiustoFahrenheit:

Stringtointegerconversion:

Specifiesthatavirtualfunctionoverridesanothervirtualfunction.Ifthevirtualfunctiondoesnotoverrideaparent'svirtualfunction,throwsacompilererror.

Specifiesthatavirtualfunctioncannotbeoverriddeninaderivedclassorthataclasscannotbeinheritedfrom.

Classcannotbeinheritedfrom.

Amoreelegant,efficientwaytoprovideadefaultimplementationofafunction,suchasaconstructor.

Withinheritance:

Amoreelegant,efficientwaytoprovideadeletedimplementationofafunction.Usefulforpreventingcopiesonobjects.

Syntacticsugarforiteratingoveracontainer'selements.

Notethedifferencewhenusingintasopposedtoint&:

Thecopyconstructorandcopyassignmentoperatorarecalledwhencopiesaremade,andwithC++11'sintroductionofmovesemantics,thereisnowamoveconstructorandmoveassignmentoperatorformoves.

Convertingconstructorswillconvertvaluesofbracedlistsyntaxintoconstructorarguments.

Notethatthebracedlistsyntaxdoesnotallownarrowing:

Notethatifaconstructoracceptsastd::initializer_list,itwillbecalledinstead:

Conversionfunctionscannowbemadeexplicitusingtheexplicitspecifier.

Allmembersofaninlinenamespacearetreatedasiftheywerepartofitsparentnamespace,allowingspecializationoffunctionsandeasingtheprocessofversioning.Thisisatransitiveproperty,ifAcontainsB,whichinturncontainsCandbothBandCareinlinenamespaces,C'smemberscanbeusedasiftheywereonA.

Allowsnon-staticdatamemberstobeinitializedwheretheyaredeclared,potentiallycleaningupconstructorsofdefaultinitializations.

C++11isnowabletoinferwhenaseriesofrightanglebracketsisusedasanoperatororasaclosingstatementoftypedef,withouthavingtoaddwhitespace.

Memberfunctionscannowbequalifieddependingonwhether*thisisanlvalueorrvaluereference.

C++11allowsfunctionsandlambdasanalternativesyntaxforspecifyingtheirreturntypes.

Thisfeatureisespeciallyusefulwhencertainreturntypescannotberesolved:

InC++14,decltype(auto)canbeusedinstead.

Thenoexceptspecifierspecifieswhetherafunctioncouldthrowexceptions.Itisanimprovedversionofthrow().

Non-throwingfunctionsarepermittedtocallpotentially-throwingfunctions.Wheneveranexceptionisthrownandthesearchforahandlerencounterstheoutermostblockofanon-throwingfunction,thefunctionstd::terminateiscalled.

Transferringstd::unique_ptrs:

AnexampleofafunctionwrapperwhichjustforwardsotherAobjectstoanewAobject'scopyormoveconstructor:

Seealso:forwardingreferences,rvaluereferences.

Thestd::threadlibraryprovidesastandardwaytocontrolthreads,suchasspawningandkillingthem.Intheexamplebelow,multiplethreadsarespawnedtododifferentcalculationsandthentheprogramwaitsforallofthemtofinish.

Convertsanumericargumenttoastd::string.

Typetraitsdefinesacompile-timetemplate-basedinterfacetoqueryormodifythepropertiesoftypes.

Astd::shared_ptrisasmartpointerthatmanagesaresourcethatissharedacrossmultipleowners.Asharedpointerholdsa_controlblock_whichhasafewcomponentssuchasthemanagedobjectandareferencecounter.Allcontrolblockaccessisthread-safe,however,manipulatingthemanagedobjectitselfisnotthread-safe.

Thechronolibrarycontainsasetofutilityfunctionsandtypesthatdealwithdurations,clocks,andtimepoints.Oneusecaseofthislibraryisbenchmarkingcode:

Tuplesareafixed-sizecollectionofheterogeneousvalues.Accesstheelementsofastd::tuplebyunpackingusingstd::tie,orusingstd::get.

Createsatupleoflvaluereferences.Usefulforunpackingstd::pairandstd::tupleobjects.Usestd::ignoreasaplaceholderforignoredvalues.InC++17,structuredbindingsshouldbeusedinstead.

std::arrayisacontainerbuiltontopofaC-stylearray.Supportscommoncontaineroperationssuchassorting.

ThecompilerisfreetocallnewT{},thenfunction_that_throws(),andsoon...SincewehaveallocateddataontheheapinthefirstconstructionofaT,wehaveintroducedaleakhere.Withstd::make_shared,wearegivenexception-safety:

std::ref(val)isusedtocreateobjectoftypestd::reference_wrapperthatholdsreferenceofval.Usedincaseswhenusualreferencepassingusing&doesnotcompileor&isdroppedduetotypededuction.std::crefissimilarbutcreatedreferencewrapperholdsaconstreferencetoval.

std::beginandstd::endfreefunctionswereaddedtoreturnbeginandenditeratorsofacontainergenerically.Thesefunctionsalsoworkwithrawarrayswhichdonothavebeginandendmemberfunctions.

AnthonyCalandra

See:https://github.com/AnthonyCalandra/modern-cpp-features/graphs/contributors

MIT

若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

本文:C++11中的stoi和stod怎么使用的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:C++怎么把二叉搜索树转换累加树下一篇:

44 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18