運行平台:ubuntu 12.04/GCC 4.8.0
第二章:基本內置類型
1.decltype類型指示符
當我們從表達式的類型來推斷要定義的類型時,可以使用decltype()來解析;decltype與auto不同,decltype應用於變量,返回該變量的類型。
string s("Hello World!!!"); // punct_cnt has the same type that s.size returns decltype(s.size()) punct_cnt = 0;
2.轉義序列
\xxx 八進制
\x(xxx) 十六進制
unsigned char c = -1; // assuming 8-bit chars, c has value 255 i = c; // the character with value 255 is an unprintable character // assigns value of c (i.e., 255) to an int std::cout << i << std::endl; // prints 255
unsigned u = 10; int i = -42; std::cout << i + i << std::endl; // prints -84 std::cout << u + i << std::endl; // if 32-bit ints, prints 4294967264
3.getline()的使用
string line; // read input a line at a time until end-of-file while (getline(cin, line)) cout << line << endl;
4.str.size()返回為string::size_type類型,這是標准庫與機器無關的特性之一。
如下面這個例子,輸入大小在0-15之間的數字,求其對應的十六進制符號:
const string hexdigits = "0123456789ABCDEF"; // possible hex digits cout << "Enter a series of numbers between 0 and 15" << " separated by spaces. Hit ENTER when finished: " << endl; string result; // will hold the resulting hexify'd string string::size_type n; // hold numbers from the input while (cin >> n) if (n < hexdigits.size()) // ignore invalid input result += hexdigits[n]; // fetch the indicated hex digit cout << "Your hex number is: " << result << endl;
而與之相關的則是size_t類型,size_t是標准C庫中定義的,應為unsigned int,在64位系統中為 long unsigned int。
#include <cstddef> using std::size_t; constexpr size_t rowCnt = 3, colCnt = 4; int ia[rowCnt][colCnt]; // 12 uninitialized elements // for each row for (size_t i = 0; i != rowCnt; ++i) { // for each column within the row for (size_t j = 0; j != colCnt; ++j) { // assign the element's positional index as its value ia[i][j] = i * colCnt + j; } }
第四章:表達式
1.值的溢出
思考下面運算的結果:
short short_value = 32767; // max value if shorts are 16 bits short_value += 1; // this calculation overflows cout << "short_value: " << short_value << endl;
第六章:函數
1.返回數組指針的函數,這三種表達方式,留意:
int *p = elemPtr(6); // p points to an int int (*arrP)[5] = arrPtr(5); // arrP points to an array of five ints int (&arrR)[5] = arrRef(4); // arrR refers to an array of five ints / two arrays int odd[] = {1,3,5,7,9}; int even[] = {0,2,4,6,8}; // function that returns a pointer to an int in one of these arrays int *elemPtr(int i) { // returns a pointer to the first element in one of these arrays return (i % 2) ? odd : even; } // returns a pointer to an array of five int elements decltype(odd) *arrPtr(int i) { return (i % 2) ? &odd : &even; // returns a pointer to the array } // returns a reference to an array of five int elements int (&arrRef(int i))[5] { return (i % 2) ? odd : even; }
2.含有可變參數的函數
在C11中提供了兩種方法:如果參數類型相同,可以傳遞一個名為initializer_list的標准庫類型;如果類型不同,可以使用可變參數模版,見16章。使用方法如下:
// overloaded version takes only a list of strings void error_msg(initializer_list<string> il) { for (auto beg = il.begin(); beg != il.end(); ++beg) cout << *beg << " " ; cout << endl; } string expected = "description", actual = "some other case"; initializer_list<int> li = {0,1,2,3}; // expected, actual are strings if (expected != actual) error_msg({"functionX", expected, actual}); else error_msg({"functionX", "okay"});
3.函數指針
C11中尾置返回類型,如聲明如下:
// use trailing return type auto getFcn(const string&) -> string::size_type(*)(const string&, const string&);
而函數指針的另外兩種聲明定義,一種是()括號解析,另一種則是借用decltype確定返回類型為函數指針類型,使用如下:
decltype(sumLength) *getFcn(const string &); // direct definition string::size_type (*getFcn(const string&))(const string&, const string&); // define getFcn decltype(sumLength)* getFcn(const string &fetch) { if (fetch == "sum") return sumLength; return largerLength; }
C Primer Plus是名副其實的入門經典,所以當然選這本入門,C與指針是進階類書籍,可以幫你解開入門過程中問題出現最多的c中的模塊——指針,也是c的精義所在的種種疑惑。
C PRIMER 比較適合初學者,
The C Programming Language比較適合鞏固基礎用
www.amazon.cn/...tive=0