C/C++

Удалить дубликаты в структуре данных лист (ошибка в коде)

Void DeleteDublicats(std::list& employees)
{
size_t counter = 0;
unsigned int tmpday,tmpmonth,tmpyear;
std::list::iterator it1, it2;
for (it1 = employees.begin(); it1 != employees.end(); it1++)
{
tmpday = it1->birthdate.day;
tmpmonth = it1->birthdate.day;
tmpyear = it1->birthdate.day;
for (it2 = employees.begin(); it2 != employees.end(); it2++)
{
if (tmpday == it2->birthdate.day)
{
counter++;
if (counter == 2)
{
if (tmpmonth == it2->birthdate.month)
{
if (tmpyear == it2->birthdate.year)
{
employees.erase(it1);
}
}
}
}
}
}
}
#include <string>
#include <tuple>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
struct Data {
using value_type = tuple<string, string, string>;
Data() = default;
Data(const value_type& value)
: name(get<0>(value)), surname(get<1>(value)), birthdate(get<2>(value)) {}
Data(value_type&& value)
: name(get<0>(value)), surname(get<1>(value)), birthdate(get<2>(value)) {}
string name;
string surname;
string birthdate;
value_type get_data()const { return { name, surname, birthdate }; }
friend bool operator<(const Data& a, const Data& b) {
if (a.surname < b.surname) return true;
if (a.surname == b.surname && a.name < b.name) return true;
if (a.surname == b.surname && a.name == b.name && a.birthdate < b.birthdate) return true;
return false;
}
friend ostream& operator<<(ostream& out, const Data& date) {
cout << date.surname << ' ' << date.name << ' ' << date.birthdate;
return out;
}
};
Data::value_type input() {
cout << "Фамилия: ";
string surname;
getline(cin, surname);
cout << "Имя: ";
string name;
getline(cin, name);
cout << "Дата рождения (гггг-мм-дд): ";
string birthdate;
getline(cin, birthdate);
return { name, surname, birthdate };
}
void load(set<Data>& data) {
vector<Data> box{
Data({ "Степан", "Антипов", "1987-08-30"}),
Data({ "Анна", "Григорьева", "1995-11-13" }),
Data({ "Фёдор", "Васильев", "2000-01-29" }),
Data({ "Илья", "Антипов", "1954-05-24" }),
Data({ "Инна", "Иванова", "1988-07-18" }),
Data({ "Алла", "Григорьева", "1995-11-13" }),
Data({ "Анна", "Григорьева", "1995-11-13" }),
Data({ "Алла", "Григорьева", "1995-11-10" })
};
for (const auto& item : box) data.insert(item);
}
int main() {
system("chcp 1251 > nul");
set<Data> box;
load(box);
for (const auto& item : box) cout << item << '\n';
cout.put('\n');
for (auto i = 0; i < 5; ++i) box.insert(input());
for (const auto& item : box) cout << item << '\n';
cout.put('\n');
system("pause > nul");
}
Нурлан Муса (Мусин)
Нурлан Муса (Мусин)
70 431
Лучший ответ
Дмитрий Картунен спасибо!
но не могли бы вы обьяснить вот эту часть построчно?
был бы премного благодарен
using value_type = tuple;
Data() = default;
Data(const value_type& value)
: name(get<0>(value)), surname(get<1>(value)), birthdate(get<2>(value)) {}
Data(value_type&& value)
: name(get<0>(value)), surname(get<1>(value)), birthdate(get<2>(value)) {}
Проще использовать std::set. В нём не может быть дубликатов. Но для этого надо задать, как сравнивать объекты на больше-меньше.
Алексей Сенько
Алексей Сенько
58 065
Дмитрий Картунен задачка в том чтобы сделать это с листами
а unique не будет работать так как в стуктуре вот что :
class FIO
{
public:
std::string name;
std::string surname;
std::string patranomic;
};
class Date
{
public:
unsigned year : 12;
unsigned month : 6;
unsigned day : 6;
};
class employee
{
public:

FIO fio;
Date birthdate;

};