3

I have a fairly simple problem. I'm trying to make a function that accepts a map by reference and iterates through the keys of the map.

#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;
}

But, I'm getting an error at the line "fold = (*i);" which states:

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>&]()’
user1005909
  • 1,825
  • 2
  • 17
  • 27

4 Answers4

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

fold is of std::string type; whereas (*i) is of map<string, int32_t>::value_type type, which happens to be std::pair<const string, int32_t>.Obviously, you cannot assign later to the former.

What you probably want to do, is to

fold = i->first; // which extracts "key" from the std::map<>::iterator
Alexander Poluektov
  • 7,844
  • 1
  • 28
  • 32
2

a map it's a container of pair. you can access the value part using the -> operator:

 fold = i->second;
CapelliC
  • 59,646
  • 5
  • 47
  • 90
2

*i is an std::pair; you should write

fold = i->first

To get the key of the entry.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
2

Try the following instead of fold = (*i)

fold = i->first;
hatboyzero
  • 1,929
  • 1
  • 21
  • 44