2017-11-12 7 views
-1

std::unordered_setを使用して、配列内にいくつの異なる数字があるのか​​をカウントしています。
私がしようとしているのは、数字の1つが配列から削除されているときに、特定のバケットのサイズを1減らすことです。unordered_setの特定のバケットのサイズを1減らすには?

erase()を使ってみましたが、バケット全体が削除されました。何とかそれをする可能性はありますか?

それはこのように動作するはずです:

std::unordered_set<int> P; 
P.insert(0); 
P.insert(0); 

printf("%d", P.count(0)); //this prints 2 

//P.decrease(0) <-- I'm looking for something like this 
printf("%d", P.count(0)); //this should print 1 
+1

問題の詳細について教えてください。あなたはちょうどいくつかのアイテムの出現を数えたいですか? – Carlos

+0

@Carlos私はこのK * Kエリア内にいくつのユニークな数字があるかを数えて、すべてのK * Kエリアをチェックする大きなN * N配列を持っています。これを行うには、std :: unordered_setを使用します。 Kが非常に大きいかもしれないので、私はK * Kエリアを1つ動かすたびに一意の数字の数を削除したくないので、左の行から要素を数え、右の列。 –

+0

私はあなたが何を意味するか知っていると思います。例を作ることが理にかなっているかもしれません。これを解決するには、左上隅がルートである各領域の接頭辞の合計のようなものを行う必要があります。 しかし、例がないと何を意味するのか分かりません。 – Carlos

答えて

1

あなたはセットで、カウントを持つことはできません。すべてがセット内にあるか、セット内にないかのいずれかです。おそらくマップを使いたいと思うでしょうか?私はちょうどその違いを実証するためにいくつかのサンプルコードを書いた。

#include <unordered_set> 
#include <unordered_map> 
#include <iostream> 

int main() { 
    // http://en.cppreference.com/w/cpp/container/unordered_set 
    std::unordered_set<int> s; 

    std::cout << "Things are either in the set, or not in the set. There is no count." << std::endl; 
    std::cout << "insert 0" << std::endl; 
    s.insert(0); 
    std::cout << "insert 0" << std::endl; 
    s.insert(0); 

    std::cout << "have we seen a 0, yes or no? " << s.count(0) << std::endl; 

    std::cout << "erase 0" << std::endl; 
    s.erase(0); 
    std::cout << "have we seen a 0, yes or no? " << s.count(0) << std::endl; 

    // http://en.cppreference.com/w/cpp/container/unordered_map 
    std::unordered_map<int, int> m; 
    std::cout << "Every key in the map has a value. We can use that to represent count." << std::endl; 
    std::cout << "add 1 to the count for value 0" << std::endl; 
    m[0] += 1; 
    std::cout << "add 1 to the count for value 0" << std::endl; 
    m[0] += 1; 

    std::cout << "How many zeroes are in the bucket? " << m[0] << std::endl; 

    std::cout << "subtract 1 from the count for key==0" << std::endl; 
    m[0] -= 1; 
    std::cout << "How many zeroes are in the bucket? " << m[0] << std::endl; 
} 
+0

または、おそらく 'std :: unordered_multiset'です。カウントが大きくなると、 'map'はもっと良い答えかもしれません。 – aschepler

関連する問題