设为首页 加入收藏

TOP

5.5 Increment and Decrement Operators (2)
2013-10-07 15:23:45 来源: 作者: 【 】 浏览:69
Tags:5.5 Increment and Decrement Operators

This program uses the postfix version of -- to decrement cnt. We want to assign the value of cnt to the next element in the vector and then decrement cnt before the next iteration. Had the loop used the prefix version, then the decremented value of cnt would be used when creating the elements in ivec and the effect would be to add elements from 9 down to 0.

注意:最后这句是虚拟语气:倘若把代码中的后缀递减改为前缀递减,其效果是把[9..0] 这10 个元素压入ivec。

Combining Dereference and Increment in a Single Expression

The following program, which prints the contents of ivec, represents a very common C++(www.cppentry.com) programming pattern:

  1. vector<int>::iterator iter = ivec.begin();  
  2. // prints 10 9 8 ... 1  
  3. while (iter != ivec.end())  
  4. cout << *iter++ << endl; // iterator postfix increment 

The expression *iter++ is usually very confusing to programmers new to both C++(www.cppentry.com) and C.

The precedence of postfix increment is higher than that of the dereference operator, so *iter++ is equivalent to *(iter++). The subexpression iter++ increments iter and yields a copy of the previous value of iter as its result. Accordingly, the operand of * is a copy of the unincremented value of iter.

This usage relies on the fact that postfix increment returns a copy of its original, unincremented operand. If it returned the incremented value, we’d dereference the incremented value, with disastrous results: The first element of ivec would not get written. Worse, we’d attempt to dereference one too many elements!

ADVICE: BREVITY CAN BE A VIRTUE

Programmers new to C++(www.cppentry.com) who have not previously programmed in a C-based language often have troublewith the terseness of some expressions. In particular, expressions such as *iter++ can be bewildering—at first. Experienced C++(www.cppentry.com) programmers value being concise. They are much more likely to write

  1. cout << *iter++ << endl; 

than the more verbose equivalent

  1. cout << *iter << endl;  
  2. ++iter; 

For programmers new to C++(www.cppentry.com), the second form is clearer because the action of incrementing the iterator and fetching the value to print are kept separate. However, the first version is much more natural to most C++(www.cppentry.com) programmers.

It is worthwhile to study examples of such code until their meanings are immediately clear. Most C++(www.cppentry.com) programs use succinct expressions rather than more verbose equivalents. Therefore, C++(www.cppentry.com) programmers must be comfortable with such usages.

Moreover, once these expressions are familiar, you will find them less error-prone.

C++(www.cppentry.com) 程序员应熟悉这种常见写法。值得花点时间锻炼,使自己一眼就能看懂这种复杂表达式的意思。

EXERCISES SECTION 5.5

Exercise 5.15: Explain the difference between prefix and postfix increment.

Exercise 5.16: Why do you think C++(www.cppentry.com) wasn’t named ++C

Exercise 5.17: What would happen if the while loop that prints the contents of a vector used the prefix increment operator

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇5.12.7 Old-Style Casts 下一篇5.5 Increment and Decrement Ope..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Sphinx : 高性能SQL (2025-12-24 10:18:11)
·Pandas 性能优化 - (2025-12-24 10:18:08)
·MySQL 索引 - 菜鸟教 (2025-12-24 10:18:06)
·Shell 基本运算符 - (2025-12-24 09:52:56)
·Shell 函数 | 菜鸟教 (2025-12-24 09:52:54)