0

私は C++ で最初のステップを実行しており、いくつかの助けを借りて、簡単な関数を作成するコードを作成しました。しかし、私には問題があります。特定のライブラリを必要とするビットセット関数を使用していますが、このライブラリをコードに導入する担当者がわかりません。

ネットで情報を読んでいるのですが、うまくいかないので、詳しい方法を教えていただけないでしょうか。

あなたが私が見てきたアイデアを作るためにhttp://www.boost.org/doc/libs/1_36_0/libs/dynamic_bitset/dynamic_bitset.htmlhttp://www.boost.org/doc/libs/1_46_0 /libs/dynamic_bitset/dynamic_bitset.html#cons2および同様の場所。

コードを添付して、私が何をしているのかを理解してもらいます。

前もって感謝します :)

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

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

}

4

1 に答える 1

1

bitset動的に拡張したくない場合はbitset、標準に準拠したすべての C++ 実装に組み込まれている を使用できます。

#include <iostream>
#include <bitset>

int main() {
  unsigned long long dec;
  std::cout << "Write a number in decimal: ";
  std::cin >> dec;
  const size_t number_of_bits = sizeof(dec) * 8;
  std::bitset<number_of_bits> bs(dec);
  std::cout << bs << std::endl;
  for (size_t i = 0; i < number_of_bits; i++) {
    if (bs[i])
      std::cout << "Position " << i << " is 1" << std::endl;
  }
  return 0;
}

dynamic_bitsetこのクラスを使用するには、 Boostライブラリをダウンロードし、boost フォルダーをコンパイラのインクルード ディレクトリに追加する必要があります。GNU C++ コンパイラを使用している場合は、次のようにする必要があります。

g++ -I path/to/boost_1_46_1 mycode.cpp -o mycode
于 2011-03-23T11:42:21.327 に答える