[C++]bitset用int数组实现

这并不是真正的bitset,只是用一些简单的c++知识来模仿bitset。

define N 5

const int max_length = 32 * N;//这个数字共有32*5位。从第0位到第max_length-1位,第0位是最低位。
class bitset {
private:
int a[N];//一个int32位,所以只用开N个int
public:
bitset();//默认构造函数,所有位初始化为0
void set(int pos);//把位置pos设置成1
void reset(int pos);//将位置pos设置成0
int count() const;//输出一共有多少个为1的位
bool test(int pos) const;//位置pos是否是1
bool any() const;//是否有是1的位
bool none() const;//是否没有是1的位
bool all() const;//是否所有位都是1

//位运算部分和普通数字的位运算相同。
bitset& operator&= (const bitset& b);
bitset& operator|= (const bitset& b);
bitset& operator^= (const bitset& b);
bitset& operator= (const bitset& b);
bitset& operator <<= (int pos);
bitset& operator >>= (int pos);
bitset operator~() const;
bitset operator&(const bitset& b) const;
bitset operator|(const bitset& b) const;
bitset operator^(const bitset& b) const;
bitset operator<<(int pos) const;
bitset operator>>(int pos) const;
bool operator== (const bitset& b) const;
bool operator!= (const bitset& b) const;
bool operator[] (int pos) const;//返回位置pos是不是1.

//输出部分已经实现
friend std::ostream& operator << (std::ostream& os, const bitset& s) {
for (int i = N-1; i >= 0; i–) {
for (int j = 31; j >= 0; j–) {
if (s.a[i] & (1 << j)) os << 1;
else os << 0;
}
}
return os;
}
};

练习点:只要定义少数关键的函数或操作符,就可以把其他函数和操作符实现。一定要学会用如何减少代码量。

#include<iostream>
#include"Bitset.h"
using namespace std;
int main() {
    bitset a, b;
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        a.set(x);
    }
    cout << "a.count() is " << a.count() << "\n";
    cout << "a.test(5) is " << (a.test(5) ? "true" : "false") << "\n";
    cout << "a.any() is " << (a.any() ? "true" : "false") << "\n";
    cout << "a.none() is " << (a.none() ? "true" : "false") << "\n";
    cout << "a.all() is " << (a.all() ? "true" : "false") << "\n";
    b = ~b;
    for (int i = 0; i < m; i++) {
        int x;
        cin >> x;
        b.reset(x);
    }
    cout << a << "\n";
    cout << b << "\n";
    if (a == b) {
        cout << "hello\n";
    }
    if (a != b) {
        cout << "world\n";
    }
    bitset c;
    // test &
    c = a;
    c &= b;
    cout << c << "\n";
    c = a & b;
    cout << c << "\n";
    // test |
    c = a;
    c |= b;
    cout << c << "\n";
    c = a | b;
    cout << c << "\n";
    // test ^
    c = a;
    c ^= b;
    cout << c << "\n";
    c = a ^ b;
    cout << c << "\n";
    // test <<
    c = a;
    c <<= 2;
    cout << c << "\n";
    c = a << 2;
    cout << c << "\n";
    // test >>
    c = b;
    c >>= 2;
    cout << c << "\n";
    c = b >> 2;
    cout << c << "\n";
    // test []
    for (int i = 0; i < q; i++) {
        int x;
        cin >> x;
        if (a[i])
            cout << "Yes\n";
        else
            cout << "No\n";
    }
}
#ifndef BITSET_H
#define BITSET_H
#include<iostream>
#define N 5
const int max_length = 32 * N;
class bitset {
    private:
        int a[N];
    public:
        bitset();
        void set(int pos);
        void reset(int pos);
        int count() const;
        bool test(int pos) const;
        bool any() const;
        bool none() const;
        bool all() const;
        bitset& operator&= (const bitset& b);
        bitset& operator|= (const bitset& b);
        bitset& operator^= (const bitset& b);
        bitset& operator= (const bitset& b);
        bitset& operator <<= (int pos);
        bitset& operator >>= (int pos);
        bitset operator~() const;
        bitset operator&(const bitset& b) const;
        bitset operator|(const bitset& b) const;
        bitset operator^(const bitset& b) const;
        bitset operator<<(int pos) const;
        bitset operator>>(int pos) const;
        bool operator== (const bitset& b) const;
        bool operator!= (const bitset& b) const;
        bool operator[] (int pos) const;
        friend std::ostream& operator << (std::ostream& os, const bitset& s) {
            for (int i = N-1; i >= 0; i--) {
                for (int j = 31; j >= 0; j--) {
                    if (s.a[i] & (1 << j)) os << 1;
                    else os << 0;
                }
            }
            return os;
        }
};
#endif
#include "Bitset.h"
#include <string.h>
bitset::bitset() {
    memset(a, 0, sizeof(a));
}

bool bitset::test(int pos) const {
    int x = pos / 32;
    if (a[x] & (1 << (pos % 32))) {
        return true;
    } else {
        return false;
    }
}

void bitset::set(int pos) {
    int x = pos / 32;
    if (!test(pos)) {
        a[x] += 1 << (pos % 32);
    }
}

void bitset::reset(int pos) {
    int x = pos / 32;
    if (test(pos)) {
        a[x] -= 1 << (pos % 32);
    }
}

int bitset::count() const {
    int sum = 0;
    for (int i = 0; i != 160; i++) {
        if (test(i)) {
            sum++;
        }
    }
    return sum;
}

bool bitset::any() const {
    for (int i = 0; i != 160; i++) {
        if (test(i)) {
            return true;
        }
    }
    return false;
}

bool bitset::none() const {
    return !any();
}

bool bitset::all() const {
    for (int i = 0; i != 160; i++) {
        if (!test(i)) {
            return false;
        }
    }
    return true;
}

bitset& bitset::operator&=(const bitset &b) {
    for (int i = 0; i != 160; i++) {
        if (test(i) && b.test(i)) {
            set(i);
        } else {
            reset(i);
        }
    }
    return *this;
}
bitset& bitset::operator|=(const bitset &b) {
    for (int i = 0; i != 160; i++) {
        if (test(i) || b.test(i)) {
            set(i);
        } else {
            reset(i);
        }
    }
    return *this;
}
bitset& bitset::operator^=(const bitset &b) {
    for (int i = 0; i != 160; i++) {
        if ((test(i) && !b.test(i)) || (!test(i) && b.test(i))) {
            set(i);
        } else {
            reset(i);
        }
    }
    return *this;
}
bitset& bitset::operator=(const bitset &b) {
    for (int i = 0; i != 160; i++) {
        if (b.test(i)) {
            set(i);
        } else {
            reset(i);
        }
    }
    return *this;
}
bool bitset::operator[](int pos) const {
    if (test(pos)) {
        return true;
    } else {
        return false;
    }
}
bitset& bitset::operator <<= (int pos) {
    bitset temp(*this);
    memset(a, 0, sizeof(a));
    for (int i = 0; i != 160 - pos; i++) {
        if (temp.test(i)) {
            set(i + pos);
        } else {
            reset(i + pos);
        }
    }
    return *this;
}
bitset& bitset::operator>>=(int pos) {
    bitset temp(*this);
    memset(a, 0, sizeof(a));
    for (int i = 0; i != 160; i++) {
        if (temp.test(i)) {
            set((i - pos) % 159);
        } else {
            reset((i - pos) % 159);
        }
    }
    for (int i = 0; i != pos; i++) {
        set(159 - i);
    }
    return *this;
}
bitset bitset::operator~() const {
    bitset temp;
    memset(temp.a, 0, sizeof(temp.a));
    for (int i = 0; i != 160; i++) {
        if (test(i)) {
            temp.reset(i);
        } else {
            temp.set(i);
        }
    }
    return temp;
}

bitset bitset::operator&(const bitset& b) const {
    bitset answer;
    for (int i = 0; i != 160; i++) {
        if (test(i) && b.test(i)) {
            answer.set(i);
        } else {
            answer.reset(i);
        }
    }
    return answer;
}
bitset bitset::operator|(const bitset& b) const {
    bitset answer;
    for (int i = 0; i != 160; i++) {
        if (test(i) || b.test(i)) {
            answer.set(i);
        } else {
            answer.reset(i);
        }
    }
    return answer;
}
bitset bitset::operator^(const bitset& b) const {
    bitset answer;
    for (int i = 0; i != 160; i++) {
        if ((test(i) && !b.test(i)) || (!test(i) && b.test(i))) {
            answer.set(i);
        } else {
            answer.reset(i);
        }
    }
    return answer;
}
bitset bitset::operator<<(int pos) const {
    bitset temp;
    for (int i = 0; i != 160; i++) {
        if (test(i)) {
            temp.set(i + pos);
        } else {
            temp.reset(i + pos);
        }
    }
    return temp;
}
bitset bitset::operator>>(int pos) const {
    bitset temp;
    for (int i = 0; i != 160; i++) {
        if (test(i)) {
            temp.set((i - pos) % 159);
        } else {
            temp.reset((i - pos) % 159);
        }
    }
    for (int i = 0; i != pos; i++) {
        temp.set(159 - i);
    }
    return temp;
}
bool bitset::operator == (const bitset& b) const {
    for (int i = 0; i != 160; i++) {
        if (test(i) != b.test(i)) {
            return false;
        }
    }
    return true;
}
bool bitset::operator!= (const bitset& b) const {
    return !(*this == b);
} 

答案:

#include"Bitset.h"
bitset::bitset() {
    for (int i = 0; i < N; i++) a[i] = 0;
}
void bitset::set(int pos) {
    int x = pos / 32;
    int y = pos % 32;
    a[x] |= 1 << y;
}
void bitset::reset(int pos) {
    int x = pos / 32;
    int y = pos % 32;
    a[x] &= ~(1 << y);
}
int bitset::count() const {
    int sum = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < 32; j++) {
            if (a[i] & (1 << j)) sum++;
        }
    }
    return sum;
}
bool bitset::test(int pos) const {
    int x = pos / 32;
    int y = pos % 32;
    return (a[x] & (1 << y));
}
bool bitset::any() const {
    return count() > 0;
}
bool bitset::none() const {
    return count() == 0;
}
bool bitset::all() const {
    return count() == max_length;
}
bitset& bitset::operator&= (const bitset& b) {
    for (int i = 0; i < N; i++) a[i] &= b.a[i];
    return *this;
}
bitset& bitset::operator|= (const bitset& b) {
    for (int i = 0; i < N; i++) a[i] |= b.a[i];
    return *this;
}
bitset& bitset::operator^= (const bitset& b) {
    for (int i = 0; i < N; i++) a[i] ^= b.a[i];
    return *this;
}
bitset& bitset::operator <<= (int pos) {
    while (pos--) {
        for (int i = N-1; i >= 0; i--) {
            if (i == 0) {
                a[i] <<= 1;
                continue;
            }
            a[i] <<= 1;
            if (a[i-1] & (1 << 31))
                a[i] |= 1;
            else
                a[i] &= ~1;
        }
    }
    return *this;
}
bitset& bitset::operator>>= (int pos) {
    while (pos--) {
        for (int i = 0; i < N; i++) {
            if (i == N-1) {
                a[i] >>= 1;
                continue;
            }
            a[i] >>= 1;
            if (a[i+1] & 1)
                a[i] |= 1 << 31;
            else
                a[i] &= ~(1 << 31);
        }
    }
    return *this;
}
bitset& bitset::operator= (const bitset& b) {
    for (int i = 0; i < N; i++) a[i] = b.a[i];
    return *this;
}
bitset bitset::operator~() const {
    bitset s;
    for (int i = 0; i < N; i++) s.a[i] = ~a[i];
    return s;
}
bitset bitset::operator& (const bitset& b) const {
    bitset s;
    s = *this;
    return s &= b;
}
bitset bitset::operator| (const bitset& b) const {
    bitset s;
    s = *this;
    return s |= b;
}
bitset bitset::operator^ (const bitset& b) const {
    bitset s;
    s = *this;
    return s ^= b;
}
bitset bitset::operator << (int pos) const {
    bitset s;
    s = *this;
    return s <<= pos;
}
bitset bitset::operator >> (int pos) const {
    bitset s;
    s = *this;
    return s >>= pos;
}
bool bitset::operator== (const bitset& b) const {
    for (int i = 0; i < N; i++)
        if (a[i] != b.a[i]) return false;
    return true;
}
bool bitset::operator!= (const bitset& b) const {
    for (int i = 0; i < N; i++)
        if (a[i] != b.a[i]) return true;
    return false;
}
bool bitset::operator[] (int pos) const {
    int x = pos / 32;
    int y = pos % 32;
    return a[x] & (1 << y);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值