标签:c++程序学习(二)
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;
}本文出自 “王尼美的成人之路” 博客,转载请与作者联系!
标签:c++程序学习(二)
原文地址:http://8335914.blog.51cto.com/8325914/1538291