error: no match for 'operator<' (operand types are 'const Something' and 'const Something')
So did you create that operator< overload? You need a comparison operator to use your class with a std::set.
Do you realize that with a std::set operator= is copying the the rhs object to the lhs object? It is not "adding" an additional element to the set. Did you look up the documentation for std::set? How is operator= and operator[] implemented for this class?
I understand, I was just reducing the lines of code as much as possible to just illustrate the question.
I'm not overloading the operator because I didn't need to with vectors. Why is it different with sets?
for (auto &sth : myvector){
if (sth.name=="some name")
sth.code=0;
}
However, this is not possible with set or unordered_set. Why is that?
So, two questions:
Is there a way I can iterate over structs in a set using a range-based for?
What's different with set or unordered_set that doesn't allow me to take a copy by reference when I use a range-based for?
A set is "sorted" by a condition, that is why you need the operator< to determine how to sort the Something. Until you implement the operator< you will continue to have problems.
With a vector I can do:
You should be able to iterate over a set as well. However once an element is placed within a set it can not be changed, it is const.
In a set, the value of an element also identifies it (the value is itself the key, of type T), and each value must be unique. The value of the elements in a set cannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container.