BitMagic-C++
sample1.cpp
Go to the documentation of this file.
1/*
2Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15
16For more information please visit: http://bitmagic.io
17*/
18
19/** \example sample1.cpp
20 Example how to use bvector<> to set bits and then retrieve indexes of ON bits
21
22
23 \sa bm::bvector<>::get_next()
24 \sa bm::bvector<>::get_first()
25 \sa bm::bvector<>::set()
26 \sa bm::bvector<>::count()
27 \sa bm::bvector<>::clear()
28 */
29
30/*! \file sample1.cpp
31 \brief Example: bvector<> set bits and then retrieve indexes of ON bits
32*/
33#include <iostream>
34
35#include "bm.h"
36#include "bmundef.h" /* clear the pre-proc defines from BM */
37
38using namespace std;
39
40int main(void)
41{
42 try
43 {
44 bm::bvector<> bv { 1, 2, 3 }; // Bitvector variable declaration with init list
45
46 cout << "1. bitcount: " << bv.count() << endl;
47
48 // Set some bits.
49
50 bv.set(10);
51 bv.set(100);
52 bv.set(1000000);
53
54 // New bitvector's count.
55
56 cout << "2. bitcount: " << bv.count() << endl;
57
58
59 // Print the bitvector.
60
61 auto value = bv.get_first();
62 do
63 {
64 cout << value;
65 value = bv.get_next(value);
66 if (value)
67 {
68 cout << ",";
69 }
70 else
71 {
72 break;
73 }
74 } while(1);
75
76 cout << endl;
77
78 bv.clear(); // Clean up.
79
80 cout << "3. bitcount: " << bv.count() << endl;
81
82 // We also can use operators to set-clear bits;
83
84 bv[10] = true;
85 bv[100] = true;
86 bv[10000] = true;
87
88 cout << "4. bitcount: " << bv.count() << endl;
89
90 if (bv[10])
91 {
92 bv[10] = false;
93 }
94
95 cout << "5. bitcount: " << bv.count() << endl;
96 }
97 catch(std::exception& ex)
98 {
99 std::cerr << ex.what() << std::endl;
100 return 1;
101 }
102
103
104 return 0;
105}
106
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
pre-processor un-defines to avoid global space pollution (internal)
Bitvector Bit-vector container with runtime compression of bits.
Definition: bm.h:115
size_type count() const BMNOEXCEPT
population count (count of ON bits)
Definition: bm.h:2366
int main(void)
Definition: sample1.cpp:40