2

unordered_setのコンストラクターを見てきました。ハッシュバケットの数を設定せずに、カスタムアロケータインスタンスを使用してunordered_setを構築することはできませんか?カスタムアロケータが必要であり、型にはデフォルト値の定義がないため、実装の詳細をいじりたくありません。MSDNは、コンストラクターに対して3つのオーバーロードしか提供しませんが、どれもひどく有用ではありません。

編集:聖なるがらくた。std :: hashの私のSTL実装は、カスタムアロケータタイプの文字列に特化していません-明示的なtypedefs std::stringとstd::wstringのみを実行できます。つまり、ランダムな文字列をハッシュしようとしたくないのは理解できますが、カスタムアロケータがあるからですか?これは私をうんざりさせます。

tokens(std::unordered_set<string>().bucket_count(), std::hash<string>(), std::equal_to<string>(), stl_wrapper::hash_set<string>::allocator_type(this))
template<typename Char, typename CharTraits, typename Allocator> class std::hash<std::basic_string<Char, CharTraits, Allocator>>
    : public std::unary_function<std::basic_string<Char, CharTraits, Allocator>, std::size_t> {
public:
    size_t operator()(const std::basic_string<Char, CharTraits, Allocator>& ref) const {
        return std::hash<std::basic_string<Char, CharTraits>>()(std::basic_string<Char, CharTraits>(ref.begin(), ref.end()));
    }
};

問題を解決しますが、冗長な構造とコピーですか?Ewwwww。

4

2 に答える 2

2

それは奇妙ですが、あなたは正しいです。デフォルトで可能なすべてのパラメーターの組み合わせをサポートするのはやり過ぎだと思ったと思います。

unordered_setこれを処理するために私が考えることができる最善の方法は、すべてのデフォルト設定で空を作成しunordered_set::bucket_count、を使用してそこからデフォルトのバケット数を取得し、実際に必要なコンテナーをインスタンス化するときに入力として使用することです。

unordered_set<int> temp;
size_t buckets = temp.bucket_count;
unordered_set<string> actual(buckets, Hash(), Pred(), 
    YourAllocator(param1 /*, etc */));
于 2010-12-01T16:46:08.400 に答える
0

を書いているのでAllocator、バケットの数も制御するのは理にかなっています。結局のところ、両方ともメモリに関連しています:)

スティーブはあなたがしたくないのであればメソッドの核心を与えました、今私にヘルパー関数を提案させてください:)

template <typename T>
size_t number_buckets()
{
  std::unordered_set<T> useless;
  return useless.bucket_count();
}

そしてそれで、少し(単純な)ヘルパー:

template <typename T, typename Hash, typename Pred, typename Allocator>
std::unordered_set<T,Hash,Pred,Allocator>
  make_unordered_set(Hash const& hash, Pred const& pred, Allocator const& alloc)
{
  static size_t const nbBuckets = number_buckets<T>();
  return std::unordered_set<T,Hash,Pred,Allocator>(nbBuckets, hash, pred, alloc);
}

とかなりうまく機能しautoます:

auto set = make_unordered_set<std::string>(Hash(), Pred(), Allocator(1,2,3));

もちろん、お気に入りの実装から定数を単純に取り除くこともできます。

于 2010-12-01T17:43:14.707 に答える