PS,1880后程序員

          看不完的牙,寫不完的程序,跑不完的步。
          隨筆 - 97, 文章 - 34, 評論 - 10, 引用 - 0
          數(shù)據(jù)加載中……

          C++ Primer 之 讀書筆記 第五章

           

          5.3 位操作符(The Bitwise Operators)

          盡量不要使用符號(hào)數(shù)進(jìn)行位運(yùn)算,因?yàn)榉?hào)位的處理和機(jī)器有關(guān)。

          XOR的描述真是簡單明了,For each bit position, the result is 1 if either but not both operands contain 1; Otherwise, the result is 0.

          輸入輸出標(biāo)準(zhǔn)庫重載了移位操作符>><<用于輸入和輸出。

          復(fù)合表達(dá)式的求值(Evaluating Compound Expressions

          這里面有兩個(gè)關(guān)鍵詞:

          l         Precedence

          Precedence specifies how the operands are grouped.優(yōu)先級規(guī)定的是操作數(shù)的結(jié)合方式

          l         associativity

          Associativity specifies how to group operators at the same precedence level.結(jié)合性規(guī)定了具有相同優(yōu)先級的操作符如何分組。

          例如:

          賦值操作符是右關(guān)聯(lián)

           ival = jval = kval = lval       // right associative

           (ival = (jval = (kval = lval))) // equivalent, parenthesized version

          算數(shù)操作符是左關(guān)聯(lián)

          ival * jval / kval * lval       // left associative

          (((ival * jval) / kval) * lval) // equivalent, parenthesized version

          5.4 賦值操作符(Assignment Operators

          讓你不得不佩服,Lippman這樣的大師僅僅賦值操作符(Assignment Operators)就可以講出這么多的內(nèi)容。

          l         賦值運(yùn)算是右關(guān)聯(lián),它的返回值是左操作數(shù);類型為左操作數(shù)的類型(The result of an assignment is the left-hand operand, the type of the result is the type of the left-hand operand.)。

          這樣就好理解這樣的賦值語句了:

          先執(zhí)行jval = 0;返回值是jval ,再執(zhí)行ival = jval

          int ival, jval;

          ival = jval = 0; // ok: each assigned 0

          l         賦值操作的優(yōu)先級低。

          下面的條件語句返回值是true,因?yàn)橄葓?zhí)行賦值語句,返回值是ii大于0,因此條件語句判斷為true

          if (i = 42)

          下面的條件語句返回值是false,因?yàn)橄葓?zhí)行賦值語句,返回值是ii等于0,因此條件語句判斷為false

          if (i = 0)

          5.5 Increment and Decrement Operators自增和自減操作符

          l         重點(diǎn)

          前置和后置操作符

          l         建議

          只在必要的時(shí)候才使用后置操作符。

          l         區(qū)別

          前置和后置操作符的返回值是不同的,這是最重要的區(qū)別,也是理解的基礎(chǔ)。

          前置操作符:值加1,再返回。(It increments the value and returns the incremented version.

          后置操作符:必須保留原始值,(原始值加1)返回的結(jié)果是未增加的值。(The postfix operator must store the original value so that it can return the unincremented value as its result.

          l         代碼

          cnt—的返回值是cnt

          vector<int> ivec;           // empty vector

          int cnt = 10;

          // add elements 10...1 to ivec

          while (cnt > 0)

              ivec.push_back(cnt--); // int postfix decrement

          *iter++就是*(iter++)++操作的優(yōu)先級高于*,所以先執(zhí)行iter++,這個(gè)操作的返回值是iter,然后iter+1.再執(zhí)行解引用操作*,這個(gè)解引用操作的操作數(shù)是返回的iter

          vector<int>::iterator iter = ivec.begin();

          // prints 10 9 8 ... 1

          while (iter != ivec.end())

              cout << *iter++ << endl; // iterator postfix increment

          Sizeof

          ?如果是返回對象的size,那么sizeof的返回值是不是和數(shù)據(jù)有關(guān)?

          賦值順序

          l         && || 操作符計(jì)算其操作數(shù)的次序:當(dāng)且僅當(dāng)其右操作數(shù)確實(shí)影響了整個(gè)表達(dá)式的值時(shí),才計(jì)算這兩個(gè)操作符的右操作數(shù)。

          && and || operators specify the order in which their operands are evaluated: In both cases the right-hand operand is evaluated if and only if doing so might affect the truth value of the overall expression.

          (這句話說的真是嚴(yán)謹(jǐn)。)

          l         一個(gè)表達(dá)式里,不要在兩個(gè)或更多的子表達(dá)式中對同一對象做自增或自減操作。

          Do not use an increment or decrement operator on the same object in more than two subexpressions of the same expression.

          5.11 new delete 表達(dá)式(The new and delete Expressions

          new表達(dá)式返回的是一個(gè)指針,這個(gè)指針指向新分配的一個(gè)對象。

          int *pi = new int; // pi points to dynamically allocated,

          delete pi;

          但是這樣寫就不是動(dòng)態(tài)分配的指針

          int i;

          int *pi = &i;

          delete pi; //因?yàn)?/span>pi指向的是一個(gè)local變量

          后面的寫法是不能執(zhí)行delete表達(dá)式的。

          new動(dòng)態(tài)創(chuàng)建的對象一定要執(zhí)行delete來刪除。否則內(nèi)存就會(huì)被耗盡。

          大師給了以下的建議:

          l         Setting the pointer to 0 after the object it refers to has been deleted makes it clear that the pointer points to no object.(一旦刪除了指針?biāo)赶虻膶ο螅⒓磳⒅羔樦脼?/span> 0,這樣就非常清楚地表明指針不再指向任何對象。)

          l         Reading or writing to the object after it has been deleted. This error can sometimes be detected by setting the pointer to 0 after deleting the object to which the pointer had pointed.(讀寫已刪除的對象。如果刪除指針?biāo)赶虻膶ο笾螅瑢⒅羔樦脼?/span> 0 值,則比較容易檢測出這類錯(cuò)誤。)

          l         Applying a delete expression to the same memory location twice. This error can happen when two pointers address the same dynamically allocated object. If delete is applied to one of the pointers, then the object's memory is returned to the free store. If we subsequently delete the second pointer, then the free store may be corrupted.(對同一個(gè)內(nèi)存空間使用兩次 delete 表達(dá)式。當(dāng)兩個(gè)指針指向同一個(gè)動(dòng)態(tài)創(chuàng)建的對象,刪除時(shí)就會(huì)發(fā)生錯(cuò)誤。如果在其中一個(gè)指針上做 delete 運(yùn)算,將該對象的內(nèi)存空間返還給自由存儲(chǔ)區(qū),然后接著 delete 第二個(gè)指針,此時(shí)則自由存儲(chǔ)區(qū)可能會(huì)被破壞。)

          posted on 2009-05-20 14:19 amenglai 閱讀(306) 評論(0)  編輯  收藏 所屬分類: C++ Primer 之 讀書筆記

          主站蜘蛛池模板: 广水市| 青铜峡市| 彩票| 交城县| 万山特区| 武鸣县| 岑巩县| 正蓝旗| 大悟县| 和平县| 元谋县| 耒阳市| 华宁县| 璧山县| 呼伦贝尔市| 体育| 南华县| 陇川县| 罗山县| 海门市| 金平| 昭觉县| 桃源县| 大丰市| 太白县| 沁阳市| 阳曲县| 基隆市| 江川县| 高阳县| 沂水县| 仁布县| 沈阳市| 临湘市| 永德县| 贡嘎县| 长子县| 东山县| 桑植县| 光泽县| 佳木斯市|