位图(bitmap)是编码布尔信息的非常简洁的方式,位图的思想是:整数类型(字符类型)的每个位都可以编码一个布尔值——通常用0表示false,用1表示true。通过位操作将其中的某位设置成0或者1。
#include<iostream>
using namespace std;
int main()
{
int k=1<<6; //将第6个bit位为1,其余为0
int j=1<<7;
int map=4;
map |=k; //将map的第6个bit位设置成1;
bool flag=(map & j); //判断map的第7个bit位是否为1。
//参考网上的一个示例程序:
int i;
char *source = "aaabbccdabgf";
char *dest;
char *temp;
unsigned int bitmap[8] = {0,0,0,0,0,0,0,0};
unsigned char c;
unsigned int mask;
dest = (char*)malloc(strlen(source));
temp = dest;
printf("Before %s\n", source);
i=0;
while(source[i])
{
c = source[i];
mask = 1 << (c % 32); //1左移若干位,一个整型数据一般为32bit
if ((bitmap[c/32] & mask) == 0)
{
*temp++ = source[i];
bitmap[c/32] |= mask; //设置bit位为1
}
i++;
}
*temp = '\0';
printf("After %s\n", dest);
}
相关文献:
http://blog.youkuaiyun.com/yushuai007008/article/details/7466945
http://bbs.chinaunix.net/thread-3558006-1-1.html
本文介绍了一种使用位图(bitmap)进行数据处理的方法。利用位图的特性,通过位操作实现高效的数据存储和检索。示例代码展示了如何通过位图去除字符串中的重复字符。
1240

被折叠的 条评论
为什么被折叠?



