I'm building a data structure to keep track of count and rank of elements. I have a set of a struct I made and when I'm trying to modify the members in the struct with the range based for loop I get this error:
error: cannot assign to variable 'it' with const-qualified type 'const item &'
I already overloaded the < operator. What do I have to do more to make this work?
#include <iostream>
#include <stdio.h>
#include <set>
using namespace std;
struct item {
int val, count;
item(int v, int c) : val{v}, count{c} {}
};
bool operator<(const item &a, const item &b) {
return a.val < b.val;
}
class DS {
set<item> S;
public:
void notWorking(int x) {
for (auto &it : S) {
if (it.val < x) it.count++;
}
}
};
int main() {
return 0;
}