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

C++一些用法整理

Posted on 2010-04-16 10:31 小强摩羯座 阅读(221) 评论(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}



只有注册用户登录后才能发表评论。


网站导航: