The problem with copy . In "Task 1" everything works, but in "Task 2" after the execution of sort displays only the name of the group and refers to
void Group::outputStudents(ostream& out)const { copy(students.begin(), students.end(), ostream_iterator<Stud>(out, " ")); } In this case, sorting is performed. Writes that vector iterators are incompatible. Here is the whole code:
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <algorithm> #include<functional> #include<numeric> #include <iterator> #include <ctime> #include <vector> #include <fstream> #include <sstream> using namespace std; struct Stud { string firstName; string secondName; int averageScore; }; ostream& operator <<(ostream& out, const Stud& stud); istream& operator >>(istream& in, Stud& stud); class Group { private: string nameGroup; vector<Stud> students; public: Group(); Group(const Group&obj); Group(const string& nameGroup_); ~Group(); const string& getNameGroup()const; int getNumberStudents()const; void outputStudents(ostream& out)const; void setNameGroup(const string& nameGroup_); const vector<Stud>& getStud() const; void setStud(const vector<Stud>& students_); Group& operator =(const Group& obj); bool operator <(const Group& arg2) const; }; Group::Group() : nameGroup("ITINF-17"){} Group::Group(const Group&obj) { nameGroup = obj.nameGroup; students = obj.students; } Group::Group(const string& nameGroup_) : nameGroup(nameGroup_){} Group::~Group(){ students.clear(); } const string& Group::getNameGroup()const { return nameGroup; } int Group::getNumberStudents()const { return students.size(); } string getNameStudent(const Stud& a) { string name = a.firstName + ' ' + a.secondName; return name; } void Group::setStud(const vector<Stud>& students_) { students = students_; }; void Group::setNameGroup(const string& nameGroup_) { nameGroup = nameGroup_; } void Group::outputStudents(ostream& out) const { copy(students.begin(), students.end(), ostream_iterator<Stud>(out, " ")); } bool cmpByQuantityStudents(const Group& a, const Group& b) { return a.getNumberStudents() < b.getNumberStudents(); } Group& Group::operator =(const Group& obj) { if (this == &obj) return (*this); this->~Group(); nameGroup = obj.nameGroup; students = obj.students; } bool Group::operator < (const Group& arg2) const { return (this->nameGroup < arg2.nameGroup); } ostream& operator <<(ostream& out, const Stud& stud) { out << "\t" << stud.firstName << "\t" << stud.secondName << "\t" << stud.averageScore << "\n"; return out; } istream& operator >>(istream& in, Stud& stud) { getline(in, stud.firstName, ':'); getline(in, stud.secondName, ':'); string strTmp; getline(in, strTmp, ':'); stud.averageScore = atoi(strTmp.c_str()); return in; } ostream& operator <<(ostream& out, const Group& group) { out << group.getNameGroup(); group.outputStudents(out); return out; } istream& operator >>(istream& in, Group& group) { string strTmp; getline(in, strTmp, '$'); group.setNameGroup(strTmp); getline(in, strTmp, '$'); stringstream streamStudents(strTmp); vector<Stud> stud; copy(istream_iterator<Stud>(streamStudents), istream_iterator<Stud>(), back_inserter(stud)); group.setStud(stud); return in; } int main() { ifstream fin("groups.txt"); if (!fin) { cerr << "File not found"; system("pause"); return 0; } vector<Group> groups; copy(istream_iterator<Group>(fin), istream_iterator<Group>(), back_inserter(groups)); cout << "Task 1:\n"; copy(groups.begin(), groups.end(), ostream_iterator<Group>(cout,"\n")); ofstream fout("result.txt"); copy(groups.begin(), groups.end(), ostream_iterator<Group>(fout, "\n")); fin.close(); fout.close(); cout << '\n' << "Task 2: \n"; cout << "Sort by name group: \n"; sort(groups.begin(), groups.end()); copy(groups.begin(), groups.end(), ostream_iterator<Group>(cout )); cout << '\n'; cout << "Sort by students: \n"; sort(groups.begin(), groups.end(), cmpByQuantityStudents); copy(groups.begin(), groups.end(), ostream_iterator<Group>(cout)); system("pause"); return 0; } Example groups.txt ITM $ Ivan: Ivanov: 85: Oleg: Olegov: 100: $ IFA $ Olga: Olegova: 95: $
this->~Group();causes undefined behavior. - VTT