c++11 boost - 容器(1) array、dynamic_bitset

2018-07-12 09:44:01

array 是大小固定的数组容器,对已知数组大小,且对性能有很大需求的可以选择。

// Copyright (c) 2015
// Author: Chrono Law
#include <iostream>

#include <boost/array.hpp>
using namespace boost;

void case1()
{
    array<int, 10> ar;

    ar[0] = 1;
    ar.back() = 10;
    assert(ar[ar.max_size() - 1] == 10);

    ar.assign(777);
    for (auto x : ar)
    {   std::cout << x << ",";   }

    int *p = ar.c_array();
    *(p + 5) = 253;
    std::cout << ar[5] << std::endl;

    ar.at(8) = 666;
    std::sort(ar.begin(), ar.end());

}

#include <boost/assign.hpp>

void case2()
{
    array<std::string, 3> ar = {"alice","bob", "carl"};

    int a[10] = {0};
    array<int, 10> ar1 = {0};
    assert(std::equal(ar1.begin(), ar1.end(), a));

    array<std::string, 3> ar2 = {"racer"};
    assert(ar2.at(1).empty());

    using namespace boost::assign;
    array<int, 3> arr(list_of(2)(4)(6)) ;

    for (auto i = 0u;i< arr.size() ;++i)
    {   std::cout << arr[i] << ",";}

}

int main()
{
    case1();
    case2();
}
[root@192 c++]# g++ -std=c++11 main.cpp 
[root@192 c++]# ./a.out 
777,777,777,777,777,777,777,777,777,777,253
2,4,6,


标准容器 vector<bool> 能动态的增长但是不方便进行位运算,而 bitset 则刚好相反。

dynamic_bitset 恰巧弥补了这两者之间的空白。

// Copyright (c) 2015
// Author: Chrono Law
#include <iostream>
using namespace std;

#include <boost/core/lightweight_test.hpp>
#include <boost/utility.hpp>
#include <boost/dynamic_bitset.hpp>
using namespace boost;

//////////////////////////////////////////
void case1()
{
    dynamic_bitset<> db1;
    dynamic_bitset<> db2(10); //大小为10
    dynamic_bitset<> db3(0x16, BOOST_BINARY(10101));       //大小为22
    dynamic_bitset<> db4(string("0100")); //字符串构造
    dynamic_bitset<> db5(db3);  //拷贝构造

    dynamic_bitset<> db6;
    db6 = db4;  //赋值操作符

    cout << hex << db5.to_ulong() << endl;   //转换为整数
    cout << db4[0] << db4[1] << db4[2] << endl;  //使用operator []

}

void case2()
{
    dynamic_bitset<> db;

    db.resize(10, true);  //扩展为10个二进制位,值全为1
    cout << db << endl;

    db.resize(5);  //缩小容量为5
    cout << db << endl;

    {
        dynamic_bitset<> db(5,BOOST_BINARY(01110));

        cout << db << endl;
        assert(db.size() == 5);

        db.clear();  //清空
        assert(db.empty()&& db.size()==0);

    }

	//dynamic_bitset 内部使用 Block 来存储二进制位
	//64位系统 Block = 64
    assert(dynamic_bitset<>(64).num_blocks()==1);
    assert(dynamic_bitset<>(65).num_blocks()==2);

    {
        dynamic_bitset<> db(5,BOOST_BINARY(01001));
        db.push_back(true); //容器的末尾,二进制的最高位添加
        assert(db.to_ulong() == BOOST_BINARY_UL(101001));

    }

    {
        dynamic_bitset<> db(5,BOOST_BINARY(01001));
        db.append(BOOST_BINARY(101));  //转成一个块大小,并追加到高位
        assert(db.size() == sizeof(unsigned long)*8 + 5);
        cout << db << endl;
        //0000000000000000000000000000000000000000000000000000000000000101  --  01001

    }
}

void case3()
{
    dynamic_bitset<> db1(4, BOOST_BINARY(1010));

    db1[0] &= 1;  //最低位按位与
    db1[1] ^= 1;  //异或
    cout << db1 << endl;

    dynamic_bitset<> db2(4, BOOST_BINARY(0101));
    assert(db1 > db2);

    cout << (db1 ^ db2) << endl;
    cout << (db1 | db2) << endl;

}

void case4()
{
    dynamic_bitset<> db(4, BOOST_BINARY(0101));

	//test测试n为是否为1
    assert(db.test(0) && !db.test(1));
    
    //any有一个1就返回true,none没有1返回true
    assert(db.any() && !db.none());
    assert(db.count() == 2); //多少个1

    {
        dynamic_bitset<> db(4, BOOST_BINARY(0101));

        db.flip();  //反转
        assert(db.to_ulong() == BOOST_BINARY(1010));

        db.set();  //所有置为0
        assert(!db.none());

        db.reset(); //所有置为1
        assert(!db.any() );

        db.set(1, 1);  //特定位置为1
        assert(db.count() == 1);

    }

    {
        dynamic_bitset<> db(5, BOOST_BINARY(00101));

        auto pos = db.find_first();  //找到第一个为1的位置
        assert(pos == 0);

        pos = db.find_next(pos);  //从当前位置开始找到下一个为1的位置
        assert(pos == 2);

    }
}

void case5()
{
	cout << endl;
    dynamic_bitset<> db(10, BOOST_BINARY(1010101));
    assert(db.to_ulong() == 85);      //85

	cout << "aaa" << endl;

    db.append(10);
    assert(db.to_ulong() == 10325);  //类型转换不能超过
	
	int i = sizeof(unsigned long);
	
	//超出了ulong上限,报错
    db.push_back(1);
    //cout << db.to_ulong() << endl;
    BOOST_TEST_THROWS(db.to_ulong(), std::overflow_error);

	//打印
    string str;
    to_string(db, str);
    cout << str << endl;

	//打印出所有,包括前面的0
    dump_to_string(db , str);
    cout << str << endl;
}

void case6()
{
    dynamic_bitset<> db1(5, BOOST_BINARY(10101));
    dynamic_bitset<> db2(5, BOOST_BINARY(10010));

    cout << (db1 | db2) << endl;
    cout << (db1 & db2) << endl;
    cout << (db1 - db2) << endl;

    dynamic_bitset<> db3(5, BOOST_BINARY(101));
    assert(db3.is_proper_subset_of(db1));  //是真子集

    dynamic_bitset<> db4(db2);
    assert(db4.is_subset_of(db2));      //子集
    assert(!db4.is_proper_subset_of(db2));  //不是真子集
}

void func(int n)
{
    //cout << "test " << n << endl;

    dynamic_bitset<> db(n);
    db.set();
    //cout << db.size() << endl;

    for (dynamic_bitset<>::size_type i = db.find_next(1);
            i != dynamic_bitset<>::npos ;
            i = db.find_next(i ) )
    {
        for (dynamic_bitset<>::size_type j = db.find_next(i);
                j != dynamic_bitset<>::npos ;
                j = db.find_next(j ))
        {
            if ( j % i == 0)
            {
                db[j] = 0;
            }
        }
    }

    cout << dec ;
    for (dynamic_bitset<>::size_type i = db.find_next(2);
            i != dynamic_bitset<>::npos ;
            i = db.find_next(i) )
    {
        cout << i << ", ";
    }

}

void case7()
{
    func(10);
    func(50);
}

int main()
{
    case1();
    case2();
    case3();
    case4();
    case5();
    case6();
    case7();
}
[root@192 c++]# g++ -std=c++11 main.cpp 
[root@192 c++]# ./a.out 
15
001
1111111111
11111
01110
000000000000000000000000000000000000000000000000000000000000010101001
1000
1101
1101

100000000000000000000000000000000000000000000000000000000000010100001010101
00000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000010100001010101
10111
10000
00101
3, 5, 7, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,


 备注

1.编译器版本gcc4.8.5,运行环境centos7 64位
2.本文只做简单记录用,详细用法请参考 Boost Library,或者是罗剑锋的 boost程序库完全开发指南 书本
3..原文地址http://www.freecls.com/a/2712/a5


©著作权归作者所有
收藏
推荐阅读
简介
天降大任于斯人也,必先苦其心志。