Проблема с выводом c++ (sort, copy)

162
11 июля 2018, 23:40

Проблема с copy.В "Task 1" все работает, но в "Task 2" после выполнения sort выводит только название группы и ссылается на

void Group::outputStudents(ostream& out)const
    {
        copy(students.begin(), students.end(), ostream_iterator<Stud>(out, " "));
    }

При этом сортировка выполняется. Пишет, что vector iterators incompatible. Вот весь код:

#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;
}

Пример groups.txt ITM$ Ivan:Ivanov:85: Oleg:Olegov:100: $ IFA$ Olga:Olegova:95: $

READ ALSO
Spring Failed to start component

Spring Failed to start component

Начал изучать спринг, создал с помощью инициализатора проект и ввёл в него код-пример с этого сайтаВ итоге в проекте есть один класс:

247
Как правильно назвать классы в терминах MVC/MVP/MVVM/MVPVM?

Как правильно назвать классы в терминах MVC/MVP/MVVM/MVPVM?

В процессе изучения различных вариантов организации кода для пользовательского интерфейса (MVC, MVP, MVVM, MVPVM и того, что этими названиями называют)...

288
Как узнать качество интернета на android?

Как узнать качество интернета на android?

У меня такой вопрос: Как можно узнать качество интернета на телефоне?

199
Потеря пикселей при конвертации

Потеря пикселей при конвертации

Суть задачи: Есть массив субтитров (long start, long end, string text)Мне нужно в зависимости от времени показа изменять ширину (int) для соответствующего...

212