reference Advanced, reference advanced add
#include <iostream> #include <stdlib.h>//int a[10]//Int (&ra) [10]//int a[2][5]//int (&ra) [2][5]vo] ID main1 () {int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9,};int (&ra) [ten] (a);//reference is to give the original variable an alias with the same address int i = 0;for (Auto da Ta:ra)//c++11 Loop {data = i + 5;std::cout << data << Std::endl;} Std::cout << a << ra << std::endl;std::cout << &a << &ra << std::endl;system (" Pause ");} void Main2 () {int a[2][5] = {1, 2, 3, 4, 5, 6, 7, 8, 9,};int (&ra) [2][5] (a);//The reference is to give the original variable an alias with the same address for (int i = 0; i < 2; i++) {for (int j = 0; J < 5; J + +) {std::cout << "" << Ra[i][j];} Std::cout << "\ n";} Std::cout << a << ra << std::endl;std::cout << &a << &ra << std::endl;system (" Pause ");} int Jia (int a, int b) {return a + B;} int jian (int a, int b) {return a-A;} void Change (int (* & RP) (Int,int)) {rp = Jian;} void Main3 () {int (* p) (int a, int b) (Jia); std::cout << P (1, 2) << Std::endl;//int (*&RP) (int a, int b) (p);//reference function pointer//rp=jian;//() only applies to initialize change (p); Std::cout << P ( 1, 2) << std::endl;system ("pause");} Int (*& changep (int (*&RP) (Int,int))) (int, int) {RP = Jian;return rp;} void Main4 () {int (*p) (int a, int b) (Jia); std::cout << P (1, 2) << std::endl;p = CHANGEP (p); Std::cout << P ( 1, 2) << std::endl;system ("pause");} void Main5 () {//int *p[4];int a = 1, b = 2, c = 3;int *px[3] = {&a, &b, &c};//int && p [4] = {A,b,c} ;//reference array is illegal}struct mystr {int b;double a;char c;//code area function does not count toward the structure of Sizeofvoid go () {std::cout << "123456789" << Std::endl;}}; Class Myclass{char & A;char & B;char & c;//The nature of the reference is a pointer to the direct sizeof reference, that is, the reference data size//reference variable occupies 4 bytes};void main6 () {int num = 10;int & Rnum (num);d ouble db = 10.9;double & RDB (db),//Direct Action referenced variable std::cout << sizeof (rnum) << std::e Ndl;std::cout << sizeof (RDB) << std::endl;std::cout << sizeof (MyClass) << std::Endl;system ("Pause");} int getdata (int && num)//rvalue reference, save memory copy, memory optimization must {std::cout << num << std::endl;num + 10;return num;} void main7 () {int a = 5;int B = 4;std::cout << GetData (a+1) << std::endl;system ("pause");} The left value, generally can take the address is the left/right value some cases can, some cases can not void Main8 () {int a = 3;int B = A + 1;//rvalue-L value std::cout << GetData (Std::move (a) << Std::endl;//std::move convert Lvalue to Rvalue, C++11}void Main9 () {//const int num (6); Char str[10] ("Hello");// The qualifying string is not modified by const char *PC (str);//Pointer to constant is limited to the data cannot be modified, +1,+2,+3str[3] = ' x ';//Yes,//pc[3] = ' y ';//* (PC + 3) = ' Y ';p c = "World"; System ("Pause");} void Main10 () {char str[10] ("Hello"), const char (&RSTR) [ten] (str),//constant Reference const char (&RRSTR) [ten] (RSTR);// A reference can be initialized to another reference str[4] = ' X ';//rstr[4] = ' Y ';} void Main11 () {int (*p) (int a, int b) (Jia); std::cout << P (1, 2) << std::endl;int (* const &RP) (int a, int b) (p);//reference function pointer//rp=jian;//() only for initialization}
Auto Auto variable automatically creates data based on type
#include <iostream> #include <stdlib.h>void main () {double db = 10.9;double *pdb = &db;auto num = pdb;//Universal incoming Interface Std::cout << typeid (db). Name () << std::endl;std::cout << typeid (num). Name () << STD::ENDL;STD :: cout << typeid (PDB). Name () << Std::endl;//typeid (db). Name () db2;decltype (db) NumA (10.9);// Generic backup interface std::cout << sizeof (NUMA) << " << numa << std::endl;system (" pause ");}
Bool
#include <iostream> #include <stdlib.h>void main () {bool BL = (1 && 1) | | 2 | | ( -1 && 0); Std::cout << typeid (BL). Name () << std::endl;std::cout << bl << Std::endl; Decltype (BL) bt (1 + 2 * 3-4 && 3 + 2 | |-1); Std::cout << bt << std::endl;system ("pause");}
Enum
C is a weak type and does not do type checking. C + + is a strong type and requires more rigor.
Enum.c
#include <stdio.h>enum color{red=11, yellow,green,white};void Main () {enum color Color1;color1 = 18;// Do not pay attention to data type Color1 = red;printf ("%d", red);p rintf ("\n%d", yellow);p rintf ("\n%d", green); GetChar ();}
Enum.cpp
#include <iostream>enum color:char{red= ' A ', yellow, green, white};void main () {color MyColor = Red;mycolor = Yello W;//mycolor = ' A ';//Make sure that there is no error within the scope of the enumeration MyColor = color::white;//new syntax color Mycolor1 (red); color Mycolor2 (color::red);p rintf ( "%d,%c\n", red,red);p rintf ("%d,%c\n", Yellow,yellow); System ("Pause");}
newdelete Global
#include <iostream> #include <stdlib.h>//Global New Delete monitors all release allocations//local new Delete monitors all allocations for a class release void *operator New (size_t size) {if (size = = 0) {return 0;} void *p = malloc (size), Std::cout << "globally called Memory allocated" <<p<<std::endl;return p;} void operator delete (void *p) {std::cout << "globally called Memory Freed" << P << std::endl;free (p);} void *operator new[] (size_t size) {return operator new (size);//Each object calls the already overloaded new, calling construct}void operator] (delete[) { return operator delete (p);//each object calls already overloaded delete, calls the destructor}class tansheng{public:static int jishuqi;//static int *p;int length; Public:tansheng ()//build when initialize {std::cout << "Tan Sheng is created" << Std::endl;} ~tansheng ()//release memory when deleting {std::cout << "Tan Sheng is destroyed" << Std::endl;} static void * operator new (size_t size) {Jishuqi + = 1;std::cout << "Object created" << Std::endl;tansheng *ptemp =:: New tansheng;//hijacking return ptemp;} static void * operator new[] (size_t size) {std::cout << "object array created" << Std::endl;return operator new (size);} Static void operator delete (void *p) {Jishuqi-= 1;std::cout << "Object destroyed" << Std::endl;::d elete p;//:: Global}static Voi D operator delete[] (void *p) {std::cout << "Object array destroyed" << Std::endl;return operator Delete (p);}}; int Tansheng::jishuqi = 0;void mai01n () {//int *p = new Int[10];//delete[]p;tansheng *P1 = new Tansheng[5];d elete []p1;sys TEM ("Pause");} void Main () {int *p = new int (8);d elete P;system ("pause");}
Big Data multiplication and structure
#define _crt_secure_no_warnings#include <stdio.h> #include <stdlib.h> #include <string.h>// C Language Declaration variables need to be added STUCT//C language structure inside can not have function//C language structure without public, private, inherit struct mystruct{int num1;int num2;}; struct MyStruct mystruct1;///addition, subtraction///1234.567x12345.987///15k+ Division, void Getbigdata (char *dataa,char* datab) {int lengt Ha = strlen (dataa), int lengthb = strlen (datab), int *pres = (int *) malloc (sizeof (int) * (Lengtha + LENGTHB)), memset (pres, 0, sizeof (int) * (Lengtha + LENGTHB));//initialization//multiplicative for (int i = 0; i < lengtha;i++) {for (int j = 0; J < lengthb;j++) {Pres[i+j +1]+= (Dataa[i]-' 0 ') * (Datab[j]-' 0 ');}} carry for (int i = Lengtha + lengthb-1;i>=0;i--) {if (pres[i]>=10)//Carry {pres[i-1] + = Pres[i]/10;//carry Pres[i]%= 10;// Remove the single digit}}int i = 0;while (pres[i]==0) {i++;//is not exactly 0 position}char *lastres = malloc (sizeof (char) * (Lengtha + LENGTHB)); int j;for (j = 0; J < Lengtha + LENGTHB; J + +, i++) {lastres[j] = pres[i] + ' 0 ';} LASTRES[J] = ';p rintf ("Last result =%s", lastres);} void Main () {char str1[100] = {0};char StR2[100] = {0};scanf ("%s%s", str1, str2);p rintf ("str1=%s,str2=%s", str1, str2);//Print Results Getbigdata (str1, str2); System (" Pause ");}
#define _crt_secure_no_warnings#include<iostream> #include <stdlib.h> #include <string.h>// In addition to the data there is a function struct bigdatacom{protected://internal private char Dataa[100];char datab[100];p ublic://public public void init (const char *STR1, const char *str2) {std::cout << typeid (*this). Name () << Std::endl; strcpy (This->dataa, str1); strcpy (This->datab, str2);} char * Getbigdata () {int lengtha = strlen (dataa); int LENGTHB = strlen (datab); int *pres = (int *) malloc (sizeof (int) * (Lengtha + LENGTHB)); memset (pres, 0, sizeof (int) * (Lengtha + LENGTHB));//initialization//multiplicative for (int i = 0; i < Lengtha; i++) {for (int j = 0 ; J < LENGTHB; J + +) {pres[i + j + 1] + = (dataa[i]-' 0 ') * (Datab[j]-' 0 ');}} carry for (int i = Lengtha + lengthb-1; I >= 0; i--) {if (Pres[i] >= 10)//Carry {pres[i-1] + = Pres[i]/10;//carry Pres[i] %= 10;//take out single digit}}int i = 0;while (pres[i] = = 0) {i++;//is not exactly 0 position}char *lastres = (char*) malloc (sizeof (char) * (Lengtha + LENGTHB ); int j;for (j = 0; J < Lengtha + Lengthb; j + +, i++) {Lastres[j]= pres[i] + ' 0 ';} LASTRES[J] = ' + '; return lastres;//printf ("Last result =%s", lastres);}; struct MyClass:p ublic bigdatacom//inheritance {void Coutstr ()//new {std::cout << this->dataa << this->datab < < Std::endl;}}; void Main () {MyClass class1;class1.init ("12345", "n"), Std::cout << class1.getbigdata () << Std::endl; CLASS1.COUTSTR (); System ("Pause");} void Main1 () {bigdatacom big1;//c++ struct may not structbig1.init ("123123", "456456");//Call intrinsic function Std::cout << Big1.getbigdata () << std::endl;system ("pause");}
function Templates andAutoAutomatic Variables
function templates
#include <stdlib.h> #include <iostream> #include <cstdarg>//function template variable parameter//parameter at least one of them is a template type template< TypeName nt>nt sum (int count,nt data1 ...) Accumulate {va_list pointer to arg_ptr;//parameter list va_start (Arg_ptr, count);//Limit the number of parameters NT Sumres (0), which is limited from count, for (int i = 0; i < count;i++ ) {sumres + = Va_arg (arg_ptr, NT);} Va_end (arg_ptr);//End return sumres;} T general data type Template<typename t>t MAX (t*p, const int n) {T maxdata (p[0]), for (int i = 1; i < n; i++) {if (Maxdata < P[i]) {maxdata = P[i];}} return maxdata;} int Getmax (int *p, int n) {int max (0); max = p[0];//assumes the first digit maximum for (int i = 1; i <; i++) {if (Max<p[i])//Ensure max>=p[i ]{max = P[i];//}}return Max;} Double Getmax (double *p, int n) {double max (0); max = p[0];//assumes the first digit maximum for (int i = 1; i <; i++) {if (Max < p[i])//ensure Max>=p[i]{max = P[i];//}}return Max;} void Main2 () {std::cout << sum (5,1,2,3,4,5) << std::endl;std::cout << sum (6, 1, 2, 3, 4, 5,6) << St D::endl;std::cout << sum (7, 1, 2, 3, 4, 5, 6,7) << Std::endl;std::cout << SUM (7, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1) << std::endl;std::cout << sum (6, 1.1, 2 .1, 3.1, 4.1, 5.1, 6.1) << Std::endl;system ("pause");} void Main1 () {double a[10] = {2, 3, 4, 98,, 999.1, 123, 0,};int b[10] = {1, 2, 3, 4,, 6, 7, 8, 9,};st D::cout << Max (a,10) << std::endl;std::cout << Max (b, ten) << Std::endl;system ("pause");}
Auto and Function Templates
#include <stdlib.h> #include <iostream>/*auto get (int num, double data)->decltype (num*data) {}*/// Automatic data type, based on the actual derivation of the type, Template<class t1,class t2>//gets the type auto get (T1 data, T2 Bigdata) According to the type->decltype (data *bigdata) {return data*bigdata;} The function parameter does not allow the use of automatic variables//int putnum (auto num)//{////}void main () {std::cout << typeid (Get (12.0, ' A ')). Name () << STD :: Endl;std::cout << get (12.0, ' a ') << std::endl;std::cout << typeid (Get ("a")). Name () << std: : Endl;std::cout << Get (+, ' A ') << std::endl;system ("pause");}
Wide character localization
#include <iostream> #include <stdlib.h> #include <locale>void main () {setlocale (Lc_all, "CHS");// Set localization wchar_t *P1 = l "123456123123qweqeqe"; std::wcout << p1 << std::endl;wchar_t *p2 = l "Beijing 123456"; std::wcout << p2 << std::endl;system ("pause");}
inline inner function
#include <stdlib.h> #include <iostream>//replace the # define GETX3 (N) n*n*n//1+2*1+2*1+2//function//inline is only recommended for compilers// /In general, we do the following restrictions on the inline function://(1) cannot have recursion//(2) cannot contain static data//(3) cannot contain a loop//(4) cannot contain a switch and goto statement//(5) cannot contain an array//if an inline function definition does not meet the above limitation, The compilation system treats it as a normal function. inline int getX3 (int x);//inline function, internally expanded inline int getX3 (int x)//type safety {return x*x*x;} Template <class t>inline t getX2 (t x)//c++ type mismatch error, not simply replace {return x*x;} void Main () {std::cout << GETX3 (1 + 2) << std::endl;std::cout << getX3 (1 + 2) << Std::endl;std::cou T << GETX3 ((1 + 2)) << std::endl;std::cout << GETX3 ((2.0 + 2)) << Std::endl;system ("pause");}
CCPPdifferent
C Features
#include <stdio.h> #include <stdlib.h>int a;//c language global variables have declarations and definitions of differences int a;int a;int a;int a;static int b;static int B;main () {//3-3? System ("Calc"): System ("MSPaint"); int num = 5 < 3?10:9;printf ("%d", num); int a = 3;//(a = 3) = 4;int b = 5;//(a > B a:b) =2;//(++a) ++register int Numa=1;//&numa;system ("pause");} void Test (int a, double, int)//compilation cannot pass {}
C++
#include <stdio.h> #include <iostream>//c++ entities that have the right value detected, automatically converted to an Lvalue//c language no, right. Conversion of Rvalue to lvalue//c++ global variable no difference between declaration and definition Static global variables are also not declared with the definition of the difference//c++ is a strongly typed system, the function return value must have the type//register C + + compiler is optimized, the fetch address is detected, will not put it to register//register can take address int a;//int A; the static int b;//static int b;//c++ compiler is broadly compiled,//In order to modify the source code, leaving behind extension//placeholder, placeholder parameter, void test (int a, double, int) {std::cout << A;} void main1 () {int a = 3; (A = 3) = 4;int B = 5; (A > b? a:b) = 2; (++a) ++;//(A + 1) ++;register int num (1); std::cout << &num<< std::endl;std::cout << a <<b<< std::endl;test (1, 2.9, 3); System ("Pause");} Detele try to set it to Null//void main2 () {int *p = new Int;delete p;//prevent duplicate deletion p = Null;delete p;} void Main () {//int num;//file redirection input output, Web page redirection Cgichar str[100] = {0};std::cin>>str;std::cout << Str;system ( STR);}
Copyright notice: This blog all articles are original, welcome to Exchange, welcome reprint, reprint do not tamper with the content, and indicate the source, thank you!
C/s + + Academy 0814-Reference Advanced, reference advanced add/auto automatic variable to automatically create data based on type/bool/enum/newdelete global/Big data multiplication with struct/function template and auto/wide character localization/inline