C++课程设计学生成绩信息管理系统包括doc报告文档exe可执行文件cpp源代码以及txt格式源Word下载.docx
《C++课程设计学生成绩信息管理系统包括doc报告文档exe可执行文件cpp源代码以及txt格式源Word下载.docx》由会员分享,可在线阅读,更多相关《C++课程设计学生成绩信息管理系统包括doc报告文档exe可执行文件cpp源代码以及txt格式源Word下载.docx(25页珍藏版)》请在冰点文库上搜索。
time/t,就不允许修改系统时间了(缺点:
仅仅显示小时和分钟)
#include<
usingnamespacestd;
voidmain()
{
system("
time/t"
pause"
}
改变DOS字体颜色
colorob"
注:
当system(“colorob”)中的ob变为相应的数字时,DOS屏幕字体变为相应的颜色
改变DOS标题
title王万腾"
打印出系统日期
date"
date/t"
for_each的简单应用
#include<
vector>
string>
algorithm>
//特别注意:
for_each()还是在algorithm头文件中定义的来
voidprint_i(intt)
{
cout<
<
t<
ends;
}
intmain()
inta[]={1,2,3,4,5,6,7,8,9,10,11,12,13};
vector<
int>
elements(a,a+13);
for_each(elements.begin(),elements.end(),print_i);
endl;
return0;
strings("
WangWanteng"
string:
:
size_typelen2=s.size();
"
s.capacity():
s.capacity()<
ends<
s.size():
len2<
总结:
若s为一字符串,则s.size()==s.length(),s.capacity()总比s.size()或s.length()大4个字符
string的substr()
strings1("
MaYu"
s1=s.substr(4,3);
s1<
上例:
s.substr(4,3)是指从s串第(4+1)个字符开始往后数3个字符返回
关于swap()的传说
Wangwanteng"
王万腾"
swap(s,s1);
inta=1;
intb=2;
swap(a,b);
a<
s<
FunctionObjects
Definition
Afunctionobjectisanobjecttowhichthefunctioncalloperatorcanbeapplied.Typically,itisaclassthatdefinesthefunctioncalloperator(operator()())asamemberfunction.Whenafunctionobjectisusedasafunction,thefunctioncalloperatorisinvokedwheneverthefunctioniscalled.Considerthefollowingclassdefinition:
classbiggerThanThree
public:
booloperator()(intval)const
{returnval>
3;
}
};
IfwecreateaninstanceofclassbiggerThanThree,everytimewereferencethisobjectusingthefunctioncallsyntax,thefunctioncalloperatormemberfunctionisinvoked.Togeneralizethisclass,weaddaconstructorandaconstantdatamember,whichissetbytheconstructor:
classbiggerThan
constinttestValue;
biggerThan(intx):
testValue(x){}
booloperator()(intval)const
{returnval>
testValue;
TheresultisageneralbiggerthanXfunction,wherethevalueofXisdeterminedwhenwecreateaninstanceoftheclass.Wecandoso,forexample,asanargumenttooneofthegenericfunctionsthatrequireapredicate.Inthismannerthefollowingcodefindsthefirstvalueinalistthatislargerthan12:
std:
list<
iteratorfirstBig=
std:
find_if(aList.begin(),aList.end(),biggerThan(12));
3.2.2Use
Thereareanumberofsituationswhereitisconvenienttosubstitutefunctionobjectsinplaceoffunctions:
touseanexistingfunctionobjectprovidedbytheC++StandardLibraryinsteadofanewfunction;
toimproveexecutionbyusinginlinefunctioncalls;
andtoallowafunctionobjecttoaccessorsetstateinformationthatisheldbyanobject.Let'
sdealwitheachoftheseinthenextthreesections.
3.2.2.1ToEmployExistingC++StandardLibraryFunctionObjects
Table
5illustratesthefunctionobjectsprovidedbytheC++StandardLibrary.
5:
FunctionobjectsprovidedbytheC++StandardLibrary
Functionobject
Implementedoperations
Arithmeticfunctions
plus
additionx+y
minus
subtractionx-y
multiplies
multiplicationx*y
divides
divisionx/y
modulus
remainderx%y
negate
negation-x
Comparisonfunctions
equal_to
equalitytestx==y
not_equal_to
inequalitytestx!
=y
greater
greater-thancomparisonx>
y
less
less-thancomparisonx<
greater_equal
greaterthanorequalcomparisonx>
less_equal
lessthanorequalcomparisonx<
Logicalfunctions
logical_and
logicalconjunctionx&
&
logical_or
logicaldisjunctionx||y
logical_not
logicalnegation!
x
Let'
slookatacoupleofexamplesthatshowhowthesemightbeused.Thefirstexampleusesstd:
plus<
()tocomputetheby-elementadditionoftwolistsofintegervalues,placingtheresultbackintothefirstlist.Thiscanbeperformedbythefollowingcode:
transform(listOne.begin(),listOne.end(),listTwo.begin(),
listOne.begin(),std:
());
Thesecondexamplenegateseveryelementinavectorofbooleanvalues:
transform(aVec.begin(),aVec.end(),aVec.begin(),
logical_not<
bool>
ThebaseclasstemplatesusedbytheC++StandardLibrarytodefinethefunctionsinTable
5arealsoavailableforcreatingnewunaryandbinaryfunctionobjects.Theclasstemplatesunary_functionandbinary_functionaredefinedintheheader<
functional>
.
Thebaseclassesaredefinedasfollows:
namespacestd{
template<
classArg,classResult>
structunary_function
typedefArgargument_type;
typedefResultresult_type;
classArg1,classArg2,classResult>
structbinary_function
typedefArg1first_argument_type;
typedefArg2second_argument_type;
AnexampleoftheuseofthesetemplatesisfoundinSection
6.3.TherewewanttotakeabinaryfunctionoftypeWidgetandanargumentoftypeint,andcomparethewidgetidentificationnumberagainsttheintegervalue.Afunctiontodothisiswritteninthefollowingmanner:
structWidgetTester:
binary_function<
Widget,int,bool>
booloperator()(constWidget&
wid,inttestid)const
{returnwid.id==testid;
Theimportanceofinheritancefromoneofthetwobaseclasstemplatesbecomesapparentwhenelementaryfunctionobjectsarecomposedtoformcomplexexpressionsusingnegatorsandbinders,whichareexplainedinSection
3.5.
3.2.2.2ToImproveExecution
Asecondreasontoconsiderusingfunctionobjectsinsteadoffunctionsisfastercode.Thedifferencebetweenafunctionandanobjectofaclassthatdefinesthememberoperator()()isthatfunctionsareconvertedtopointerswhenpassedasargumentstootherfunctions,thusincurringtheoverheadofafunctioncalleveniftheyaredeclaredinline.Inmanycasesaninvocationofafunctionobject,asintheexamplesonstd:
transform()inSection
3.2.2.1,canbeexpandedin-line,eliminatingtheoverheadofafunctioncall.
3.2.2.3ToAccessorSetStateInformation
Thethirdmajorreasontouseafunctionobjectinplaceofafunctioniswheneachinvocationofthefunctionmustremembersomestatesetbyearlierinvocations.Anexampleofthisoccursinthecreationofagenerator,tobeusedwiththegenericalgorithmstd:
generate().Ageneratorissimplyafunctionobjectthattakesnoargumentsandreturnsanew(andpossiblydifferent)valueeachtimeitisinvoked.Themostcommonlyusedformofgeneratorisarandomnumbergenerator,butthereareotherusesfortheconcept.Asequencegeneratorsimplyreturnsthevaluesofanincreasingsequenceofnaturalnumbers(1,2,3,4andsoon).WecancallthisobjectiotaGenafterthesimilaroperationintheprogramminglanguageAPL,anddefineitasfollows:
classiotaGen
public:
iotaGen(intstart=0):
current(start){}
intoperator()(){returncurrent++;
private:
intcurrent;
Aniotaobjectmaintainsacurrentvalue,whichcanbesetbytheconstructor,ordefaultstozero.Eachtimethefunction-calloperatorisinvoked,thecurrentvalueisreturned,andalsoincremented.Usingthisobject,thefollowingcallontheC++StandardLibraryfunctionstd:
generate()initializesavectorof20elementswiththevalues1through20:
vector<
aVec(20);
generate(aVec.begin(),aVec.end(),iotaGen
(1));
Amorecomplexexampleofusingafunctionobjectoccursintheradixsortingexampleprogram,whichisgivenasanexampleofusingthelistdatatypeinSection
6.3.Inthisprogramreferencesareinitializedinthefunctionobject,sothatduringthesequenceofinvocationsthefunctionobjectcanaccessandmodifylocalvaluesinthecallingprogram.
IntroductiontoIterators
TheconceptofiteratorsisfundamentaltousingthecontainerclassesandtheassociatedalgorithmsprovidedbytheC++StandardLibrary.Aniteratorisapointer-likeobjectusedtocyclethroughalltheelementsstoredinacontainer.Becausedifferentalgorithmsneedtotraversecontainersinavarietyoffashions,therearedifferentformsofiterators.EachcontainerclassintheC++StandardLibrarycangenerateaniteratorwithfunctionalityappropriatetothestoragetechniqueusedinimplementingthecontainer.ItisthecategoryofiteratorsrequiredasargumentsthatchieflydistinguisheswhichalgorithmsintheC++StandardLibrarycanbeusedwithwhichcontainerclasses.
Justaspointerscanbeusedinavarietyofwaysintraditionalprogramming,iteratorscanbeusedforanumberofdifferentpurposes.Aniteratorcanbeusedtodenoteaspecificvalue,justasapointercanbeusedtoreferenceaspecificmemorylocation.Apairofiteratorscanbeusedt