位域:  最先使用在c语言中后来C++继承了这一优良的特点。

举个栗子:     int  -->  4字节   2^32位 ,如果我们只需要其表达一个0~16的数字,

使用一个int就显得稍稍有些许浪费,所以我们这里就可以使用到位域0~16  --> 2^1 ~ 2^5

我们就可以这样来声明了:      int   sudo: 5; 就可以了!

 /*
设计一个结构体存储学生的成绩信息,
需要包括学号,年纪和成绩3项内容,学号的范围是0~99 999 999,
年纪分为freshman,sophomore,junior,senior四种,
成绩包括A,B,C,D四个等级。
请使用位域来解决:
*/
#include<iostream> using namespace std; enum Age{ freshman , sophomore , junior ,senior } ; enum grade{ A , B , C , D }; class student {
private :
unsigned int num :;
Age age : ;
grade gra: ;
public :
/* 析构函数 */
~ student(){
cout<<"student has been xigou l ! ";
}
/* 构造函数 */
student(unsigned int num , Age age , grade gra );
void show();
} ; student::student(unsigned int num , Age age , grade gra ):num(num),age(age),gra(gra){
}; inline void student::show(){ cout<<"学号为:"<<this->num<<endl;
string sag;
//采用枚举
switch(age){
case freshman : sag = "freshman" ; break ;
case junior : sag = "junior" ; break ;
case senior : sag = "senior" ; break ;
case sophomore : sag = "sophomore" ; break ;
}
cout<<"年纪为:"<<sag<<ends;
cout<<"成绩为:"<<char('A'+gra)<<endl;
} int main(int args [] ,char argv[]){ student stu(,sophomore,C) ;
stu.show();
cout<<sizeof stu<<endl;
return ;
}
 /*
编写一个名为CPU的类,描述一个CPU的以下信息:
时钟频率,最大不会超过3000MHZ = 3000*10^6 ;字长,可以
是32位或64位;核数,可以是单核,双核,或四核,是否
支持超线程。各项信息要求使用位域来表示。通过输出sizeof
(CPU)来观察该类所占的字节数。
*/
#include<iostream>
#include<string>
using namespace std;
//字长
enum word{ a, b };
//核数
enum keshu{one ,two ,four };
//是否支持超线程
enum es_no{yes ,no} ; class intel_CPU{
private :
// 2^29 = 536 870 912
unsigned int tp : ;
word wd : ;
keshu ks : ;
es_no en : ;
public :
//构造函数
intel_CPU(int tp=,word wd=a,keshu ks=two,es_no en=no):
tp(tp),wd(wd),ks(ks),en(en){
cout<<"this is a construct !"<<endl;
};
//析构函数
~intel_CPU();
void show(); };
intel_CPU::~intel_CPU(){ cout<<"this is a 析构函数!!"<<endl;
} void inline intel_CPU::show() { cout<<"时钟频率为:"<<ends;
cout<<tp<<endl;
cout<<"字长为【32 or 64】: ";
if(wd==a)cout<<<<endl;
else cout<<<<endl;
switch (ks){
case one : cout<<"单核"<<endl; break;
case two : cout<<"双核"<<endl; break;
case four: cout<< "四核”"<<endl; break;
default : cout<<"山寨机,没核" ; break;
}
cout<<"是否支持超线程模式:"<< (en==yes?"支持":"不支持")<<endl;
} int main(){ intel_CPU icp();
icp.show();
cout<<sizeof icp<<endl; }

关于位域如何节省内存(C++)的更多相关文章

  1. [PHP]引用返回与节省内存

    PHP中的引用是什么:1.在 PHP 中引用意味着用不同的名字访问同一个变量内容2.引用可以被看作是 Unix 文件系统中的硬链接. 3.使用unset的话,只是删除他这个名字自身对内容的引用,并没有 ...

  2. python类与对象-如何为创建大量实例节省内存

    如何为创建大量实例节省内存 问题举例 在网络游戏中,定义玩家类Player(id, name, level...), 每个玩家在线将创建一个Player实例,当在线人数很多时,将产生大量实例, 如何降 ...

  3. java内存缓存,节省内存

    缓存的对象 这个问题就是我们上面提到的极端情况,在Java中,会对-128到127的Integer对象进行缓存,当创建新的Integer对象时,如果符合这个这个范围,并且已有存在的相同值的对象,则返回 ...

  4. 这种实现方式比使用 += 要更节省内存和 CPU,尤其是要串联的字符串数目特别多的时候。

    这种实现方式比使用 += 要更节省内存和 CPU,尤其是要串联的字符串数目特别多的时候. package main import ( "bytes" "fmt" ...

  5. JS高级---构造函数通过原型添加方法,原型的作用: 共享数据, 节省内存空间

    JS高级---构造函数,通过原型添加方法,原型的作用: 共享数据, 节省内存空间 构造函数 //构造函数 function Person(sex, age) { this.sex = sex; thi ...

  6. 牺牲速度来节省内存,Redis是觉得自己太快了吗

    前言 正常情况下我们选择使用 Redis 就是为了提升查询速度,然而让人意外的是,Redis 当中却有一种比较有意思的数据结构,这种数据结构通过牺牲部分读写速度来达到节省内存的目的,这就是 zipli ...

  7. 节省内存的循环banner(一)

    循环banner是指scrollview首尾相连,循环播放的效果,使用非常广泛.例如淘宝的广告栏等. 如果是简单的做法可以把所有要显示的图片全部放进一个数组里,创建相同个数的图片视图来显示图片.这样的 ...

  8. C语言位域——精妙使用内存

    参考链接  https://blog.csdn.net/yanbober/article/details/8697967  https://blog.csdn.net/Tommy_wxie/artic ...

  9. 大数据量场景下storm自定义分组与Hbase预分区完美结合大幅度节省内存空间

    前言:在系统中向hbase中插入数据时,常常通过设置region的预分区来防止大数据量插入的热点问题,提高数据插入的效率,同时可以减少当数据猛增时由于Region split带来的资源消耗.大量的预分 ...

随机推荐

  1. UVA 1292 十二 Strategic game

    Strategic game Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Sta ...

  2. 【转载】C++知识库内容精选 尽览所有核心技术点

    原文:C++知识库内容精选 尽览所有核心技术点 C++知识库全新发布. 该知识库由C++领域专家.CSDN知名博客专家.资深程序员和项目经理安晓辉(@foruok)绘制C++知识图谱,@wangshu ...

  3. [HDOJ5289]Assignment(RMQ,二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:求满足区间内最大值和最小值差为k的区间个数. 预处理出区间的最值,枚举左端点,根据最值的单 ...

  4. C语言文法定义与C程序的推导过程

    program à external_declaration | program external_declaration <程序> ->  <外部声明> |  < ...

  5. Java方法

    所谓方法,就是用来解决一类问题的代码的有序组合,是一个功能模块. 一般情况下,定义一个方法的语法是: 其中: 1. 访问修饰符:方法允许被访问的权限范围, 可以是 public.protected.p ...

  6. FJNU 1154 Fat Brother And His Love(胖哥与女神)

    FJNU 1154 Fat Brother And His Love(胖哥与女神) Time Limit: 2000MS   Memory Limit: 257792K [Description] [ ...

  7. Monocular Vision

    Monocular Vision: a condition in which one eye is blind, seeing with only one eye Binocular Vision:  ...

  8. python_way day16 JQuary

    python_way day16 JQuery 封装dom js代码 jQuery(1.10,1.12-兼容性好,2.0.以后放弃了ie9以下) - 封装了Dom & JavaScript 查 ...

  9. linux安装phpstorm出现 Startup Error: Unable to detect graphics environment

    在centos6.2下在安装phpstorm下遇到 Startup Error: Unable to detect graphics environment 其中jdk安装的版本是 1.7.0_79  ...

  10. ubuntu的dns设置

    ubuntu的dns设置为: dns-nameservers 8.8.8.8 注意不要少s