標籤:
相關的c++課程聽了不少,但聽課不能代替看書,書籍系統的總結對於形成體系,查漏補缺還是有很大作用的。
市面上c++書籍多如牛毛,公認好評的無非《c++ primer》,《The c++ programming language》等全面介紹語言特性的書籍和 《effective c++》 《more effective c++》等專家建議類書籍。為了對應自己進行中的c++系列課程的學習,需要短時間內對c++有一個系統的梳理,即採用2-8原則,以較短的時間,掌握最為常用的那部分。顯然上述最上所述兩個大部頭,不是當下的首選。所以另一本市面已不多見,但也曾公認好評的入門書,《accelerated c++》可能就是最好的選擇(像書中所說,如果你想使用c++編程,就需要掌握這本書中的所有知識,儘管它沒有告訴你想知道的一切)。在接下來假期這段時間內,自己需要仔細閱讀並盡量多地完成書中的習題,在此開始總結帖,希望能把收穫進行梳理沉澱。
然而,正如侯捷老師所說,“作為一個好的學習者,重要的事你的學習態度,起步固然可以從輕鬆小品開始,但如果碰上大部頭就退避三舍,逃之夭夭,面對任何技術只求快餐速成,學語言而從來不寫程式,那就永遠沒有成為高手的一天。” 與自己共勉,開學後開讀《c++ primer》...
第0章 入門
對hello world程式的講解梳理,自然不會有什麼新的東西,相關概念的複習吧。
第一章 使用字串
本章程式:
// ask for a person‘s name, and generate a framed greeting#include <iostream>#include <string>int main(){ std::cout << "Please enter your first name: "; std::string name; std::cin >> name; // build the message that we intend to write const std::string greeting = "Hello, " + name + "!"; // build the second and fourth lines of the output const std::string spaces(greeting.size(), ‘ ‘); const std::string second = "* " + spaces + " *"; // build the first and fifth lines of the output const std::string first(second.size(), ‘*‘); // write it all std::cout << std::endl; std::cout << first << std::endl; std::cout << second << std::endl; std::cout << "* " << greeting << " *" << std::endl; std::cout << second << std::endl; std::cout << first << std::endl; return 0;}
1. wchar_t 用來存放“寬字元”的內建類型,寬字元是一種足夠大的類型,可以為多種語言儲存字元。
2. string 類 有一種
string z(n,c)的操作,即定義string類型變數z,並且用字元c的n份複製來初始化z。 注意: c必須為char類型,不能使string類型或字串常量
3. string類加號 s+t 中, s或者t中任意一個都可以是(但不可以兩個都是)字串直接量或者char類型
所以
std::string exclaim = "!";
std::string s = "Hello" + ", World" + exclaim; //錯誤,+左結合,出現了兩個字串常量相加
第二章 迴圈和計數
本章介紹了while,if,for等語句的用法,設計while語句時的迴圈不變式的概念,以及算數,邏輯操作符和他們的優先順序,新東西不多,看程式和注釋吧
#include <iostream>#include <string>// say what standard-library names we useusing std::cin; using std::endl;using std::cout; using std::string;int main(){ // ask for the person‘s name cout << "Please enter your first name: "; // read the name string name; cin >> name; // build the message that we intend to write const string greeting = "Hello, " + name + "!"; // the number of blanks surrounding the greeting const int pad = 0; // the number of rows and columns to write const int rows = pad * 2 + 3; const string::size_type cols = greeting.size() + pad * 2 + 2; //當定義一個變數來儲存特定資料結構的大小時, //養成使用標準庫定義的相應類型的好習慣 . //size_t 不帶正負號的整數類型(來自標頭檔<cstddef>),可包含任意對象長度 //string::size_type 不帶正負號的整數類型,包含任意string對象長度 // write a blank line to separate the output from the input cout << endl; // write `rows‘ rows of output // invariant: we have written `r‘ rows so far for (int r = 0; r != rows; ++r) { string::size_type c = 0; // invariant: we have written `c‘ characters so far in the current row while (c != cols) { // is it time to write the greeting? if (r == pad + 1 && c == pad + 1) { cout << greeting; c += greeting.size(); } else { // are we on the border? if (r == 0 || r == rows - 1 || c == 0 || c == cols - 1) cout << "*"; else cout << " "; ++c; } } cout << endl; } return 0;}
第三章 使用批量資料
主要是vector的定義和相關操作,以及流輸出控制符的介紹。
1 標頭檔<ios>定義了streamsize 類型,這是輸入輸出庫用來表示長度的類型;
標頭檔<iomanip>定義了控制符setprecision,用來說明希望輸出中包含多少位有效數字
2 vector
size_type 無符號類型,足夠儲存最大的vector大小;
v.begin(), v.end(), v.push_back, v.size(),
3 其他庫函數
sort(b,e), max(e1,e2), while(cin>>x),
s.precision(n) 設定流s以後的輸出精度為n,(省略n保持原有的輸出精度)返回原有的輸出精度
setprecision(n)
#include <algorithm>#include <iomanip>#ifndef __GNUC__#include <ios>#endif#include <iostream>#include <string>#include <vector>using std::cin; using std::sort;using std::cout; using std::streamsize;using std::endl; using std::string;using std::setprecision; using std::vector;int main(){ // ask for and read the student‘s name cout << "Please enter your first name: "; string name; cin >> name; cout << "Hello, " << name << "!" << endl; // ask for and read the midterm and final grades cout << "Please enter your midterm and final exam grades: "; double midterm, final; cin >> midterm >> final; // ask for and read the homework grades cout << "Enter all your homework grades, " "followed by end-of-file: "; vector<double> homework; double x; // invariant: `homework‘ contains all the homework grades read so far while (cin >> x) homework.push_back(x); // check that the student entered some homework grades#ifdef _MSC_VER typedef std::vector<double>::size_type vec_sz;#else typedef vector<double>::size_type vec_sz;#endif vec_sz size = homework.size(); if (size == 0) { cout << endl << "You must enter your grades. " "Please try again." << endl; return 1; } // sort the grades sort(homework.begin(), homework.end()); // compute the median homework grade vec_sz mid = size/2; double median; median = size % 2 == 0 ? (homework[mid] + homework[mid-1]) / 2 : homework[mid]; // compute and write the final grade streamsize prec = cout.precision(); cout << "Your final grade is " << setprecision(3) << 0.2 * midterm + 0.4 * final + 0.4 * median << setprecision(prec) << endl; return 0;}
Accelerated c++ 讀書筆記1