2012-02-14 8 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/utility/pair)。 –

答えて

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

それはペアのコンテナのマップにあります。 あなたが使用して値の一部にアクセスすることができます - >演算子:私はSTD ::ペアである*

fold = i->second; 
2

を。書く必要があります

fold = i->first 

エントリのキーを取得します。

2

`のstd :: map`イテレータを逆参照すると、あなたに[`のstd :: pair`](http://en.cppreference.com/wを与えることを覚えておいてください代わりにfold = (*i)

fold = i->first; 
関連する問題