C++ Primer 學習筆記_1_快速入門

來源:互聯網
上載者:User

標籤:c++   c++ primer   快速入門   注釋   編譯   

C++快速入門


--這個月的第一篇部落格,獻給我最愛的C++程式設計語言!


一、編寫簡單的C++程式

[cpp] view plaincopyprint?
  1. //main函數是唯一一個被作業系統顯式調用的函數。  
  2. int main()  
  3. {  
  4.     /* 
  5.     *該返回值作為狀態指示器,需要返回給作業系統! 
  6.     *以echo $?命令可以查看該返回值 
  7.     *通常非零返回值表示有錯誤出現,由作業系統定義 
  8.     */  
  9.     return 0;  
  10. }  
//main函數是唯一一個被作業系統顯式調用的函數。int main(){    /*    *該傳回值作為狀態指示器,需要返回給作業系統!    *以echo $?命令可以查看該傳回值    *通常非零傳回值表示有錯誤出現,由作業系統定義    */    return 0;}

二、初窺輸入/輸出

    1、標準輸入:cin

    2、標準輸出:cout

    3、cerr、clog

【當寫到cout、cerr與clog時,輸出寫至同一視窗!但是利用重新導向可以將這些流與所選擇的檔案聯絡起來】

[cpp] view plaincopyprint?
  1. #include <iostream>  
  2. //iostream庫定義了接受各種內建類型的輸入/輸出操作符版本  
  3.   
  4. int main()  
  5. {  
  6.     /* 
  7.     *當操作符是輸入(>>)、輸出(<<)操作符時, 
  8.     *其結果是左運算元, 
  9.     *也就是說:輸入/輸出操作返回的值是輸入/輸出流本身 
  10.     */  
  11.   
  12.     //將"..."的內容寫入到cout對象中  
  13.     std::cout << "Enter two numbers:" << std::endl;  
  14.     int v1,v2;  
  15.     //從istream運算元讀入資料並儲存在右運算元中  
  16.     std::cin >> v1 >> v2;  
  17.     //endl具有:1、輸出換行 2、重新整理與裝置相關聯的緩衝區 的作用  
  18.     std::cout << "The sum of " << v1 << " and " << v2  
  19.               << " is " << v1 + v2 << std::endl;  
  20.     return 0;  
  21. }  
#include <iostream>//iostream庫定義了接受各種內建類型的輸入/輸出操作符版本int main(){/**當操作符是輸入(>>)、輸出(<<)操作符時,*其結果是左運算元,*也就是說:輸入/輸出操作返回的值是輸入/輸出流本身*///將"..."的內容寫入到cout對象中    std::cout << "Enter two numbers:" << std::endl;    int v1,v2;    //從istream運算元讀入資料並儲存在右運算元中    std::cin >> v1 >> v2;    //endl具有:1、輸出換行 2、重新整理與裝置相關聯的緩衝區 的作用    std::cout << "The sum of " << v1 << " and " << v2              << " is " << v1 + v2 << std::endl;    return 0;}

三、關於注釋

1、注釋用於:

    概括演算法、確認變數的用途、闡明難以理解的程式碼片段


2、規則

    1)多行注釋:使用注釋對/**/,同時,在注釋的每一行以星號開始,指明整個範圍是多行注釋的一部分。

    2)單行注釋:使用//

    3)通常最好是將一個註解區塊放在所解釋代碼的上方!

    4)注釋對不可嵌套


是否正確:

[cpp] view plaincopyprint?
  1. #include <iostream>  
  2. /* 
  3. *comment pairs/* */ cannot nes.  
  4. *"cannot nest" is considered source code.  
  5. *as is the rest of the program  
  6. */  
  7.   
  8. int main()  
  9. {  
  10.     return 0;  
  11. }  
  12.   
  13. 1、std::cout << "/*";          //輸出”/*”  
  14. 2、std::cout << "*/";          //輸出”*/”  
  15. 3、std::cout << /* "*/" */;    //編譯出錯  
#include <iostream>/**comment pairs/* */ cannot nes.*"cannot nest" is considered source code.*as is the rest of the program*/int main(){return 0;}1、std::cout << "/*";//輸出”/*”2、std::cout << "*/";//輸出”*/”3、std::cout << /* "*/" */;//編譯出錯

四、控制結構

1、關於C++程式的縮排和格式:

     一旦你選定了某種風格,就要始終如一地使用他!


2、再談編譯

編譯器可以檢查出的錯誤:

    1)語法錯誤

    2)類型錯誤

    3)聲明錯誤

編譯器無法檢測出的錯誤:

    邏輯錯誤,該類錯誤一般需要單步跟蹤等進行調試,工具如GDB等。


3、讀入未知數目的輸入

    如果定義了變數intval;並執行語句while(std::cin >> val){...},會為判斷條件,先執行輸入操作,如果遇到檔案結束符(end-of-file)或遇到無效的輸入時,如讀取了一個不是整數的值,則istream對象是無效的。處於無效狀態的istream對象將導致條件失敗。


4、從鍵盤輸入檔案結束符

    Windows:Ctrl+z                     UNIX/Linux/MacOS:Ctrl+D【在Ubuntu下有時候需要連摁兩下】


P14.習題

[cpp] view plaincopyprint?
  1. //題1.10  
  2. #include <iostream>  
  3.   
  4. int main()  
  5. {  
  6.     int sum = 0;  
  7.     int val = 50;  
  8.     while (val <= 100)  
  9.     {  
  10.         sum += val;  
  11.         ++ val;  
  12.     }  
  13.     std::cout << sum << std::endl;  
  14.     return 0;  
  15. }  
//題1.10#include <iostream>int main(){int sum = 0;int val = 50;while (val <= 100){sum += val;++ val;}std::cout << sum << std::endl;return 0;}

[cpp] view plaincopyprint?
  1. //題1.11  
  2. #include <iostream>  
  3.   
  4. int main()  
  5. {  
  6.     for (int i = 10;i >= 0; --i)  
  7.     {  
  8.         std::cout << i << std::endl;  
  9.     }  
  10.     return 0;  
  11. }  
//題1.11#include <iostream>int main(){for (int i = 10;i >= 0; --i){std::cout << i << std::endl;}return 0;}

[cpp] view plaincopyprint?
  1. //題1.12編譯並認識下列錯誤  
  2. #include <iostream>  
  3.   
  4. int main(  
  5. {  
  6.     std::cout << "read" << std::endl:  
  7.     std::cout << update << std::endl;  
  8.     std::cout << "write" << std::endl;  
  9.     return 0  
  10. }  

C++ Primer 學習筆記_1_快速入門

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.