문제

내가 하고 있는 할당을 위한 학교를 소개하는 해시맵,그래서 내가 만드는 템플릿 클래스에 대한 hashmap 사용하는 std::hash 기능입니다.문제는 나는 데에 나 insert 기능의 코드는 아래와 같습니다:

template <class K, class V>
void HashMap<K, V>::insert(K key, V value)
{
    std::hash<std::string> stringHash;
    int intKey = stringHash(key);
    int bucket = intKey % this->size();
    map[bucket].push_back(std::pair<K, V>(key, value));
}

내에서 오류가 발생 라인: int bucket = intKey % this->size();.

그리 왜 이 부동점 오류가 이 나 작업 전체에서 정수입니다.으로 핵심"바나나"값 3,해시된 int2068534322.하는 경우 this->size 은 5,모듈로 평가해야 하라 2.

그래서,왜 점점 부동점 오류가?

편집 1:도 this->size() 으로 교체 하드 코딩 5(이 무엇인 this->size 을 평가해야기)그래서 this->size 지 않는 데 문제가 평가 함께 0.

도움이 되었습니까?

해결책

당신이 하는 모듈(==과)가동,그래서 당신은 필요하신 분모지 않는 영

template <class K, class V>
void HashMap<K, V>::insert(K key, V value)
{
    std::hash<std::string> stringHash;
    int intKey = stringHash(key);
    int bucket = this->size() ? intKey % this->size() : intKey; 
       // or whatever makes sense to assign for the latter condition
    map[bucket].push_back(std::pair<K, V>(key, value));
}

거나 적어도 배치 assert 문 때 이렇게 추적하는 잘못된 통화에서 나온:

std::assert(this->size());
int bucket = intKey % this->size(); 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top