亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

請問在C++中的使用比較運算符時,i++與i+1有什么區別?

請問在C++中的使用比較運算符時,i++與i+1有什么區別?

富國滬深 2018-05-20 18:10:44
這是用C++寫的數組線性表的插入函數,其中第二個if條件中,如果用 listSize+1 是沒有問題的,如果用 listSize++ 程序執行是有錯誤的(非編譯錯誤)void insert(int location, elementtype theElement)     {        if(location > arrayLength - 1)            cout<<"List is full."<<endl;        if(location > (listSize+1) || location < 1 )            cout<<"Please enter correct value."<<endl;        else         {            for(int n = listSize; n >= location; n--)                 elements[n++] = elements[n];             elements[location] = theElement;             listSize++;         }     }i++和i+1在比較運算符中有什么區別嗎?
查看完整描述

1 回答

?
縹緲止盈

TA貢獻2041條經驗 獲得超4個贊

The statement:

    if(location > listSize++ || location < 1 )
        cout<<"Please enter correct value."<<endl;

can be considered like

    if(location > listSize || location < 1 )
    {
        ++listSize;
        cout<<"Please enter correct value."<<endl;
    }

From the C++ Standard (5.2.6 Increment and decrement)

1 The value of a postfix ++ expression is the value of its operand. [ Note: the value obtained is a copy of the original value —end note ]...

So, it will change listSize's value(because of ++listSize;), which is not you hope to see.


查看完整回答
反對 回復 2018-07-21
  • 1 回答
  • 0 關注
  • 1112 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號