天天看點

求變量的資料類型,typeid,bool,C和C++的不同,new和delete,C++中的枚舉,inline和可變參數模闆,auto和函數模闆,寬字元

求變量的資料類型,通過函數typeid(變量名).name();獲得變量的資料類型。

案例如下:

#include<iostream>

#include<stdlib.h>

voidmain()

{

   doubledb

= 10.9;

   double

*pdb = &db;

   autonum

=pdb;

   //通過typeid的方式獲得資料類型

   std::cout

<< typeid(db).name()

<< std::endl;

<< typeid(num).name()

<< typeid(pdb).name()

   //typeid(db).name() db2

   //decltype用于查詢表達式的資料類型(declared

type)

   decltype(db)numa(10.8);

<< sizeof(numa)

<< " " <<numa

<<std::endl;

   system("pause");

}

運作結果如下:

求變量的資料類型,typeid,bool,C和C++的不同,new和delete,C++中的枚舉,inline和可變參數模闆,auto和函數模闆,寬字元

注意:如果是在qt中運作(要加上c++11的配置,在.pro中添加:

#include<typeinfo>

/*

 *求變量的資料類型通過函數typeid(變量名).name()獲得變量的的資料類型

 */

usingnamespacestd;

intmain()

    doubledb=10.9;

    double*pdb=&db;

    autonum=pdb;

    //通過typeid的方式獲得資料類型

    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用于查詢表達是的資料類型(declaredtype)

    decltype(db)numa(10.8);

    std::cout<<sizeof(numa)<<""<<numa<<std::endl;

    return0;

運作結果:

求變量的資料類型,typeid,bool,C和C++的不同,new和delete,C++中的枚舉,inline和可變參數模闆,auto和函數模闆,寬字元

2.bool類型

   boolb1

= (1 && 1) || 2 || (-1 && 0);

<< typeid(b1).name()

<< b1 <<std::endl;

   decltype(b1)bt(1

+ 2 * 3 - 4 && 3 + 2 || -1);

<< typeid(bt).name()

<< bt <<std::endl;

截圖:

求變量的資料類型,typeid,bool,C和C++的不同,new和delete,C++中的枚舉,inline和可變參數模闆,auto和函數模闆,寬字元

在qt中要加上#include<typeinfo>

運作結果是:

求變量的資料類型,typeid,bool,C和C++的不同,new和delete,C++中的枚舉,inline和可變參數模闆,auto和函數模闆,寬字元

3.c++中不同的細節

#include<stdio.h>

//c++全局變量沒有聲明與定義的差别

//靜态全局變量也沒有聲明與定義的差别

//c++是強類型系統,函數傳回值必須要有類型

inta;

//inta; 

不能重複定義a

staticintb;

//staticintb; 

不能重複定義b

//c++編譯器編譯的寬泛

//為了修改源代碼,後面留下拓展

//占位,占位參數

voidtest(inta,double,int)

    std::cout<<a;

    inta=3;

    //c++檢測到右值在存有實體,自動轉換為左值

    //c語言不會把右值轉換為左值

    (a=3)=4;

    intb=5;

    (a>b?a:b)=2;

    (++a)++;

    //registerc++編譯器做了優化,檢測到取位址,就不會把它放到寄存器

    //register可以取位址

    registerintnum(1);

    std::cout<<&num<<std::endl;

    std::cout<<a<<""<<b<<std::endl;

    test(1,2.9,3);

qt中的運作結果是:

求變量的資料類型,typeid,bool,C和C++的不同,new和delete,C++中的枚舉,inline和可變參數模闆,auto和函數模闆,寬字元

4.new和delete

a:用了delete之後,最好使用使用p

=null;

   int

*p =newint;

   deletep;//防止重複删除

   p

   deletep;

delete隻能delete一次,案例如下:

   intnum

= 10;//棧上

*p =newint;//堆上

   *p

= 5;

<< *p <<"

" <<p

   deletep;//隻能釋放一次

<< p <<std::endl;

錯誤截圖如下:

求變量的資料類型,typeid,bool,C和C++的不同,new和delete,C++中的枚舉,inline和可變參數模闆,auto和函數模闆,寬字元

修改後的狀态

求變量的資料類型,typeid,bool,C和C++的不同,new和delete,C++中的枚舉,inline和可變參數模闆,auto和函數模闆,寬字元

delete删除數組元素的時候,要加上[],案例如下:

   //int num[110]

*p =newint[10];

   //下面定義int

i = 0的循環方式c99标準支援

   for

(inti

= 0;i < 10;i++)

   {

       p[i]

= i;

       std::cout

<< p[i]

   }

   delete[]p;//删除數組的空間

截圖如下:

求變量的資料類型,typeid,bool,C和C++的不同,new和delete,C++中的枚舉,inline和可變參數模闆,auto和函數模闆,寬字元

删除對象數組的案例如下:

classtansheng

*p;

   intlength;

public:

   tansheng()//建構的時候初始化

<< "譚勝被建立"

   ~tansheng()//删除的時候釋放記憶體

<< "譚勝被銷毀"

};

   tansheng

*p =newtansheng;

   deletep;//基本資料類型,delete,複雜類型必須加上[]

截圖如下:

求變量的資料類型,typeid,bool,C和C++的不同,new和delete,C++中的枚舉,inline和可變參數模闆,auto和函數模闆,寬字元

   staticintjishuqi;//靜态

   ~tansheng()//删除的時候放記憶體

   staticvoid

* operator new(size_tsize)

       jishuqi

+= 1;

<< "對象被建立"

       tansheng

*ptemp = ::newtansheng;//劫持

       returnptemp;

operator delete(void *p)

-= 1;

<< "對象被銷毀"

       ::deletep;//

::全局

inttansheng::jishuqi

= 0;

//類的内部的new沒有完成配置設定記憶體的動作

//通往全局的new中間做了一個劫持

//空類占一個位元組,表示自己存在

//類的對象,資料是獨立,代碼是共享的

//沒有配置設定記憶體,構造函數無意義

classmyclass

   intnum;

   myclass();

   ~myclass();

private:

myclass::~myclass()

*p1 =newtansheng;

*p2 =newtansheng;

*p3 =newtansheng;

*p4 =newtansheng;

<< p1 <<p2

   deletep1;

   deletep2;

<< tansheng::jishuqi

<< "myclass size" <<sizeof(myclass)

結果如下:

求變量的資料類型,typeid,bool,C和C++的不同,new和delete,C++中的枚舉,inline和可變參數模闆,auto和函數模闆,寬字元

5.枚舉類型

enumcolor

:char{red

='a',yellow,green,white

   colormycolor

=red;

   //mycolor = 'a'; //確定在枚舉的範圍的之内不出錯

   mycolor

=color::white;//新文法

   colormycolor1(red);

   colormycolor2(color::red);

   printf("%d,%c\n",red,red);

   printf("%d,%c\n",yellow,yellow);

求變量的資料類型,typeid,bool,C和C++的不同,new和delete,C++中的枚舉,inline和可變參數模闆,auto和函數模闆,寬字元

6.兩個大數的字元串求乘

#define_crt_secure_no_warnings

#include<string.h>

//除了資料還有函數

structbigdatacom

protected://内部私有

   chardataa[100];

   chardatab[100];

public://共有公開

   voidinit(constchar

*str1,constchar

*str2)

<< typeid(*this).name()

       strcpy(this->dataa,str1);

       strcpy(this->datab,str2);

   char

*getbigdata()

       intlengtha

=strlen(dataa);

       intlengthb

=strlen(datab);

       //兩個數相乘,乘得到的位數不可能大于兩個數的位數之和

       int

*pres = (int

*)malloc(sizeof(int)*(lengtha

+ lengthb));

       //初始化

       memset(pres,

0, sizeof(int)*(lengtha

       //累乘

       for

= 0;i <lengtha;i++)

       {

           for

(intj

= 0;j <lengthb;j++)

           {

               //其中字元1-->9減去‘0’得到的恰巧是整型值

               pres[i

+ j] += (dataa[i]

- '0')*(datab[j]

- '0');

           }

       }

       //進位

=lengtha +lengthb

- 1;i >= 0;i--)

           if

(pres[i]

>= 10)//進位

- 1] += pres[i]

/ 10;//進位

               pres[i]

%= 10;//取出個位數

       inti

       while

== 0)

           i++;//恰好不為0的位置

       char

*lastres = (char*)malloc(sizeof(char)*(lengtha

       intj;

(j = 0;j

<lengtha +lengthb;j++,i++)

           lastres[j]

= pres[i]

+ '0';

       lastres[j]

= '\0';

       returnlastres;

structmyclass

:publicbigdatacom  //繼承

   voidcoutstr() //新增

<< this->dataa

<< this->datab

   myclass class1;

   class1.init("12345","1000");

<< class1.getbigdata()

   class1.coutstr();

voidmain1()

   bigdatacombig1;//c++結構體可要可不要struct

   big1.init("123123","456456");

<< big1.getbigdata()

7.inline隻是對于編譯器的建議

一般情況下,我們隊内聯函數做如下的限制

a:不能有遞歸

b:不能包含靜态資料

c:不能包含循環

d:不能包含switch和goto語句

e:不能包含數組

若一個内聯函數定義不滿足以上限制,則編譯系統把它當做普通函數對待。

案例說明:

//替換

#definegetx3(n)n*n*n

//1+2*1+2*1+2

//函數

//inline隻是對于編譯器的建議

////一般情況下,我們對内聯函數做如下的限制:

//(1)不能有遞歸

//(2)不能包含靜态資料

//(3)不能包含循環

//(4)不能包含switch和goto語句

//(5)不能包含數組

//若一個内聯函數定義不滿足以上限制,則編譯系統把它當作普通函數對待。

inlineint getx3(intx);//内聯函數,内部展開

inlineint getx3(intx)//類型安全

   returnx*x*x;

template <classt>

inlinetgetx2(tx)//c++類型不比對出錯,不是單純的替換

   returnx*x;

<< getx3(1 + 2) <<std::endl;

<< getx3((1 + 2)) <<std::endl;

<< getx3((2.0 + 2)) <<std::endl;

求變量的資料類型,typeid,bool,C和C++的不同,new和delete,C++中的枚舉,inline和可變參數模闆,auto和函數模闆,寬字元

8.函數模闆和可變參數

#include<cstdarg>

//函數模闆,可變參數

//參數至少要有一個模闆類型

template<typenament>

ntsum(intcount,ntdata1

...)//累加

   va_listarg_ptr;  //參數清單的指針

   va_start(arg_ptr,count);//限定從count開始,限定多少個參數

   ntsumres(0);

= 0;i <count;i++)

       sumres

+=va_arg(arg_ptr,nt);

   va_end(arg_ptr); //結束

   returnsumres;

//t通用資料類型

template<typenamet>

tmax(t*p,constintn)

   tmaxdata(p[0]);

= 1;i <n;i++)

       if

(maxdata <p[i])

           maxdata

=p[i];

   returnmaxdata;

intgetmax(int

*p,intn)

   intmax(0);

   max

=p[0]; //假設第一個數位最大

= 1;i < 10;i++)

       //確定max>=

p[i]

(max <p[i])

           max

   returnmax;

doublegetmax(double

   doublemax(0);

=p[0];//假定第一個數位最大

(max <p[i])//確定max>=p[i]

=p[i];//

<< sum(5, 1, 2, 3, 4, 5) <<std::endl;

<< sum(6, 1, 2, 3, 4, 5, 6) <<std::endl;

<< sum(7, 1, 2, 3, 4, 5, 6, 7) <<std::endl;

<< sum(7, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1) <<std::endl;

<< sum(6, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1) <<std::endl;

<< "-----------------" <<std::endl;

   doublea[10]

= { 2, 3, 4, 98, 77, 999.1, 87, 123, 0, 12 };

   int b[10]

= { 1, 2, 3, 4,15, 6, 7, 8, 9, 10 };

<< max(a,

10) << std::endl;

<< max(b,

求變量的資料類型,typeid,bool,C和C++的不同,new和delete,C++中的枚舉,inline和可變參數模闆,auto和函數模闆,寬字元

9.auto與函數模闆

 a:函數參數不允許使用自動變量

求變量的資料類型,typeid,bool,C和C++的不同,new和delete,C++中的枚舉,inline和可變參數模闆,auto和函數模闆,寬字元

 b:auto結合模闆函數的案例如下:

//自動資料類型,根據實際推導出類型

template <classt1,classt2> //根據類型擷取類型

autoget(t1data,t2bigdata)->decltype(data

*bigdata)

   returndata

*bigdata;

<< typeid(get(12.0,'a')).name()

<< get(12.0,'a')

<< typeid(get(12,'a')).name()

<< get(12,'a')

求變量的資料類型,typeid,bool,C和C++的不同,new和delete,C++中的枚舉,inline和可變參數模闆,auto和函數模闆,寬字元

10.寬字元

#include<locale>

   setlocale(lc_all,"chs");//設定本地化

   wchar_t

*p1 = l"123456123123qweqeqe";

   std::wcout

<< p1 <<std::endl;

*p2 = l"北京123456";

<< p2 <<std::endl;

求變量的資料類型,typeid,bool,C和C++的不同,new和delete,C++中的枚舉,inline和可變參數模闆,auto和函數模闆,寬字元

繼續閱讀