表明式左值右值,左值右值

#includeiostream#includestring#includestdlib.husingnamespacestd;intmain(void){inta1=49;inta2=0x49;inta3=490049;inta4=0x490049;strings1,s2,s3,s4;s1=a1;s2=a2;s3=a3;s3=a4;cout"a1=49————:"s3endl;cout"a1=0x49————:"s3endl;cout"a1=490049————:"s3endl;cout"a1=0x490049————:"s3endl;//16转2转10转ASCIIinta5=0x0049;strings5;s5=a5;cout"s5=0x5149————:"s5endl;/*strings6="O";inta6=atoi(s6.c_str());couta6endl;*/return0;}

#includeiostreamusingnamespacestd;templatetypenameTintcount(Tx){ ints1=sizeof(x); ints2=sizeof(x[0]); intresult=s1/s2; returnresult;}voidadd(inta[],intb[],intalength,intblength){ int*p; intm=0; intn=0; intflag=0; //在起首分配结果p数COO度多与最长长度保存进位 if(alength=blength卡塔尔(قطر‎{ p=newint[alength+1]{0}; m=blength; n=alength+1; } else{ p=newint[blength+1]{0}; m=alength; n=blength+1; } //同有岗位的加法 for(inti=0;im;i++卡塔尔{ p[i]=(a[i]+b[i]+flag)%10; flag=(a[i]+b[i]+flag卡塔尔(قطر‎/10; } //多余地点的加法 if(alength=blength卡塔尔国{ for(inti=m;ialength;i++卡塔尔(قطر‎{ p[i]=(a[i]+flag)%10; flag=(a[i]+flag)/10; } if(flag!=0){ p[alength]=flag; cout"结果长度:"(alength+1卡塔尔endl; for(inti=0;ialength+1;i++卡塔尔国coutp[i]""; } else{ cout"结果长度:"alengthendl; for(inti=0;ialength;i++State of Qatar coutp[i]""; } } if(blengthalength){ for(inti=m;iblength;i++){ p[i]=(b[i]+flag)%10; flag=(b[i]+flag)/10; } if(flag!=0){ p[blength]=flag; cout"结果长度:"(blength+1State of Qatarendl; for(inti=0;iblength+1;i++State of Qatarcoutp[i]""; } else{ cout"结果长度:"blengthendl; for(inti=0;iblength;i++State of Qatar coutp[i]""; }大整数减法#includeiostreamusingnamespacestd;templatetypenameTintcount(Tx){ ints1=sizeof(x); ints2=sizeof(x[0]); intresult=s1/s2; returnresult;}voidreduce(inta[],intb[],intalength,intblength){ intm=0; intn=0; intlength=0; int*p=newint[alength]{0}; length=alength; m=blength; for(inti=0;iblength;i++){ if(a[i]=b[i]) p[i]=a[i]-b[i]; else{ n=i; //完毕连环借位方法 while(1State of Qatar{ if(a[i+1]!=0){ a[i+1]=a[i+1]-1; break; } else{ a[i+1]=9; i=i+1; } } i=n; p[i]=10-b[i]+a[i]; } } //化解a数组多出来部分赋值难点,并首先老是息灭头为0的标题for(inti=alength-1;i=blength;i--卡塔尔国{ if(a[i]!=0){ p[i]=a[i]; break; } else length--; } for(inti=blength;ilength-1;i++){ p[i]=a[i]; } //第二次深透化解头为0的主题材料现身低贰回原因是率先次在解决头现身难题无非解决的是a数组赋值有0情形。 for(inti=length-1;i=0;i--卡塔尔{ if(p[i]==0卡塔尔 length--; else break; } cout"结果长度:"lengthendl; for(inti=0;ilength;i++卡塔尔(قطر‎ coutp[i]""; delete[]p; deletep; } voidmain(){ intalength=0; intblength=0; int*a; int*b; cout"输入长度alength:"; cinalength; a=newint[alength]{0}; for(inti=0;ialength;i++){ cina[i]; } cout"输入长度blength:"; cinblength; b=newint[blength]{0}; for(inti=0;iblength;i++){ cinb[i]; } reduce(a,b,alength,blength); delete[]a; delete[]b; deletea; deleteb;} } delete[]p; deletep;}voidmain(){ intalength=0; intblength=0; int*a; int*b; cout"输入长度alength:"; cinalength; a=newint[alength]{0}; for(inti=0;ialength;i++){ cina[i]; } cout"输入长度blength:"; cinblength; b=newint[blength]{0}; for(inti=0;iblength;i++){ cinb[i]; } add(a,b,alength,blength); delete[]a; delete[]b; deletea; deleteb;}

7.参考

左值右值 是说明式的本性,该属性称为 value category。按该属性分类,每一个表达式归于下列之一: lvalue left...

6.模板参数类型

  为了比较左值援用和右值引用,一伊始误打误撞,写了那样三个函数

图片 11 template <typename Type> void Swap(Type&& sb1, Type&& sb2) 2 { 3 Type sb(sb1); 4 sb1 = sb2; 5 sb2 = sb; 6 } View Code

  然后

图片 21 int main() 2 { 3 int a=1, b=2; 4 Swap(a, b); 5 std::cout<<a<<" "<<b<<std::endl; 6 return 0; 7 } View Code

  结果却是

2 2

  不用整数,换用三个自定义的门类试试看:

图片 3 1 class A 2 { 3 public: 4 A() { 5 std::cout << "Default constructor." << std::endl; 6 m_p = NULL; 7 } 8 9 ~A() { 10 std::cout << "Destructor." << std::endl; 11 delete m_p; 12 } 13 14 explicit A(const int n) { 15 std::cout << "Unary constructor." << std::endl; 16 m_p = new int(n); 17 } 18 19 A(const A& other) { 20 std::cout << "Copy constructor." << std::endl; 21 if (other.m_p) { 22 m_p = new int(*other.m_p); 23 } else { 24 m_p = NULL; 25 } 26 } 27 28 A(A&& other) { 29 std::cout << "Move constructor." << std::endl; 30 m_p = other.m_p; 31 other.m_p = NULL; 32 } 33 34 A& operator=(const A& other) { 35 std::cout << "Copy assignment operator." << std::endl; 36 if (this != &other) { 37 delete m_p; 38 if (other.m_p) { 39 m_p = new int(*other.m_p); 40 } else { 41 m_p = NULL; 42 } 43 } 44 return *this; 45 } 46 47 A& operator=(A&& other) { 48 std::cout << "Move assignment operator." << std::endl; 49 if (this != &other) { 50 delete m_p; 51 m_p = other.m_p; 52 other.m_p = NULL; 53 } 54 return *this; 55 } 56 57 int get() const { 58 return m_p ? *m_p : 0; 59 } 60 61 private: 62 int * m_p; 63 }; 64 65 int main() 66 { 67 A a(1); 68 A b(2); 69 Swap2(a, b); 70 std::cout<<a.get()<<" "<<b.get()<<std::endl; 71 return 0; 72 } View Code

  结果

Unary constructor.
Unary constructor.
Copy assignment operator.
Copy assignment operator.
2 2
Destructor.
Destructor.

  只现出了七个对象,那么Swap中的临时对象去哪儿了?

C++0x 14.8.2.1

If P is a cv-qualified type, the top level cv-qualifiers of P’s type are ignored for type deduction. If P is a reference type, the type referred to by P is used for type deduction. If P is an rvalue reference to a cv unqualified template parameter and the argument is an lvalue, the type “lvalue reference to A” is used in place of A for type deduction. 

图片 41 template <class T> int f(T&&); 2 template <class T> int g(const T&&); 3 int i; 4 int n1 = f(i); // calls f<int&>(int&) 5 int n2 = f(0); // calls f<int>(int&&) 6 int n3 = g(i); // error: would call g<int>(const int&&), which 7 // would bind an rvalue reference to an lvalue View Code

 也就是前面提到的

1 template <typename Type> void Swap(Type&& sb1, Type&& sb2)

  参数推导后

1 void Swap<int&>(int& sb1, int& sb1)