Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将无序_中特定bucket的大小减少1?_C++_Unordered Set - Fatal编程技术网

C++ 如何将无序_中特定bucket的大小减少1?

C++ 如何将无序_中特定bucket的大小减少1?,c++,unordered-set,C++,Unordered Set,我正在使用std::unordered_set计算数组中有多少不同的数字。 我试图做的是,当从数组中删除某个数字时,将特定bucket的大小减少一 我尝试使用erase(),但这会删除整个存储桶。有没有可能以某种方式做到这一点 它应该是这样工作的: std::unordered_set<int> P; P.insert(0); P.insert(0); printf("%d", P.count(0)); //this prints 2 //P.decrease(0) <--

我正在使用
std::unordered_set
计算数组中有多少不同的数字。
我试图做的是,当从数组中删除某个数字时,将特定bucket的大小减少一

我尝试使用
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
std::无序集P;
P.插入(0);
P.插入(0);
printf(“%d”,P.count(0))//这张照片是2张

//P.reduce(0)

不能使用set进行计数。所有东西要么在集合中,要么不在集合中。也许你想用地图?我只是写了一些示例代码来演示差异

#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;
}
#包括
#包括
#包括
int main(){
// http://en.cppreference.com/w/cpp/container/unordered_set
std::无序的_集;

std::你能说更多关于问题是什么吗?你想只计算某个项目的出现次数吗?@Carlos我有一个大的N*N数组,我在其中检查每个K*K区域,计算这个K*K区域内有多少个唯一的数字。为此,我使用std::无序集。因为K可能非常大,我不想删除唯一数字的数字s每次我移动K*K区域一次,所以我想从左边的行中计算元素,然后从右边的行中添加元素。我想我知道你的意思。举个例子可能很有意义。要解决这个问题,你需要为左上角是根的每个区域做前缀和。但很难知道你对我说了什么一个没有例子的例子。@Carlos我给这个问题添加了一个例子。你需要一个更好的例子。NxN矩阵在哪里?或者可能是
std::unordered\u multiset
。尽管如果计数会很大,
map
仍然是更好的答案。