use*_*561 1 c++ pointers iterator
我创建了一个unordered_set我自己类型的struct. 我有一个iterator该集合,并希望增加该集合count的成员 ( ) 所指向的。但是,编译器会抱怨以下消息:structiterator
main.cpp:61:18: error: increment of member \xe2\x80\x98SentimentWord::count\xe2\x80\x99 in read-only object
我怎样才能解决这个问题?
\n\n这是我的代码:
\n\n#include <fstream>\n#include <iostream>\n#include <cstdlib> \n#include <string>\n#include <unordered_set>\n\n\nusing namespace std;\n\n\nstruct SentimentWord {\n string word;\n int count;\n};\n\n\n//hash function and equality definition - needed to used unordered_set with type SentimentWord\nstruct SentimentWordHash {\n size_t operator () (const SentimentWord &sw) const;\n};\n\nbool operator == (SentimentWord const &lhs, SentimentWord const &rhs);\n\n\n\nint main(int argc, char **argv){\n\n\n ifstream fin;\n int totalWords = 0;\n unordered_set<SentimentWord, SentimentWordHash> positiveWords;\n unordered_set<SentimentWord, SentimentWordHash> negativeWords;\n\n\n //needed for reading in sentiment words\n string line;\n SentimentWord temp;\n temp.count = 0;\n\n\n fin.open("positive_words.txt");\n while(!fin.eof()){\n getline(fin, line);\n temp.word = line;\n positiveWords.insert(temp);\n }\n fin.close();\n\n\n //needed for reading in input file\n unordered_set<SentimentWord, SentimentWordHash>::iterator iter;\n\n\n fin.open("041.html");\n while(!fin.eof()){\n totalWords++;\n fin >> line;\n temp.word = line;\n iter = positiveWords.find(temp);\n if(iter != positiveWords.end()){\n iter->count++;\n }\n }\n\n\n for(iter = positiveWords.begin(); iter != positiveWords.end(); ++iter){\n if(iter->count != 0){\n cout << iter->word << endl;\n }\n }\n\n\n return 0;\n\n}\n\n\nsize_t SentimentWordHash::operator () (const SentimentWord &sw) const {\n return hash<string>()(sw.word);\n}\n\n\nbool operator == (SentimentWord const &lhs, SentimentWord const &rhs){\n if(lhs.word.compare(rhs.word) == 0){\n return true;\n }\n return false;\n} \nRun Code Online (Sandbox Code Playgroud)\n\n任何帮助是极大的赞赏!
\n| 归档时间: |
|
| 查看次数: |
1854 次 |
| 最近记录: |