#include #include using namespace std; class Student { unsigned id; string name; public: Student() = delete; Student(unsigned arg1, string arg2 = "") : id(arg1), name(arg2) {} Student(const Student&) = default; bool operator<(const Student& obj) const { return id < obj.id; } bool operator==(const Student& obj) const { return id == obj.id; } friend ostream& operator<<(ostream& out, const Student& obj) { out << obj.id << " " << obj.name; return out; } }; ostream& operator<<(ostream& out, const multiset& stu) { for (auto it = stu.cbegin(); it != stu.cend(); ++it) { out << *it << endl; } return out; } int main() { multiset Students; Students.insert({117,"John"}); Students.insert({124,"Paul"}); Students.insert({102,"George"}); Students.insert({106,"Ringo"}); Students.insert({223,"Peter"}); Students.insert({203,"Paul"}); Students.insert({243,"Mary"}); cout << "Students.size() = " << Students.size() << endl; cout << Students << endl; multiset::iterator msIt; msIt = Students.insert({309,"Mick"}); cout << "New student: " << *msIt << endl; msIt = Students.insert({117,"Elvis"}); cout << "Another new student: " << *msIt << endl << endl; cout << Students << endl; // Check count cout << "count of 117 = " << Students.count(117) << endl; // cout << "# of Paul = " << Students.count("Paul") << endl; // ERROR cout << endl; // check find multiset::const_iterator cMsIt; cMsIt = Students.find(124); cout << "find 124: " << *cMsIt << endl; // cout << *(Students.find(107)) << endl; // ERROR ++cMsIt; cout << *cMsIt << endl; ++cMsIt; cout << *cMsIt << endl; int id = 125; cMsIt = Students.find(id); // cout << *cMsIt << endl; // CRASH if (cMsIt == Students.end()) cout << "Can't find " << id << endl << endl; // equal_range cout << "equal_range 117" << endl; auto twoIterators = Students.equal_range(117); cout << *twoIterators.first << endl << *twoIterators.second << endl << endl; cout << "equal_range 203" << endl; twoIterators = Students.equal_range(203); cout << *twoIterators.first << endl << *twoIterators.second << endl << endl; cout << "equal_range 204" << endl; twoIterators = Students.equal_range(204); cout << *twoIterators.first << endl << *twoIterators.second << endl << endl; if (twoIterators.first == twoIterators.second) cout << "204 not found" << endl << endl; // erase cout << "Erase 117: " << Students.erase(117) << endl; cout << "Erase 118: " << Students.erase(118) << endl << endl; cout << Students << endl; }