esential c++ 學習筆記1--c++ 編程基礎.txt

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

          ??? }
          8指針 指針為程序引入了一層間接性,我們可以操作指針(代表某特定內存地址),而不再直接操控對象。
          ?指針主要形成兩件事,可以增加程序本身的彈性,但同時也增加了直接操控對象時所沒有的復雜度
          ?1 int ival=1024
          ?? int *p=&ival; 其中*p 指int型對象的地址
          ?2 指針所具有的雙重性,既可以讓我們操控指針內含的內存地址,也可以讓我們操作指針所指定的對象值
          ?? pi? 指定pi所含有的內存地址
          ? *pi? 核定ival的值
          ?3 指針的提領(dereference)
          ? 如果pi 尋址到某個對象,則執行提領操作,如果pi 不指定任何對象,提領會導致未知的執行結果
          ? 一個為只想任何對象的指針,其內含地址為0,我們稱為null,任何指針都可以被初始話,或是令值為0
          ? if(pi&&...)
          ? 只有pi含一個非0值時,其結果為true
          ? vector<int> *pv=0;?
          ? const int seq_cnt=6;
          ? vector<int> *seq_addres[seq_cnt]={
          ??? &fibonacci,&lucas,&pell...
          ? };
          ? 一個指針數組,容量為seq_cnt,每個指針都指向vector<int>??
          ? 4 #include<cstdlib>
          ? rand(seed) 返回一個介于0和seed 之間的隨機數
          ? 5 對象指針
          ? if(!fibonacci.empty()&&....){
          ???? pv.empty()..
          ?? }
          9 文件寫
          ? 對文件的讀寫,首先的含入fstream
          ? #include<fstream>
          ? 1 ofstream outfile("seq_data.txt"); 如果文件不存在,產生一個文件,如果文件已經存在,這個文件
          ? 被開啟作為輸出只用,但是源文件中的數據會輸調
          ? 2 ofstream outfile("seq_data.txt",ios_base::app) 追加模式
          ? 3 oufile 為false 表示文件未開啟成功
          10 文件讀
          ? ifstream 將文件名傳人,如果文件未能開啟成功,ifstream 對象被核定為false ,如果成功,為true
          ? ifstream infile("seq_data.txt");
          ? int num_tries=0;
          ? int num_cor=0;
          ? if(!infile){
          ??? //由于某種原因,文件無法開啟
          ? }
          ? 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 同時讀寫同一個文件
          ? 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 康文 閱讀(717) 評論(0)  編輯  收藏 所屬分類: c\c++

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

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 叙永县| 南京市| 禄劝| 镇巴县| 辰溪县| 东山县| 宝鸡市| 策勒县| 桐梓县| 陆河县| 潍坊市| 寿宁县| 荃湾区| 綦江县| 公安县| 阿坝| 安溪县| 临西县| 安阳县| 杭州市| 翼城县| 祁阳县| 承德市| 济宁市| 四子王旗| 汝南县| 裕民县| 辽阳县| 文水县| 临安市| 建德市| 连城县| 黄梅县| 泸溪县| 阳春市| 海门市| 五常市| 横山县| 隆回县| 咸丰县| 平山县|