当我们遇到大量整数排序时候为了节省内存空间我们能够考虑使用bit数组来实现,缺点是其仅仅适用于正整数。

思想:

在32位系统一个int类型占4个字节,按位来计算一个int类型能够记录32个数。因此,採用int型数组和移位来实现相关功能。

C++实现bit数组

#include<iostream>
using namespace std;
const unsigned int bitValue[32]=
{
0x80000000,
0x40000000,
0x20000000,
0x10000000,
0x08000000,
0x04000000,
0x02000000,
0x01000000,
0x00800000,
0x00400000,
0x00200000,
0x00100000,
0x00080000,
0x00040000,
0x00020000,
0x00010000,
0x00008000,
0x00004000,
0x00002000,
0x00001000,
0x00000800,
0x00000400,
0x00000200,
0x00000100,
0x00000080,
0x00000040,
0x00000020,
0x00000010,
0x00000008,
0x00000004,
0x00000002,
0x00000001
};
const int bitLen =sizeof(int)*8;
class BitArray
{
private:
unsigned int len;
unsigned int *bit;
public:
BitArray(unsigned int length)
{
if(length<0)
{
throw "length 小于 0";
}
this->len=length;
bit= new unsigned int[length/bitLen+(length%bitLen>0?1:0)]();//初始化为0
}
unsigned int getBit(int index)
{
if(index<0||index>len)
{
throw "index 越界";
}
unsigned int data=bit[index/bitLen];
return (data&bitValue[index%bitLen])>>(bitLen-index%bitLen-1);
}
void setBit(unsigned int index,unsigned int value)
{
if(index<0||index>len)
{
throw "index 越界";
}
if(value!=1&&value!=0)
{
throw "value 值仅仅能为1或者0";
}
unsigned int data=bit[index/bitLen];//计算出其属于数组中哪个int值
if(value==1)
{
bit[index/bitLen]=data|bitValue[index%bitLen];
}else
{
bit[index/bitLen]=data&~bitValue[index%bitLen];
}
}
unsigned int getLength()
{
return this->len;
}
~BitArray()
{
delete[] bit;
}
}; int main()
{
try
{
BitArray bArray(1000000);
bArray.setBit(99999,1);
bArray.setBit(201,1); cout<<bArray.getBit(0)<<endl;
cout<<bArray.getBit(99999)<<endl;
cout<<bArray.getBit(10000)<<endl;
}
catch(char *str_ex)
{
cout<<str_ex<<endl;
}
cin.get();
return 0;
}

C++实现位数组的更多相关文章

  1. 用C语言关于学生管理系统的几种实现方法(一位数组,二维数组,指针,结构体)

    一位数组: #include <stdio.h> #include<string.h> #define N 5 void luru(float s[],int n); void ...

  2. C++ 出现bug :二位数组的操作运算,求非对角线的元素的和

    编写一个通用程序,求出二位数组(行数和列数必须相等)的非对角线的元素之和,试建立类MATRIX完成上述功能 #include<iostream> using namespace std; ...

  3. 如何实现简单的位数组(bit array)(转)

    源:如何实现简单的位数组(bit array) 在 comp.lang.c 上面看到一则不错的 FAQ,<How can I implement sets or arrays of bits?& ...

  4. [翻译] Linux 内核中的位数组和位操作

    目录 Linux 内核里的数据结构 原文链接与说明 Linux 内核中的位数组和位操作 位数组声明 体系结构特定的位操作 通用位操作 链接 Linux 内核里的数据结构 原文链接与说明 https:/ ...

  5. C#编程(五十七)----------位数组

    位数组 如果需要处理很多位,就可以使用BitArray类和BitVector32.BitArray位于命名空间System.Collections中. BitVector32位于命名空间System. ...

  6. C#高级编程五十七天----位数组

    位数组 假设须要处理非常多位,就能够使用BitArray类和BitVector32.BitArray位于命名空间System.Collections中. BitVector32位于命名空间System ...

  7. PHP 之二位数组根据某个字段排序封装

    /** * @param $array * @param $keys * @param string $sort * @return array */ function arraySort($arra ...

  8. c15--二位数组

    // // main.c // day08 #include <stdio.h> int main(int argc, const char * argv[]) { /* int scor ...

  9. java中Arrays.sort()对二位数组进行排序

    int [][]a = new int [5][2]; //定义一个二维数组,其中所包含的一维数组具有两个元素 对于一个已定义的二位数组a经行如下规则排序,首先按照每一个对应的一维数组第一个元素进行升 ...

  10. C和指针 第五章 位数组

    5.4的习题:编写一组函数,实现维数组,函数原型如下: //指定位设置为1void set_bit(char bit_array[], unsigned bit_number); //指定位清零 vo ...

随机推荐

  1. 关于阿里云oss

    这两天抽时间看了一下阿里云oss,阿里云oss是阿里为大数据推出的开放存储服务,为多种语言预留出了接口,下面是我对php接口的一点理解. 当注册了阿里云oss账号时会得到接口,在config里面填上这 ...

  2. C#操作Mysql类

    using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Text. ...

  3. mac安装python3 pandas tushare

    1,升级pip python3 -m pip install --upgrade pip 2,安装依赖包 pip install --user numpy scipy jupyter pandas s ...

  4. JUC集合-BlockingQueue

    BlockingQueue 阻塞队列,支持两个附加操作. 1,在队列为空时,获取元素的线程会等待对列变为非空. 2,在队列为满时,存储元素的线程会等待对列可用. 使用场景: 生产者往对列里添加元素 消 ...

  5. BZOJ1060: [ZJOI2007]时态同步(树形dp 贪心)

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3285  Solved: 1286[Submit][Status][Discuss] Descript ...

  6. wordpress插件推荐

    以下插件可以全部到后台插件中心安装,只需使用关键词搜索即可 安全插件:Wordfence Security 后台增加一道密码:Stealth Login Page 隐藏后台登录地址:WPS-Hide- ...

  7. Android 从imageview中获得bitmap的方法

    第一种: 使用setDrawingCacheEnabled()和getDrawingCache()这两种方法,第一个是为了设置是否开启缓存,第二个就可以直接获得imageview中的缓存,一般来说需要 ...

  8. 利用Axis2默认口令安全漏洞入侵WebService网站

    近期,在黑吧安全网上关注了几则利用Axis2默认口令进行渗透测试的案例,大家的渗透思路基本一致,利用的技术工具也大致相同,我在总结这几则案例的基础之上进行了技术思路的拓展.黑吧安全网Axis2默认口令 ...

  9. Ruby. Vs . Python

    前言:从语言的本质上来分析,我对Ruby持反对态度,毕竟语言是为了交流,在表达的效率层面为了正确性必须适当放弃复杂性.且有句老话说的好,Ruby In Rails 才是语言,而Ruby只是这个语言的工 ...

  10. OpenSessionInViewFilter浅谈

    本篇文章转载自--OpenSessionInViewFilter的配置及作用 spring为我们解决hibernate的Session的关闭与开启问题. Hibernate 允许对关联对象.属性进行延 ...