乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      輸入兩個(gè)整數(shù)a,b,實(shí)現(xiàn)兩個(gè)數(shù)的交換

       融水公子 2018-08-06
      錯(cuò)誤方法一:
      基本上,想到兩個(gè)數(shù)交換
      就會(huì)使用:
       #include<iostream>
      using namespace std;

      int swap(int a,int b){
          int t=a;
          a=b;
          b=t;
      }
      int main(){
          cout<<swap(1,2)<<endl;
      }
      結(jié)果是錯(cuò)的:
      解析: 
       正確方法2:
      采用指針
      #include<iostream>
      using namespace std;
      //因?yàn)樵趕wap函數(shù)里面  x,y 沒有作用域
      //*a *b 就延伸了這個(gè)作用域到了 x,y
      int swap(int *a,int *b){
          int t=*a;//*a指向的就是x 他就是x
          *a=*b;
          *b=t;
      }
      int main(){
          int x,y;
          cout<<"input 2 num:"<<endl;
          cin>>x>>y;
          swap(&x,&y);
          cout<<x<<" "<<y<<endl;
      }
       
       
      陷阱3:
      int swap(int *a,int *b){
      int *t=*a;
      *a=*b;
      *b=*t
      }
      這種寫法是錯(cuò)誤的!
      正確的改寫:
       

      寫法4:
      引用類型:
       

        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多