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#include "bm.h"
35
36using namespace std;
37
38int main(void)
39{
40 try
41 {
42 bm::bvector<> bv { 1, 2, 3 }; // Bitvector variable declaration with init list
43
44 cout << "1. bitcount: " << bv.count() << endl;
45
46 // Set some bits.
47
48 bv.set(10);
49 bv.set(100);
50 bv.set(1000000);
51
52 // New bitvector's count.
53
54 cout << "2. bitcount: " << bv.count() << endl;
55
56
57 // Print the bitvector.
58
59 auto value = bv.get_first();
60 do
61 {
62 cout << value;
63 value = bv.get_next(value);
64 if (value)
65 {
66 cout << ",";
67 }
68 else
69 {
70 break;
71 }
72 } while(1);
73
74 cout << endl;
75
76 bv.clear(); // Clean up.
77
78 cout << "3. bitcount: " << bv.count() << endl;
79
80 // We also can use operators to set-clear bits;
81
82 bv[10] = true;
83 bv[100] = true;
84 bv[10000] = true;
85
86 cout << "4. bitcount: " << bv.count() << endl;
87
88 if (bv[10])
89 {
90 bv[10] = false;
91 }
92
93 cout << "5. bitcount: " << bv.count() << endl;
94 }
95 catch(std::exception& ex)
96 {
97 std::cerr << ex.what() << std::endl;
98 return 1;
99 }
100
101
102 return 0;
103}
104
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
size_type count() const BMNOEXCEPT
population cout (count of ON bits)
Definition bm.h:2194
int main(void)
Definition sample1.cpp:38