繁体   English   中英

如何在我的特定代码中实现动态bitset

[英]How to implement dynamic bitset in my specific code

我正在使用bitset并提高我的代码的性能我想将其更改为动态bitset,但在阅读了与此相关的一些帖子之后,我仍然不知道定义代码的方法。

所以我附上了我的代码,我想知道你们中是否有人可以帮助我给我一些关于我应该修改什么以及如何修改的想法。

提前致谢 :)

// Program that converts a number from decimal to binary and show the positions
// where the bit of the number in binary contains 1

#include <bitset>
#include <iostream>
#include <string>
#include <vector>

using namespace std;


int main()
{

    unsigned long long int dec;
    bitset<5000> binaryNumber;
    bitset<5000> mask;
    mask = 0x1;

    cout << "Write a number in decimal: ";
    cin >> dec;

    // Conversion from decimal to binary
    int x;
    for (x = 0; x < binaryNumber.size(); x++)
    {
        binaryNumber[x] = dec % 2;
        dec = dec / 2;
    }

    cout << "The number " << dec << " in binary is: ";
    for (x = (binaryNumber.size() - 1); x >= 0; x--)
    {
        cout << binaryNumber[x];
    }
    cout << endl;

    // Storage of the position with 1 values
    vector<int> valueTrue;
    for (int r = 0; r < binaryNumber.size(); r++) //
    {
        if (binaryNumber.test(r) & mask.test(r)) // if both of them are bit "1"
                                                 // we store in valueTrue vector
        {
            valueTrue.push_back(r);
        }
        mask = mask << 1;
    }


    int z;
    cout << "Bit 1 are in position: ";
    for (z = 0; z < valueTrue.size(); z++)
    {
        cout << valueTrue.at(z) << " ";
    }
    cout << endl;

    system("pause");
    return 0;
}

拥有动态bitset的最简单方法是使用一个;) http://www.boost.org/doc/libs/1_36_0/libs/dynamic_bitset/dynamic_bitset.html

更新:提供一个完整的例子

#include<iostream>
#include <boost/dynamic_bitset.hpp>
int main() {
    unsigned long long dec;
    std::cout << "Write a number in decimal: ";
    std::cin >> dec;
    boost::dynamic_bitset<> bs(64, dec);
    std::cout << bs << std::endl;
    for(size_t i = 0; i < 64; i++){
        if(bs[i])
            std::cout << "Position " << i << " is 1" << std::endl;
    }
    //system("pause");
    return 0;
}   

对于那些不使用Boost的人 - 你可以使用优化的vector<bool> ,这样每个元素只使用1位。

http://www.cplusplus.com/reference/stl/vector/

这是你的程序大致用dynamic_bitset重写的

#include <iostream>
#include <climits>
#include <string>
#include <boost/lexical_cast.hpp>
#include <boost/dynamic_bitset.hpp>
int main()
{
    std::cout<<"Write a number in decimal: ";
    unsigned long long dec;
    std::cin >> dec;

    // Conversion from decimal to binary
    std::string str;
    for(unsigned long long d = dec; d>0; d/=2)
        str.insert(str.begin(), boost::lexical_cast<char>(d&1) );
    boost::dynamic_bitset<> binaryNumber(str);
    std::cout << "The number " << dec << " in binary is: " << binaryNumber<<'\n';

   // Storage of the position with 1 values
   std::vector<size_t> valueTrue;
   for( size_t pos = binaryNumber.find_first();
        pos != boost::dynamic_bitset<>::npos;
        pos = binaryNumber.find_next(pos))
       valueTrue.push_back(pos);

   std::cout<<"Bit 1 are in position: ";
   for(size_t z=0; z < valueTrue.size(); ++z)
       std::cout << valueTrue[z] << " ";
   std::cout << "\n";
}

测试运行: https//ideone.com/OdhWE

注意,您不能立即从整数构造bitset,因为它的构造函数需要unsigned long 如果你可以使用无符号长整数,则不需要整个转换循环

尝试使用Boostdynamic_bitset

使用std :: bitset的另一个解决方案是定义足够大的位集,例如bitset <1000>。

然后,您可以使用变量来控制实际位,您仍然可以使用bitset的成员函数

您可以在不使用boost库的情况下执行此操作。

您可以动态分配位集。 而不是

for(x=0;x<binaryNumber.size();x++)
{
    binaryNumber[x]=dec%2;
    dec=dec/2;
}

你可以简单地做:

 binaryNumber = dec;  

是的确有效!!

然后,您可以执行以下操作,而不是使用单独的向量来存储1的位置:

for(int i=0;i<binaryNumber.size();i++){
    if(binaryNumber[i])
        cout << "Position of 1: " << i << endl;
}

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM