아래 코드는 어떤 문제가 있는가? map<uint32t, uint32t> hitcountermap; map<uint32t, uint32t>::iterator…
- User
아래 코드는 어떤 문제가 있는가? map<uint32_t, uint32_t> _hit_counter_map; map<uint32_t, uint32_t>::iterator mi = _g_sorted_counter_map.begin();
codetotal_hit += ((_hit_counter_map[mi->second]) ? _hit_counter_map[mi->second] : 1);Unsafe conversion of expression '((_hit_counter_map[mi->second]) ? _hit_counter_map[mi->second] : 1)' with type 'std::map<unsigned int, unsigned int>::mapped_type' (aka 'unsigned int') to type 'int64_t' (aka 'long') may or may not override sign-bit depending on type sizes, which likely causes different evaluation results on 64-bit and 32-bit platforms
- Assistant
코드에는 몇 가지 잠재적인 문제점이 보입니다. 제시된 코드 조각의 문제점과 가능한 해결책을 자세히 설명하겠습니다.
-
잘못된 맵 참조: 코드에서
mi는_g_sorted_counter_map이라는 맵의 반복자로 초기화되었으나, 실제로 작업을 처리하는 맵은_hit_counter_map입니다._g_sorted_counter_map라는 맵이 정의되지 않았거나, 실수로 다른 이름의 맵을 참조하고 있는 것 같습니다. 설명을 위해서_hit_counter_map의 반복자를 이용해야 할 것 같습니다.cppmap<uint32_t, uint32_t>::iterator mi = _hit_counter_map.begin(); -
키 대신 값으로 참조:
mi->second는 맵의 값(데이터) 부분을 참조합니다._hit_counter_map[mi->second]구문은mi->second를 키로 사용하여_hit_counter_map에서 값을 찾으려 시도합니다. 이는 일반적으로 의도치 않은 접근 방식일 수 있습니다. 아마도 키를 이용해 값을 찾아야 할 것입니다.cpptotal_hit += ((_hit_counter_map[mi->first]) ? _hit_counter_map[mi->first] : 1);
-