简体   繁体   中英

No match for 'operator<' when trying to use set iterator in for loop

I am writing a program that reads in all of the lines of the bible from a file 'bible.txt.' It then processes each line into individual words and stores each word in a set and a multiset. It then finds which words are used between 800 and 1000 times and stores those words in a vector. However, when I try to create an interator to go through the set of words, I receive the error:

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> >]()’

Here is the line that it has a problem with:

for (set<string>::iterator p = words.begin(); p < words.end(); ++p)

Here is the full code:

#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)

Try p != words.end(); instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM