C++ 与';操作员<';尝试在for循环中使用set迭代器时
我正在编写一个程序,从“bible.txt”文件中读取圣经的所有行。然后它将每行处理为单个单词,并将每个单词存储在一个集合和一个多集合中。然后,它发现哪些单词被使用了800到1000次,并将这些单词存储在一个向量中。但是,当我试图创建一个interator来遍历这组单词时,我收到了错误:C++ 与';操作员<';尝试在for循环中使用set迭代器时,c++,iterator,set,C++,Iterator,Set,我正在编写一个程序,从“bible.txt”文件中读取圣经的所有行。然后它将每行处理为单个单词,并将每个单词存储在一个集合和一个多集合中。然后,它发现哪些单词被使用了800到1000次,并将这些单词存储在一个向量中。但是,当我试图创建一个interator来遍历这组单词时,我收到了错误: word_count.cpp: In function ‘void sort_words()’: word_count.cpp:93:62: error: no match for ‘operator<’
word_count.cpp: In function ‘void sort_words()’:
word_count.cpp:93:62: error: no match for ‘operator<’ in ‘p < words.std::set<_Key,
_Compare, _Alloc>::end [with _Key = std::basic_string<char>, _Compare = std::less<std::basic_string<char> >, _
Alloc = std::allocator<std::basic_string<char> >, std::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<std::basic_string<char> >]()’
word\u count.cpp:在函数“void sort\u words()”中:
word_count.cpp:93:62:错误:与(set::iterator p=words.begin();p!=words.end();++p)的“operator不匹配
for(set::iterator p=words.begin();p!=words.end();++p)
试试p!=单词。结束()代码>取而代之。试试p!=单词。结束()代码>相反。啊……你说得对。不知道我为什么会这样想。我已经把那张纸条从我的答案中删除了。啊……你说得对。不知道我为什么会这样想。我已经从我的答案中删除了这个注释。虽然这段代码可以解决这个问题,但如果解释一下它是如何解决的/为什么解决的,答案会更好。请记住,您的答案不仅适用于提出问题的用户,也适用于找到问题的所有其他人。虽然此代码可能会解决问题,但如果能解释问题的原因,答案会更好。请记住,您的答案不仅针对提出问题的用户,也针对找到问题的所有其他人。
for (set<string>::iterator p = words.begin(); p < words.end(); ++p)
#include <iostream>
#include <string>
#include <fstream>
#include <set>
#include <algorithm>
#include <vector>
using namespace std;
class wordWithCount {
public:
string word;
int count;
wordWithCount(string w, int c) : word(w), count(c){}
bool operator<(const wordWithCount& right) const {
if (count != right.count) return count < right.count;
return word < right.word;
}
};
set<string> words;
multiset<string> multiwords;
vector<string> lines;
vector<wordWithCount> selectedWords;
void readLines(char *filename)
{
string line;
ifstream infile;
infile.open(filename);
if (!infile)
{
cerr << filename << " cannot open";
return;
}
getline(infile, line);
while (!infile.eof())
{
lines.push_back(line);
getline(infile, line);
}
infile.close();
}
void process_string (vector<string> lines) {
for (int i = 0; i < lines.size(); i++) {
string line = lines[i];
int found = line.find_first_not_of(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
while (found != string::npos)
{
string word = line.substr(0, found);
if (word.length() > 0)
{
words.insert(word);
multiwords.insert(word);
}
line = line.substr(found + 1);
found = line.find_first_not_of(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
}
}
}
void sort_words() {
int low = 800;
int high = 1000;
for (set<string>::iterator p = words.begin(); p < words.end(); ++p)
{
int count = multiwords.count(*p);
if (count >= low && count <= high)
selectedWords.push_back(wordWithCount(*p, count));
}
sort(selectedWords.begin(), selectedWords.end());
}
void print_words() {
for (int i = 0; i < selectedWords.size(); i++)
{
cout << selectedWords[i].word << "\t" << selectedWords[i].count;
}
}
int main() {
readLines("bible.txt");
process_string(lines);
sort_words();
print_words();
return 0;
}
for (set<string>::iterator p = words.begin(); p != words.end(); ++p)