Проблема с 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: $
Кофе для программистов: как напиток влияет на продуктивность кодеров?
Рекламные вывески: как привлечь внимание и увеличить продажи
Стратегії та тренди в SMM - Технології, що формують майбутнє сьогодні
Выделенный сервер, что это, для чего нужен и какие характеристики важны?
Современные решения для бизнеса: как облачные и виртуальные технологии меняют рынок
Начал изучать спринг, создал с помощью инициализатора проект и ввёл в него код-пример с этого сайтаВ итоге в проекте есть один класс:
В процессе изучения различных вариантов организации кода для пользовательского интерфейса (MVC, MVP, MVVM, MVPVM и того, что этими названиями называют)...
У меня такой вопрос: Как можно узнать качество интернета на телефоне?
Суть задачи: Есть массив субтитров (long start, long end, string text)Мне нужно в зависимости от времени показа изменять ширину (int) для соответствующего...