I wrote this. doesn\'t matter what am I supposed to do with it. It just doesn\'t
ID: 639084 • Letter: I
Question
I wrote this. doesn't matter what am I supposed to do with it. It just doesn't complie! Using G++ 4.9
In the line which I insert data into the set I get error! commenting out that line will solve the problem. But, why am I getting error?
#include <iostream>
#include <set>
#include <string>
using namespace std;
struct data {
string s;
int x, y;
};
int main() {
int n;
cin >> n;
set <data> Set;
for (int i = 0; i < n; i++) {
data temp;
cin >> temp.s >> temp.x >> temp.y;
Set.insert(temp);
}
// Prints the size
cout << Set.size() << endl;
int cnt = 0;
set <data> :: iterator it;
for (it = Set.begin(); it != Set.end(); ++it)
cout << (*it).s << (++cnt % 5 ? " " : " ");
return 0;
}
Explanation / Answer
A type must refine relational operators (<, > ==) to be used with std::set. std::set builds a binary search tree inside, and uses these operators for comparison.