2017-11-12 4 views
-1

배열에 얼마나 많은 숫자가 있는지 계산하려면 std::unordered_set을 사용하고 있습니다.
내가하려고하는 것은 배열에서 숫자 중 하나가 삭제 될 때 특정 버킷의 크기를 하나씩 줄이는 것입니다.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

@ 카를로스 나는이 K * K 영역 안에 얼마나 많은 고유 번호가 있는지를 세면서 모든 K * K 영역을 확인하는 큰 N * N 배열을 가지고 있습니다. 그것을하기 위해 나는 std :: unordered_set을 사용한다. K가 매우 클 수도 있기 때문에 K * K 영역을 하나씩 이동할 때마다 고유 번호의 수를 삭제하지 않으려 고하므로 왼쪽의 행에서 요소를 계산하고 오른쪽 행. –

+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'. 조사가 커지면 '지도'가 더 나은 대답이 될 수 있습니다. – aschepler