pointers - How to make an iterator to a read-only object writable (in C++) -


i've created unordered_set of own type of struct. have iterator set , increment member (count) of struct iterator points to. however, compiler complains following message:

main.cpp:61:18: error: increment of member ‘sentimentword::count’ in read-only object

how can fix this?

here's code:

#include <fstream> #include <iostream> #include <cstdlib>  #include <string> #include <unordered_set>   using namespace std;   struct sentimentword {   string word;   int count; };   //hash function , equality definition - needed used unordered_set type   sentimentword struct sentimentwordhash {   size_t operator () (const sentimentword &sw) const; };  bool operator == (sentimentword const &lhs, sentimentword const &rhs);    int main(int argc, char **argv){     ifstream fin;   int totalwords = 0;   unordered_set<sentimentword, sentimentwordhash> positivewords;   unordered_set<sentimentword, sentimentwordhash> negativewords;     //needed reading in sentiment words   string line;   sentimentword temp;   temp.count = 0;     fin.open("positive_words.txt");   while(!fin.eof()){     getline(fin, line);     temp.word = line;     positivewords.insert(temp);   }   fin.close();     //needed reading in input file   unordered_set<sentimentword, sentimentwordhash>::iterator iter;     fin.open("041.html");   while(!fin.eof()){     totalwords++;     fin >> line;     temp.word = line;     iter = positivewords.find(temp);     if(iter != positivewords.end()){       iter->count++;     }   }     for(iter = positivewords.begin(); iter != positivewords.end(); ++iter){     if(iter->count != 0){       cout << iter->word << endl;     }   }     return 0;  }   size_t sentimentwordhash::operator () (const sentimentword &sw) const {   return hash<string>()(sw.word); }   bool operator == (sentimentword const &lhs, sentimentword const &rhs){   if(lhs.word.compare(rhs.word) == 0){     return true;   }   return false; }  

any appreciated!

elements in unordered_set are, by definition, immutable:

in unordered_set, value of element @ same time key, identifies uniquely. keys immutable, therefore, elements in unordered_set cannot modified once in container - can inserted , removed, though.

i vote use unordered_map instead, using string key , int mapped value.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -