esential c++ 學(xué)習(xí)筆記1--c++ 編程基礎(chǔ).txt

          1 class 的定義,一般來(lái)說(shuō)分為兩部分,其中一個(gè)是所謂的頭文件,用來(lái)聲明class 所提供的各種操作行為
          ? 另一個(gè)是文件,程序代碼文件,用來(lái)包含這些行為的實(shí)現(xiàn)內(nèi)容。預(yù)使用class 不許在程序中含入其頭文件
          2 using namespace std
          3 template class 機(jī)制使程序員直到使用template class 時(shí)才決定真正的數(shù)據(jù)類別。先使用一個(gè)代名,
          ? 稍后才綁定至實(shí)際的數(shù)據(jù)類別
          4 Arrays 要定義array 我們必須指定array 的元素類型,名稱,并指定其尺度的大小
          ? array 的尺度必須是個(gè)常量表達(dá)式
          ? const int seq_size=18;
          ? int pell_seql seq_size=1;
          5 vector 必須首先含如vector 的頭文件。在角括號(hào)中指定其元素類型,其尺度則寫(xiě)作小括號(hào)內(nèi),不一定
          ? 是常量表達(dá)式
          ? #include<vector>
          ? vector<int> pell_seq(seq_size);
          6 初始化數(shù)組和vector
          ? 1 初始化數(shù)組
          ?? int elem_seq[seq_size]={1,2,3,4} ;
          ?? int elem_swq[]={1,2,3,4};由編譯其根據(jù)初始值自動(dòng)算出array 的值
          ? 2 初始化vector
          ?? 1) vector<int> elem_seq(seq_size);
          ????? elem_seq[0]=1;
          ????? elem_seq[1]=2;
          ????? .....
          ????? elem_seq[[17]==22;
          ??? 2) 利用一個(gè)以初始化的array
          ???? int elem_val[seq_size]={1,2,3,4}
          ???? vector<int>elem_seq(elem_val,elem_val+seq_size); 其中elem_val 為內(nèi)存地址
          7 array 和 vector 的使用
          ?? vector 知道自己的大小,而array 不知道
          ?? for(int i=0;i<elem_seq.size();i++){
          ????? cout<<elem_seq[[i]<<'';

          ??? }
          8指針 指針為程序引入了一層間接性,我們可以操作指針(代表某特定內(nèi)存地址),而不再直接操控對(duì)象。
          ?指針主要形成兩件事,可以增加程序本身的彈性,但同時(shí)也增加了直接操控對(duì)象時(shí)所沒(méi)有的復(fù)雜度
          ?1 int ival=1024
          ?? int *p=&ival; 其中*p 指int型對(duì)象的地址
          ?2 指針?biāo)哂械碾p重性,既可以讓我們操控指針內(nèi)含的內(nèi)存地址,也可以讓我們操作指針?biāo)付ǖ膶?duì)象值
          ?? pi? 指定pi所含有的內(nèi)存地址
          ? *pi? 核定ival的值
          ?3 指針的提領(lǐng)(dereference)
          ? 如果pi 尋址到某個(gè)對(duì)象,則執(zhí)行提領(lǐng)操作,如果pi 不指定任何對(duì)象,提領(lǐng)會(huì)導(dǎo)致未知的執(zhí)行結(jié)果
          ? 一個(gè)為只想任何對(duì)象的指針,其內(nèi)含地址為0,我們稱為null,任何指針都可以被初始話,或是令值為0
          ? if(pi&&...)
          ? 只有pi含一個(gè)非0值時(shí),其結(jié)果為true
          ? vector<int> *pv=0;?
          ? const int seq_cnt=6;
          ? vector<int> *seq_addres[seq_cnt]={
          ??? &fibonacci,&lucas,&pell...
          ? };
          ? 一個(gè)指針數(shù)組,容量為seq_cnt,每個(gè)指針都指向vector<int>??
          ? 4 #include<cstdlib>
          ? rand(seed) 返回一個(gè)介于0和seed 之間的隨機(jī)數(shù)
          ? 5 對(duì)象指針
          ? if(!fibonacci.empty()&&....){
          ???? pv.empty()..
          ?? }
          9 文件寫(xiě)
          ? 對(duì)文件的讀寫(xiě),首先的含入fstream
          ? #include<fstream>
          ? 1 ofstream outfile("seq_data.txt"); 如果文件不存在,產(chǎn)生一個(gè)文件,如果文件已經(jīng)存在,這個(gè)文件
          ? 被開(kāi)啟作為輸出只用,但是源文件中的數(shù)據(jù)會(huì)輸調(diào)
          ? 2 ofstream outfile("seq_data.txt",ios_base::app) 追加模式
          ? 3 oufile 為false 表示文件未開(kāi)啟成功
          10 文件讀
          ? ifstream 將文件名傳人,如果文件未能開(kāi)啟成功,ifstream 對(duì)象被核定為false ,如果成功,為true
          ? ifstream infile("seq_data.txt");
          ? int num_tries=0;
          ? int num_cor=0;
          ? if(!infile){
          ??? //由于某種原因,文件無(wú)法開(kāi)啟
          ? }
          ? else
          ? {
          ?? string name;
          ?? int nt;
          ?? int nc;
          ?? while(infile>>name)
          ?? {
          ???? // 一旦讀到到文件尾,infile 尾false
          ???? // infile>>name>>nt>>nc? ,把文件 anna 24 19 分別讀到name,nt,nc 中
          ???? infile>>nt>>nc;
          ???? if(name==usr_name)
          ???? {
          ?????? //find hime
          ?????? count<<"Welcome back,"<<usr_name
          ??????????? <<"\nYour current score is" <<nc
          ??????????? <<" out of " <<nt<<"\nGood Luck"!\n";
          ??????????? num_tries=nt;
          ??????????? num_cor=nc;???
          ???? }
          ?? }
          11 同時(shí)讀寫(xiě)同一個(gè)文件
          ? fstream iofile("seq_data.txt",ios_base::in|ios_base::app);
          ? if(!iofile)
          ?? ...
          ? else
          ? {
          ?? iofile.seekg(0); 將文件重新定位的文件的最末端
          ? }
          12
          ? #include<iostream>
          #include<string>
          using namespace std;
          //---------------------------------------------------------------------------

          #pragma argsused
          int main()
          {
          ?? string username;
          ?? cout<<"Please enter your name:";
          ?? cin>>username;
          ?? switch(username.size()){
          ???? case 0:
          ?????? cout<<"with no name";
          ?????? break;
          ???? case 1:
          ?????? cout<<"with one character";
          ?????? break;
          ???? default:
          ?????? cout<<"hollo ,"<<username<<endl;
          ?????? break;
          ?? }
          ??????? return 0;
          }
          13
          #include<iostream>
          #include<vector>
          #include<string>
          using namespace std;
          //---------------------------------------------------------------------------
          int main()
          {
          ? vector<int> ivec;
          ? string str;
          ? int val;
          ? while(cin>>val){
          ?????? ivec.push_back(val);
          ? }
          ? int sum=0;
          ? for(int i=0;i<ivec.size();i++){
          ????? sum+=ivec[i];
          ? }
          ? int average=sum/ivec.size();
          ? cout<<"sum of "<<ivec.size()
          ????? <<"elements "<<sum
          ????? <<"average "<<average<<endl;
          ??????? return 0;
          }
          14
          //---------------------------------------------------------------------------

          #include<iostream>
          #include<vector>
          #include<string>
          using namespace std;
          //---------------------------------------------------------------------------
          int main()
          {
          ?const int array_size=120;
          ?int la[array_size];
          ?int ival,icnt=0;

          ?while(cin>>ival&&icnt<array_size){
          ?? la[icnt++]=ival;
          ?}
          ?int sum=0;
          ?for(int i=0;i<array_size;i++){
          ?? sum+=la[i];
          ?}
          ?int average=sum/array_size;
          ?cout<<array_size
          ???? <<"\n"<<sum
          ???? <<"\n"<<average<<endl;
          }
          //---------------------------------------------------------------------------


          //---------------------------------------------------------------------------

          #include<iostream>
          #include<vector>
          #include<string>
          #include<fstream>
          #include<algorithm>
          using namespace std;
          //---------------------------------------------------------------------------
          int main()
          {
          ? ifstream in_file ("D:\inputfile.txt");
          ? ofstream out_file("D:\outputfile.txt");
          ? if(!in_file){
          ???? cerr<<"unable to open the inputfile" ;
          ? }
          ? if(! out_file){
          ???? cerr<<"unable to open the outputfile" ;
          ? }
          ? string word;
          ? vector<string> text;
          ? while(in_file>>word)
          ? {
          ???? text.push_back(word);
          ? }
          ? cout<<"unsort file";
          ? for(int i=0;i<text.size();++i)
          ? {
          ? cout<<text[i]<<" "<<endl;
          ? }
          ? cout<<"sort file";
          ? sort(text.begin(),text.end());// sort the vector
          ? for(int i=0;i<text.size();++i)
          ? {
          ? out_file<<text[i]<<" "<<endl;
          ? }
          }

          ?

          posted on 2006-08-14 09:27 康文 閱讀(716) 評(píng)論(0)  編輯  收藏 所屬分類: c\c++

          <2006年8月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 申扎县| 寿阳县| 济阳县| 东至县| 东乌珠穆沁旗| 武平县| 徐水县| 灵石县| 金昌市| 昂仁县| 鲁山县| 建湖县| 巴塘县| 赣州市| 华亭县| 阳新县| 保亭| 个旧市| 安庆市| 黔东| 贵溪市| 辛集市| 漳州市| 青海省| 家居| 新兴县| 武威市| 凉城县| 宜春市| 叙永县| 蓬溪县| 温宿县| 马公市| 运城市| 沁源县| 共和县| 丘北县| 将乐县| 肥西县| 镇原县| 年辖:市辖区|