- #pragma once
- #include<iostream>
- #include<vector>
- namespace MySTL
- {
- template<size_t N>
- class bitset
- {
- public:
- bitset()
- {
- _bits.resize(N / 8 + 1, 0);
- }
- void set(size_t X)
- {
- size_t i = X / 8;
- size_t j = X % 8;
- _bits[i] |= (1 << j);
- }
- void unset(size_t X)
- {
- size_t i = X / 8;
- size_t j = X % 8;
- _bits[i] &= (~(1 << j));
- }
- bool test(size_t X)
- {
- size_t i = X / 8;
- size_t j = X % 8;
- return _bits[i] & (1 << j);
- }
- private:
- std::vector<char> _bits;
- };
-
-
-
- void test_bitset()
- {
- /*bitset<100> bs;
- int array[] = { 1,2,3,4,5,6,7,8,99,78 };
- for (const auto& e : array)
- {
- bs.set(e);
- }
- std::cout<<bs.test(2)<<std::endl;
- std::cout<<bs.test(99)<<std::endl;
- std::cout<<bs.test(78)<<std::endl;
- std::cout<<bs.test(4)<<std::endl;
- bs.unset(2);
- bs.unset(99);
- bs.unset(78);
- bs.unset(4);
- std::cout << bs.test(2) << std::endl;
- std::cout << bs.test(99) << std::endl;
- std::cout << bs.test(78) << std::endl;
- std::cout << bs.test(4) << std::endl;*/
- bitset<-1> bs;
- }
-
-
-
-
- }
位图应用

- template<size_t N>
- class TwoBitSet
- {
- public:
- void set(size_t x)
- {
- if (_bs1.test(x) == false && _bs2.test(x) == false) //00 -> 01
- {
- _bs2.set(x);
- }
- else if (_bs1.test(x) == false && _bs2.test(x)) //01 -> 10
- {
- _bs1.set(x);
- _bs2.unset(x);
- }
- }
- void PrintNumOnce()
- {
- for (size_t i = 0; i < N; ++i)
- {
- if (!_bs1.test(i) && _bs2.test(i))
- {
- std::cout << i << std::endl;
- }
- }
- }
- private:
- bitset<N> _bs1;
- bitset<N> _bs2;
- };
-
- void TwoBitSetTest()
- {
- int array[] = { 1,1,2,3,4,5,6,6,7,7,8,8,8,9,9,9,9 };
- TwoBitSet<100> bs;
- for (const auto& e : array)
- {
- bs.set(e);
- }
- bs.PrintNumOnce();
- }
- }

- void TestFindTest()
- {
- int array1[] = { 1,5,99,6,6,7,10 };
- int array2[] = { 1,8,9,6,7,30,10,40,10,40};
- bitset<100> bs;
- for (const auto e : array1)
- {
- bs.set(e);
- }
- for (const auto e : array2)
- {
- if (bs.test(e))
- {
- std::cout << e << std::endl;
- }
- }
- }

- void TestFindTest()
- {
- int array1[] = { 1,5,99,6,6,7,10 };
- int array2[] = { 1,8,9,6,7,30,10,40,10,40 };
- bitset<100> bs1;
- bitset<100> bs2;
- for (const auto e : array1)
- {
- bs1.set(e);
- }
- for (const auto e : array2)
- {
- bs2.set(e);
- }
-
- for (size_t i = 0; i < 100; ++i)
- {
- if (bs1.test(i) && bs2.test(i))
- {
- std::cout << i << std::endl;
- }
- }
- }
-
- template<size_t N>
- class TwoBitSet
- {
- public:
- void set(size_t x)
- {
- if (_bs1.test(x) == false && _bs2.test(x) == false) //00 -> 01
- {
- _bs2.set(x);
- }
- else if (_bs1.test(x) == false && _bs2.test(x)) //01 -> 10
- {
- _bs1.set(x);
- _bs2.unset(x);
- }
- else if (_bs1.test(x) && _bs2.test(x) == false) // 10 -> 11
- {
- _bs1.set(x);
- _bs2.set(x);
- }
- }
- void PrintNumOnce()
- {
- for (size_t i = 0; i < N; ++i)
- {
- if (_bs1.test(i))
- {
- std::cout << i << std::endl;
- }
- }
- }
- private:
- bitset<N> _bs1;
- bitset<N> _bs2;
- };
-
- void TwoBitSetTest()
- {
- int array[] = { 1,1,2,3,4,5,6,6,7,7,8,8,8,9,9,9,9 };
- TwoBitSet<100> bs;
- for (const auto& e : array)
- {
- bs.set(e);
- }
- bs.PrintNumOnce();
- }
