posts - 195, comments - 34, trackbacks - 0, articles - 1

          導航

          <2010年4月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          常用鏈接

          留言簿(14)

          隨筆分類

          隨筆檔案

          文章檔案

          相冊

          收藏夾

          技術基礎

          技術相關

          研究方向

          算法類

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          C++一些用法整理

          Posted on 2010-04-16 10:31 小強摩羯座 閱讀(240) 評論(0)  編輯  收藏 所屬分類: C++ &VC
            1
            2
            3
            4#include<cstdio>
            5#include<iostream>
            6#include<cstdlib>
            7#include<typeinfo> 
            8
            9using namespace std;
           10
           11#define SIZE 8
           12#define SEC (60*60UL)
           13
           14//1. test mem alignment 
           15#pragma pack(0
           16    
           17void fn1()
           18{
           19    cout<<endl<<"next!"<<endl;
           20    
           21    cout<<"---------------------"<<endl;
           22    
           23    int a;
           24    char b;
           25    int c; 
           26    cout<<&a<<endl;
           27    cout<<&b<<endl;
           28    printf("0x%08x "&b); 
           29    cout<<&c<<endl; 
           30    
           31//    int ss[20] = "33424"; 
           32}

           33//2.使用調用函數寫入數據,而不是讀出 
           34int * m_uiROIID = new int[SIZE];
           35
           36int*  getROIID ()
           37{
           38    return m_uiROIID;
           39}

           40
           41void prt()
           42{
           43    for (int i = 0;i < 3;i++)
           44        scanf("%d", getROIID()+i);
           45
           46    for (int i = 0;i < SIZE;i++)
           47    {
           48        printf("%d ,", m_uiROIID[i]);
           49    }

           50}

           51//3.  a hash function  
           52long hashx(long x)
           53{
           54    long a = x | 0x0000FFFF;
           55    long b = x >> 16;
           56    return (a & ~b) | ( b & ~a);
           57}

           58
           59//還不是很正確的
           60void test()
           61{
           62    for (long i = 0; i < 5;i++)
           63        printf(" %f\n", hashx(i) );
           64}

           65
           66//4.引用指針用給參數分配空間 
           67void create(char *& p)
           68{
           69    p = new char;
           70    p = "good idear";
           71}

           72void destroy(char *&p)
           73{
           74    delete p;
           75}
           
           76void testRefPoint()
           77{
           78    char* p = NULL;
           79    if ( p == NULL) printf("%s", p);
           80    else printf("not NULL \n");
           81    create(p);
           82    printf("%s", p);
           83    if ( p == NULL) printf("NULL");
           84    else printf("not NULL after create");
           85    
           86    delete p; 
           87     printf("%s", p);
           88    if ( p == NULL) printf("NULL");
           89    else printf("not NULL after create");
           90    printf("\n");
           91}

           92//5.指針類型決定加1操作的編移量 
           93void testPointer()
           94{
           95    int a[5= {1,2,3,4,5};
           96    int *ptr = (int*)(&a+1);
           97    printf("%d %d" , *(a+1), *(ptr-1) );
           98
           99    printf("\n");
          100}

          101
          102// 6.數據是傳遞指針,測試類型同5 
          103void foo(int [][3] );
          104void main1()
          105{
          106    int a [3][3]= 1,2,3} , 4,5,6},{7,8,9}};
          107
          108
          109    printf("%d" , a[2][1]);
          110    foo(a);
          111    printf("%d" , a[2][1]);
          112
          113    printf("\n");
          114}

          115void foo( int b[][3])
          116{
          117    ++ b;
          118    b[1][1=9;
          119}

          120// 7.逗號表達式和指針使用 
          121void testP()
          122{
          123    int a, b,c, d;
          124    a=3;
          125    b=5;
          126    c=a,b;
          127    d=(a,b);
          128    printf("c=%d" ,c);
          129    printf("d=%d" ,d);
          130
          131    int arr[]={6,7,8,9,10};
          132
          133    int *ptr=arr;
          134
          135    *(ptr++)+=123;
          136
          137    printf("%d,%d",*ptr,*(++ptr));//它是從的計算的 
          138
          139
          140}

          141//8。CPP的相等比較 
          142void testCStr()
          143{
          144    char str1[]       = "abc";
          145char str2[]       = "abc";
          146const char str3[] = "abc"
          147const char str4[] = "abc"
          148const char* str5  = "abc";
          149const char* str6  = "abc";
          150cout <<  ( str1==str2 ) << endl; // 輸出什么?
          151cout <<  ( str3==str4 ) << endl; // 輸出什么?
          152cout << ( str5==str6 ) << endl; // 輸出什么?
          153}

          154
          155//10.虛析構函數, 變量定義出現,typeid和type_info的使用 
          156class Base 
          157{
          158    static    const int Size = 9
          159    public
          160    Base()
          161    {
          162        cout<<"Base()'s ctor"<<endl; 
          163        
          164    
          165    }
           
          166     virtual ~Base()=0
          167    
          168}

          169    inline Base::~Base()
          170    {
          171        cout<<" Base's dtor"<<endl;
          172    }
           
          173    
          174class Derived:public Base 
          175{
          176public:
          177    Derived()
          178    {
          179        cout<<"int Derived ctor"<<endl; 
          180    }

          181    ~Derived()
          182    {
          183        cout<<"int Derived dtor"<<endl; 
          184    }
            
          185}
          ;    
          186 
          187//11.判斷是c不是cpp程序, atexit 在main后調用函數 
          188void testCOrCpp()
          189{
          190    cout<<"is cpp"<<__cplusplus<<endl;
          191//    cout<<_STDC_<<endl;
          192     
          193     atexit(fn1); 
          194}
           
          195
          196//12. test the class ctor and dtor 
          197//Base  a; 
          198void testCtor()
          199{
          200    Base* pBase = new Derived;
          201//    Derived dObj;
          202//    pBase = &dObj; 
          203    
          204//    delete pBase;
          205    
          206    cout<<" ---------------before delete pBase--------"<<endl; 
          207    
          208    printf("******----->     %f"5.);
          209    
          210    printf("------> %d\n"5); 
          211    
          212    char *= "what";
          213    p = "abc"
          214    cout<<p<<endl; 
          215     
          216        if( typeid(*pBase) == typeid(Base) )
          217            cout<<" typeid Base"<<endl;
          218        else if( typeid( *pBase) == typeid(Derived))
          219            cout<<" typeid Derived"<<endl; 
          220        else 
          221            cout<<" typeid NULL"<<endl;
          222            
          223        const    type_info& ti =  typeid(*pBase);
          224            
          225            cout<<"ti.name()=   " << ti.name()<<endl; 
          226            
          227            cout<<"???????"<< typeid(*pBase).name()<<endl; 
          228 
          229}
           
          230     
          231 
          232int main()
          233{
          234    int a[SIZE];
          235
          236    cout<<SEC<<endl; 
          237    //  prt();
          238//    testCOrCpp(); 
          239    
          240//    testCStr();
          241    
          242//    testCtor();
          243
          244    testRefPoint();    /* 
          245    testPointer();
          246
          247    main1();
          248    testP();
          249    
          250    int *pA = new int; 
          251     
          252    delete pA; 
          253    
          254    *pA = 3;
          255    */
           
          256     
          257//    test();
          258}


          主站蜘蛛池模板: 新巴尔虎右旗| 泽普县| 绥中县| 丹江口市| 东乡| 涞水县| 包头市| 谷城县| 巧家县| 留坝县| 大田县| 鄯善县| 包头市| 浑源县| 玛纳斯县| 肃宁县| 陵川县| 九龙城区| 鲁甸县| 文昌市| 邵武市| 肇源县| 无锡市| 洛南县| 德保县| 松原市| 沙湾县| 获嘉县| 兴隆县| 巨野县| 平塘县| 城市| 青川县| 黄陵县| 东海县| 九龙县| 遂宁市| 鲁甸县| 西林县| 上林县| 丹江口市|