zoukankan      html  css  js  c++  java
  • C++之形参

      1 #include <iostream>
      2 #include <new>
      3 #include <bitset>
      4 #include <string>
      5 #include <stdexcept>
      6 
      7 
      8 using  namespace std;
      9 
     10 
     11 
     12 //非引用形参 传递参数时是拷贝 不会改变变量的原始值
     13 void AddOne(int x)
     14 {
     15     x=x+1;
     16 }
     17 void AddTwo(int *px)
     18 {
     19     *px=*px+2;
     20 }
     21 //引用形参
     22 void AddThree(int& x)
     23 {
     24     x=x+3;
     25 }
     26 int add(int x,int y)
     27 {
     28     return x+y;
     29 }
     30 int AddFour(const int x)
     31 {
     32     //x=x+1; //const值传进来不能改
     33 }
     34 void addSix(int *ip)
     35 {
     36     //指针情况下 const不能传递给非const
     37     *ip+=1;
     38 }
     39 void addSeven(const int *ip)
     40 {
     41     //*ip+=2; //指针指向的是常量 不能进行修改 编译会出错
     42 }
     43 
     44 int doOp(int x,int y,int &z)
     45 {
     46     int result;
     47     result=x+y;
     48     z=x-y;
     49     return result;
     50 }
     51 
     52 void doOp1(int x,int y,int &z)
     53 {
     54     z=x-y;
     55 }
     56 
     57 void preswap(int *&v1,int *&v2)
     58 {
     59     int *temp=v2;
     60     v2=v1;
     61     v1=temp;
     62 }
     63 int main()
     64 {
     65     //函数参数传递
     66     #ifdef NDEBUG
     67     int a=1;
     68     int c=3;
     69     int b=2;
     70     cout<<a<<endl;
     71     AddOne(a);
     72     cout<<a<<endl;
     73     //引用形参 传递的是真正的C并且c也会改变
     74     cout<<c<<endl;
     75     AddThree(c);
     76     cout<<c<<endl;
     77     //指针参数 也是非引用形参
     78     cout<<b<<endl;
     79     AddTwo(&b);
     80     cout<<b<<endl;
     81     #endif // NDEBUG
     82     #ifdef NDUBGE
     83     int a=1;
     84     int b=2;
     85     int k;
     86     const int x=8;
     87     const int y=9;
     88     k=add(a,b);
     89     cout<<k<<endl;
     90     //const值传进来不能改
     91     AddFour(x);
     92     #endif // NDUBGE
     93 
     94     int a=10;
     95     int b=8;
     96     int z;
     97     int result;
     98     result=doOp(a,b,z);
     99     cout<<result<<","<<z<<endl;
    100     doOp1(a,b,z);
    101     cout<<z<<endl;
    102     int i=10;
    103     int j=20;
    104     int *p1=&i;
    105     int *p2=&j;
    106     preswap(p1, p2);
    107     cout<<*p1<<*p2<<endl;
    108 
    109     return 0;
    110     //预处理进行调试
    111     #ifdef NDEBUG
    112     cout<<""<<endl;
    113     #endif // NDEBUG
    114 }
  • 相关阅读:
    hdu 刷题记录
    HDU step by step
    Codeforces Round #260 (Div. 2) B. Fedya and Maths
    Codeforces Round #260 (Div. 2) A. Laptops
    UVALive 6662 TheLastAnt
    UVALive 6661 Equal Sum Sets
    Codeforces Round #253 (Div. 2) A. Anton and Letters
    wikioi 3130 CYD刷题(背包)
    wikioi 1014 装箱问题(背包)
    [转]很特别的一个动态规划入门教程
  • 原文地址:https://www.cnblogs.com/yh2924/p/12579356.html
Copyright © 2011-2022 走看看