您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 招聘面试 > 优秀程序员之路――C++开发经验及技巧大汇总
1.voidmain()intmain()ANSI/ISOintmain(){/*……*/}intmain(intargc,char**argv){/*……*/}2.windowssystem(“PAUSE”);cin.get();3.C++intadd(inta,intb)//{return(a+b);}intmain(){intnRet;_asm{push1;push2;calladd;movnRet,eax;}cout“nRet:”nRetendl;return0;}4.?#includefstream.hintmain(){ifstreamOpenFile(test.txt);charch;while(!OpenFile.eof()){OpenFilech;coutch;}return0;}5.?#includefstream.hintmain(){ofstreamSaveFile(test.txt);SaveFileHelloWorld!;return0;}6.#includeiostream#includestring#includesstreamusingnamespacestd;intmain(){stringstreamstr;inti=10;stri;coutstr.str()endl;system(PAUSE);return0;}7.typedefTypedef#defineTypedefstypedefchar*string_t;#definechar*string_dstring_ts1,s2;string_ds3,s4;s1,s2s3s4#ifdef8.?charch[4]=“test”;CC++charch[5]=“test”;charch[]=“test”;stringch=“test”;9.#includeiostreamusingnamespacestd;intmain(intargc,char**argv){……cout”Myprogramname:”argv[0]endl;……}10.?#includeiostream#includefstreamintmain(){ifstreamfin(“e:\\test.txt”);//\charch;finch;return0;}11.=*thisclassCTest{public:CTest&operator=(constCTest&val){return*this;}……}12.constconstcharconst*pChar;constchar*pChar;pCharinttmp=20;*pChar=tmp;charch=‘a’;char*constpChar=&ch;,inttmp=20;*pChar=tmp;pchar=&tmp;charch=‘a’;constchar*constpChar=&ch;constconst13.C++.const_cast(expression)constintnVal;intnVal2=const_cast(nVal);dynamic_cast(expression)doubledVal;intnVal=dynamic_cast(dVal);reinterpret_cast(expression)char*pChar=‘a’;intnPoint=reinterpret_cast(pChar);static_cast(expression)intnVal;floatfVal=static_cast(nVal);14.C++inlineclassCTest{public:intadd(inta,intb){return(a+b);}}inlinetemplatetypenameTinlineconstT&std::max(constT&a,constT&b){returnab?b:a;}15.newdelete#includeiostreamusingnamespacestd;intmain(){int*pInt=newint[10];//new10intintcount=0;for(;count10;count++)pInt[count]=count;for(count=0;count10;count++)coutpInt[count]endl;delete[]pInt;//return0;}16.#includeiostreamusingnamespacestd;classCTest{public:voidTest();voidTest(inta);voidTest(inta,intb);}voidCTest::Test(){cout”Empty!”endl;}//voidCTest::Test(inta){cout”Oneparamter:”aendl;}voidCTest::Test(inta,intb){cout”Twoparamters:”abendl;}intmain(){CTesttmp;tmp.Test();tmp.Test(1);tmp.test(2,3);return0;}17.#includeiostreamusingnamespacestd;voidconvert(int&a,int&b)//{inttmp=a;a=b;b=tmp;}intmain(){inta,b;a=10;b=20;cout”Original!”endl;//abcout”a:”aendl”b:”bendl;convert(a,b);//abcout”Afterconvert!”endl;cout”a:”aendl”b:”bendl;//abreturn0;}18.#includeiostream#includebitset//bitsetusingnamespacestd;voidTest(intval,intnum=2)//210{if(2==num)//{bitset32bt(val);coutbtendl;}if(10==num)//coutvalendl;}intmain(){Test(128);//Test(128,10);return0;}19.STLvector#includeiostream#includevector//vectorusingnamespacestd;intmain(){vectorintvec;//vectorfor(intcount=0;count10;count++)//vectorvec.push_back(count);for(intcount=0;countvec.size();count++)//vectorcoutvec[count]”\t”;//coutendl;return0;}20.#includeiostreamusingnamespacestd;classCBase{//public:voidTest(){cout”Baseclass”endl;}}classCDerived:publicCbase{//public:voidTest(){cout”Inheritedclass”endl;}//}intmain(){CDerivedherit;herit.Test();//Test((CBase)herit).Test();//Testreturn0;}21.#includeiostreamusingnamespacestd;intmain(){typedefunion_TMP{intage;charsex;}TMP;//TMPuTmp;uTmp.age=23;//agecout”Age:”uTmp.ageendl;uTmp.sex=‘m’;//sexcout”Sex:”uTmp.sexendl;return0;}22.#includeiostreamusingnamespacestd;intmain(){char*pArr=newchar[10];//newcharfor(intk=0;k10;k++)pArr[k]=(65+k);//for(intk=0;k10;k++){cout*pArr;pArr++;//}coutendl;return0;}23.#includeiostream#includebitsetusingnamespacestd;classTmp{private:bitset32bt;//privatepublic:Tmp(){bt=0;}voidSetBitset(intvalue){bt=value;}//btbitset32&GetBitset(){returnbt;}//bt};intmain(){Tmptp;couttp.GetBitset()endl;tp.SetBitset(32);couttp.GetBitset()endl;return0;}24.mainmain_onexit()int#includeiostreamusingnamespacestd;intfunc();//mainintmain(){coutThisisonmainendl;_onexit(func);//funcmainsystem(PAUSE);return0;}intfunc(){coutThisisaftermainendl;system(PAUSE);return0;}25.#includeiostreamusingnamespacestd;templateclassTTadd(Ta,Tb){return(a+b);}intmain(){cout”Addtwoint:2+3=“add(2,3)endl;//intcout”Addtwofloat:1.1+2.2=“add(1.1,2.2)endl;//floatsystem(“PAUSE”);return0;}26.#includeiostream#includedequeusingnamespacestd;intmain(){dequeintque;//dequedequeint::iteratorque_i;//for(intk=0;k10;k++)que.push_back(k+65);//dequeque_i=que.begin();//begindequefor(;que_i!=que.end();que_i++)coutstatic_castchar(*que_i);//dequecoutendl;system(PAUSE);return0;}27.map26ASCII#includemap#includeiostreamusingnamespacestd;intmain(){mapchar,intmp;//mapASCIImapchar,int::iteratormp_i;typedefpairchar,intTag_pair;for(intcount=0;count26;count++)mp.insert(Tag_pair(count+65,count+65));//mapASCII26for(mp_i=mp.begin();mp_i!=mp.end();mp_i++)coutmp_i-first”\t”mp_i-secondendl;//return0;}28.bitset#includebitset#includestring#includeios
本文标题:优秀程序员之路――C++开发经验及技巧大汇总
链接地址:https://www.777doc.com/doc-5237009 .html