7

ビットセットを使用していて、コードのパフォーマンスを向上させるために、動的ビットセットに変更したいのですが、これに関連するいくつかの投稿を読んだ後でも、コードを定義する方法がわかりません。

だから私は自分のコードを添付しました、そしてあなたの誰かが私が何をどのように修正すべきかについて私にいくつかのアイデアを与えるのを手伝ってくれるかどうか知りたいです。

前もって感謝します :)

// 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;
}
4

6 に答える 6

10

動的ビットセットを持つ最も簡単な方法は、1 つを使用することです;) http://www.boost.org/doc/libs/1_36_0/libs/dynamic_bitset/dynamic_bitset.html

UPDATE : 完全な例を提供する

#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;
}   
于 2011-03-11T15:21:50.150 に答える
6

Boost を使用しない場合vector<bool>は、各要素が 1 ビットのみを使用するように最適化されたものを使用できます。

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

于 2012-05-10T16:51:38.903 に答える
4

これが大まかに書き直されたプログラムです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

コンストラクターが期待するunsigned longため、整数からビットセットをすぐに構築できないことに注意してください。unsigned long でうまくいけば、変換ループ全体は不要です。

于 2011-03-11T15:48:24.100 に答える
3

Boostdynamic_bitsetを使用してみてください。

于 2011-03-11T15:21:13.997 に答える
0

std::bitset を使用する別の解決策は、bitset<1000> などの十分な大きさのビットセットを定義することです。

次に、変数を使用して実際のビットを制御できますが、ビットセットのメンバー関数を引き続き使用できます

于 2015-10-31T15:21:09.913 に答える
-1

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;
}

それが役に立てば幸い。

于 2012-08-11T13:36:08.467 に答える