1

STL(またはboost)タイプunordered_mapを作成しようとしていますが、このコンテナーに要素を挿入しようとするとエラーがスローされます。boost::mulprecisioncpp_intgcc

#include <boost/multiprecision/cpp_int.hpp>
#include <boost/unordered_map.hpp>

using namespace boost::multiprecision;

int main()
{
  cpp_int z(123123123);
  cpp_int x(123123123);

  boost::unordered_map<cpp_int, cpp_int> data;

  // line below will throw compilation errors
  //data.insert(std::make_pair(z,x));
  return 0;
}

完全なエラーログはこちら

最初のエラー:

In file included from /usr/include/boost/functional/hash/hash.hpp:529:0,
                 from /usr/include/boost/functional/hash.hpp:6,
                 from /usr/include/boost/unordered/unordered_map.hpp:20,
                 from /usr/include/boost/unordered_map.hpp:16,
                 from main.cpp:2:
/usr/include/boost/functional/hash/extensions.hpp: In instantiation of
  ........
main.cpp:13:34:   required from here
/usr/include/boost/functional/hash/extensions.hpp:269:34: error: no matching function for call to ‘hash_value(const boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<> >&)’
             return hash_value(val);
                                  ^

STL/の多重精度型boostに関するコンテナの使用に制限はありboostますか? ブースト1.54を使用しています。

編集

これが重複している可能性があるという問題は、boost 1.56 で追加されたシリアライゼーション サポートを使用boost::multiprecisionしています (少なくともドキュメント@1.55@1.56の違いによると) 。

また、その質問では、シリアライゼーションのサポートなしでこの問題を解決するために言及された他のアプローチはありませんでしたboost::multiprecision

4

1 に答える 1

3

これが重複している可能性のある質問」は、質問自体の作業テクニックを文書化しています-文字列表現をハッシュします:

#include <boost/multiprecision/cpp_int.hpp>
#include <boost/unordered_map.hpp>

using namespace boost::multiprecision;

template <typename T>
struct hash_str
{
    size_t operator()(const T& t) const { return std::hash<std::string>()(t.str()); }
};

int main()
{
  cpp_int z(123123123);
  cpp_int x(123123123);

  boost::unordered_map<cpp_int, cpp_int, hash_str<cpp_int>> data;

  data.insert(std::make_pair(z,x));
}

ノート:

  • cpp_int::str()型が格納する完全な精度を出力するかどうかはわかりませんが、そうでない場合は、異なる値が同じものstr()を生成するため、ハッシュがハッシュテーブルの同じバケットで衝突し、機能が損なわれることはありませんが、移動しますO(1) から O(N) パフォーマンスへ。したがって、デフォルトでstr()は完全な精度が表示されないが、それを強制する方法がある場合、非常にわずかに異なる多くの値を処理する場合、それは良い考えです。

  • 浮動小数点型をキーとして使用するすべての場合と同様に、小さな丸めの違いにより、既存のマップ エントリが見つからない/一致しなくなり、意図しない「重複」などが発生する可能性があるため、注意してください。

プログラムが遅すぎ、プロファイルがハッシュが原因であることを証明している場合は、代替案やブーストのアップグレードについて心配してください....

于 2015-08-19T04:19:35.380 に答える