сортировку по алфавиту с++
необходимо сделать сортировку по алфавиту, 10 номер, сколько не пытаюсь выдает ошибку, врубиться не могу :(
main.cpp: In function ‘int main()’:
main.cpp:212:52: error: no match for ‘operator==’ (operand types are ‘int’ and ‘const std::__cxx11::basic_string’)
212 | while (in >> tos) if (tos.category == it->first && tos.title == it->second && tos.category <= n) { cout << tos << " "; break; }
| ~~~~~~~~~~~~ ^~ ~~~~~~~~~
| | |
| int const std::__cxx11::basic_string<char>
Объявление типа для переменной, участвующей в сравнении:
struct Toys {
string title;
float price;
int amount;
int category;
int manufacturercode;
friend ostream& operator << (ostream& out, const Toys& a_o) {
out << a_o.title << ' ' << a_o.price << ' ' << a_o.amount << ' ' << a_o.category << ' ' << a_o.manufacturercode << ' ' << endl;
return out;
}
friend istream& operator >> (istream& in, Toys& a_o) {
in >> a_o.title >> a_o.price >> a_o.amount >> a_o.category >> a_o.manufacturercode;
return in;
}
};
Фрагмент кода с ошибкой:
multiset<pair<string, float>> un;
Toys tos; Manufacturer mnf;
Toys minA, maxA;
for (auto it = un.begin(); it != un.end(); it++) {
in.open(tys);
while (in >> tos) if (tos.category == it->first && tos.title == it->second && tos.category <= n) { cout << tos << ; break; }
in.close();
}
Ответы (1 шт):
Рассмотрим сравнение
tos.category == it->first
it — итератор для multiset<pair<string, float>> un;. Т.е. it->first — это, вообще-то, string. tos.category, если посмотреть определение — int. Т.е. вы пытаетесь сравнивать string и int. Ничего не смущает?
Во втором сравнении в той же строке та же ошибка.
Просто недосмотр. Стоили из-за этого сразу бежать на ruSO, тем более что посмотрите — компилятор в сообщении об ошибке выдал вам ошибочный фрагмент, подчеркнул сравниваемые значения и указал их типы! Что тут может быть непонятно?