2012-02-14 3 views
3

나는 아주 간단한 문제가 있습니다. 참조로지도를 받아들이고지도의 키를 반복하는 함수를 만들려고합니다.함수에 대한 참조로 C++ 맵을 전달하려고하는데 코드를 반복 할 수 없지만 코드를 컴파일 할 수 없습니다.

#include <map> 
#include <string> 
#include <sys/types.h> 

using namespace std; 

void update_fold_score_map(string subfold, 
          int32_t index, 
          int32_t subfold_score, 
          map<string, int32_t> &fold_scores){ 
    for(map<string, int32_t>::iterator i = fold_scores.begin(); 
     i != fold_scores.end(); 
     i ++){ 
    string current_substring; 
    string fold; 
    fold = (*i); 
    current_substring = fold.substr(index, subfold.size()); 

    if (current_substring == subfold){ 
     if (fold_scores[fold] < subfold_score){ 
     fold_scores[fold] = subfold_score; 
     } 
     return; 
    } 
    } 
} 
int main(){ 
    return 0; 
} 

하지만 "fold = (* i);"줄에 오류가 있습니다. 어느 상태 :

compilemap.cpp:16:15: error: no match for ‘operator=’ in ‘fold = i.std::_Rb_tree_iterator<_Tp>::operator* [with _Tp = std::pair<const std::basic_string<char>, int>, std::_Rb_tree_iterator<_Tp>::reference = std::pair<const std::basic_string<char>, int>&]()’ 
+0

의 다음을 시도/cpp/유틸리티/쌍). –

답변

5
fold = (*i); // <- here 

foldstd::string 형이고; (*i)map<string, int32_t>::value_type 유형이며, 이는 std::pair<const string, int32_t> 일 것입니다. 분명히 이전에 할당 할 수 없습니다. 아마 당신은 무엇을 원하는

fold = i->first; // which extracts "key" from the std::map<>::iterator 
+1

아, 고마워! (* i)가 고정 된 후에 ".first"를 더합니다. – user1005909

+4

'i-> first'는 나에게 더 일반적이다. (또한'i''를'BTW'로 바꾼다.) –

2

가 쌍 컨테이너의지도이다. 에는 사용 치 부분에 액세스 할 수 -> 연산자 내가 표준 : 쌍 *

fold = i->second; 
2

단계; 당신이 작성해야합니다

fold = i->first 

항목의 키를 얻으려면.

2

은`표준 : :지도`반복자를 역 참조하는 당신에게 [`표준 : pair`] (http://en.cppreference.com/w을주는 것을 기억하십시오 대신 fold = (*i)

fold = i->first; 
관련 문제