1.C++數據類型自定義:
#include <iostream> int main(){ typedef char Name[20]; Name a,b; std::cout<<typeid(a).name()<<typeid(b).name(); std::cin.get(); return 0; }
//typeid(變量名).方法名 =>輸出變量的數據類型
另一種定義方法:
#include <iostream> int main(){ typedef struct su{ char name[10]; int age; }; su n; std::cout <<typeid(n).name()<<std::endl; std::cin.get(); return 0; }
2.定義位域
#include <iostream> int main(){ struct bs{ unsigned a :1; unsigned b :3; unsigned c :4; }bit,*pbit;//定義了bit和指向bit的指針pbit bit.a=1; bit.b=7; bit.c=15; printf("%d,%d,%d\n",bit.a,bit.b,bit.c); pbit=&bit; pbit->a=0; pbit->b&=3; pbit->c|=1; printf("%d,%d,%d\n",pbit->a,pbit->b,pbit->c); std::cin.get(); return 0; }
無名的位域是不能使用的
struct k { int a:1 int :2 /*該2位不能使用*/ int b:3 int c:2 };
3.計算程序的運行時間
#include <iostream> #include <ctime> int main(){ clock_t start,end; start =clock(); struct bs{ unsigned a :1; unsigned b :3; unsigned c :4; }bit,*pbit; bit.a=1; bit.b=7; bit.c=15; printf("%d,%d,%d\n",bit.a,bit.b,bit.c); pbit=&bit; pbit->a=0; pbit->b&=3; pbit->c|=1; printf("%d,%d,%d\n",pbit->a,pbit->b,pbit->c); end=clock(); //std::cout<<(double)(end-start)/ CLOCKS_PER_SEC; std::cout <<start; std::cout <<end; std::cin.get(); return 0; }
4.如果定義string ,在前面定義“#include<string>",求字符串長度爲string.size()
如果用char s[],在前面定義"#include<cstring>",還要定義你string的大小。