C++位图详解(BitMap)

题目:

给40亿个不重复的无符号整数,没排过序。给一个无符号整数,如何快 速判断一个数是否在这40亿个数中。 【腾讯】

思路:

这道题首先要判断40亿个不重复的无符号整数究竟占多大的内存,因为太大的内存我们无法加载到现有的计算机中。

一个整数是4个字节,40亿个整数就是160亿个字节,也就相当于16G内存,就一般的计算机而言很难实现这个加载,所以我们可以采取以下两种方案,一种是分割,一种是位图。

方法:

①分割

采用分割处理,把40亿个数分批次处理完毕,当然可以实现我们最终的目标,但是这样做时间复杂度未免优点太高。

②位图BitMap

在介绍这种方法前我首先来介绍一下什么是位图。

  • 位图BitMap:位图是一个数组每一个数据每一个二进制位表示一个数据,0表示数据不存在,1表示数据存在。

这里写图片描述

如上所示,当我们需要存放一个数据的时候,我们需要安装以下方法:

  1. 首先确定这个数字在整个数据的哪一个数据(区间)。
  2. 确定这个数据(区间)的哪一个Bit位上。
  3. 在这个位上置1即可。

实现代码:

#include <iostream>
#include <vector>
using namespace std;

class BitMap
{
public:
    BitMap(size_t range)
    {
        //此时多开辟一个空间
        _bits.resize(range / 32 + 1);
    }
    void Set(size_t x)
    {
        int index = x / 32;//确定哪个数据(区间)
        int temp = x % 32;//确定哪个Bit位
        _bits[index] |= (1 << temp);//位操作即可
    }
    void Reset(size_t x)
    {
        int index = x / 32;
        int temp = x % 32;
        _bits[index] &= ~(1 << temp);//取反
    }
    bool Test(size_t x)
    {
        int index = x / 32;
        int temp = x % 32;
        if (_bits[index]&(1<<temp))
            return 1;
        else
            return 0;
    }

private:
    vector<int> _bits;
};

void TestBitMap()
{
    BitMap am(-1);
    BitMap bm(200);
    bm.Set(136);
    bm.Set(1);
    cout << bm.Test(136) << endl;
    bm.Reset(136);
    cout << bm.Test(136) << endl;
}

int main()
{
    TestBitMap();
    return 0;
}
  • 18
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 27
    评论
要在C++中生成Bitmap,可以使用Android NDK提供的Bitmap工具类。以下是一个简单的例子: ```c++ #include <android/bitmap.h> void createBitmap(JNIEnv* env, jint width, jint height, jobject& bitmap) { AndroidBitmapInfo info; void* pixels; int ret; // 创建Bitmap对象 ret = AndroidBitmap_getInfo(env, bitmap, &info); if (ret < 0) { // 获取Bitmap信息失败 return; } info.width = width; info.height = height; info.stride = width * 4; info.format = ANDROID_BITMAP_FORMAT_RGBA_8888; ret = AndroidBitmap_setInfo(env, bitmap, &info); if (ret < 0) { // 设置Bitmap信息失败 return; } // 锁定Bitmap像素数组 ret = AndroidBitmap_lockPixels(env, bitmap, &pixels); if (ret < 0) { // 锁定Bitmap像素数组失败 return; } // 生成像素数组 uint32_t* p = static_cast<uint32_t*>(pixels); for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { int r = 255 * x / width; int g = 255 * y / height; int b = 255 * (x + y) / (width + height); p[y * width + x] = 0xFF000000 | (r << 16) | (g << 8) | b; } } // 解锁Bitmap像素数组 AndroidBitmap_unlockPixels(env, bitmap); } ``` 该函数使用Android NDK提供的Bitmap工具类,创建一个指定宽度和高度的Bitmap对象,并生成像素数组。像素数组中的每个像素都使用RGBA_8888格式表示,其中最高位为0xFF表示不透明。 要使用该函数,在Java中调用以下代码: ```java Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); createBitmap(bitmap); ``` 其中,`width`和`height`是Bitmap的宽度和高度,`createBitmap`是C++函数名。调用该函数后,将在C++中生成一个指定宽度和高度的Bitmap对象,并将其传递回Java。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值