C++实现位数组
当我们遇到大量整数排序时候为了节省内存空间我们能够考虑使用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++实现位数组的更多相关文章
- 用C语言关于学生管理系统的几种实现方法(一位数组,二维数组,指针,结构体)
一位数组: #include <stdio.h> #include<string.h> #define N 5 void luru(float s[],int n); void ...
- C++ 出现bug :二位数组的操作运算,求非对角线的元素的和
编写一个通用程序,求出二位数组(行数和列数必须相等)的非对角线的元素之和,试建立类MATRIX完成上述功能 #include<iostream> using namespace std; ...
- 如何实现简单的位数组(bit array)(转)
源:如何实现简单的位数组(bit array) 在 comp.lang.c 上面看到一则不错的 FAQ,<How can I implement sets or arrays of bits?& ...
- [翻译] Linux 内核中的位数组和位操作
目录 Linux 内核里的数据结构 原文链接与说明 Linux 内核中的位数组和位操作 位数组声明 体系结构特定的位操作 通用位操作 链接 Linux 内核里的数据结构 原文链接与说明 https:/ ...
- C#编程(五十七)----------位数组
位数组 如果需要处理很多位,就可以使用BitArray类和BitVector32.BitArray位于命名空间System.Collections中. BitVector32位于命名空间System. ...
- C#高级编程五十七天----位数组
位数组 假设须要处理非常多位,就能够使用BitArray类和BitVector32.BitArray位于命名空间System.Collections中. BitVector32位于命名空间System ...
- PHP 之二位数组根据某个字段排序封装
/** * @param $array * @param $keys * @param string $sort * @return array */ function arraySort($arra ...
- c15--二位数组
// // main.c // day08 #include <stdio.h> int main(int argc, const char * argv[]) { /* int scor ...
- java中Arrays.sort()对二位数组进行排序
int [][]a = new int [5][2]; //定义一个二维数组,其中所包含的一维数组具有两个元素 对于一个已定义的二位数组a经行如下规则排序,首先按照每一个对应的一维数组第一个元素进行升 ...
- C和指针 第五章 位数组
5.4的习题:编写一组函数,实现维数组,函数原型如下: //指定位设置为1void set_bit(char bit_array[], unsigned bit_number); //指定位清零 vo ...
随机推荐
- 两道人数多,课程少,query多的题
#每天进步一点点# 来两道很相似的题目~ (智商啊智商.....) hihoCoder #1236:Scores (简单的分桶法+bitset) 2015 Beijing Online的最后一题.题目 ...
- jquery的this和$(this)
1.JQuery this和$(this)的区别 相信很多刚接触JQuery的人,很多都会对$(this)和this的区别模糊不清,那么这两者有什么区别呢? 首先来看看JQuery中的 $() 这 ...
- MongoDB索引05-30学习笔记
MongoDB 索引 索引通常能够极大的提高查询的效率,如果没有索引,MongoDB在读取数据时必须扫描集合中的每个文件并选取那些符合查询条件的记录. 这种扫描全集合的查询效率是非常低的,特别在处理大 ...
- GStreamer基础教程02 - 基本概念
摘要 在 Gstreamer基础教程01 - Hello World中,我们介绍了如何快速的通过一个字符串创建一个简单的pipeline.为了能够更好的控制pipline中的element,我们需要单 ...
- 基本的Mysql语句
操作文件夹(库) 增 create database db1 charset utf8; 查 # 查看当前创建的数据库 show create database db1; # 查看所有的数据库 sho ...
- java中的数组的Arrays工具类的使用
package day04.d1.shuzu; import java.util.Arrays; /** * Arrays 工具类 * @author Administrator * */public ...
- logrotate配置文件
一.logrotate配置文件 /etc/logrotate.conf
- Java 系列之spring学习--注解(三)
一.注解 使用注解之前要开启自动扫描功能 <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...
- Codeforces Round #447
QAQ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<vector> ...
- RecyclerView实现终极画廊效果 中间突出并且压住两侧布局
先给大家上个gif 要不然下面很枯燥 忘记原来在哪里看到了..... 这个效果我找了NNNNNN长时间,,,我认为凭我现在的能力 写出来需要好久 所以 退而求其次找大神写好的... 你们不要小看了这个 ...