I have a class CPerson
class CPerson
{
private:
string name;
string egn;
public:
CPerson() {};
CPerson(string getname, string getegn)
{
name = getname;
egn = getegn;
}
bool operator < (const CPerson &obj)
{
if (egn < obj.egn)
return true;
else
return false;
}
bool operator == (const CPerson &obj)
{
if (egn == obj.egn)
return true;
else
return false;
}
Then I have a second class CCity which has a vector with CPerson objects.
class CCity
{
public:
string city_name;
vector <CPerson> people;
public:
CCity() {};
CCity(string filename)
{
string name, egn;
ifstream file(filename);
file >> city_name;
while (file >> name >> egn)
{
people.push_back(CPerson(name,egn));
}
}
void remove_duplicates()
{
sort(people.begin(), people.end());
people.erase(unique(people.begin(), people.end()-1));
}
};
I have already overloaded == and < which should be neccessary but when I use the remove_duplicates function the duplicates are still there after I check the vector contents. Here is the contents of the file I use in the CCity constructor:
Varna
EGN0000001 a
EGN0000001 b
EGN0000002 c
EGN0000003 d
EGN0000004 e
EGN0000004 f
EGN0000005 g
EGN0000006 h
EGN0000001 i
The error was that I was inserting the what was supposed to be the egn into name and the name in the egn. This:
CCity(string filename)
{
string name, egn;
ifstream file(filename);
file >> city_name;
while (file >> name >> egn)
{
people.push_back(CPerson(name,egn));
}
}
Should be this:
CCity(string filename)
{
string name, egn;
ifstream file(filename);
file >> city_name;
while (file >> egn>> name)
{
people.push_back(CPerson(name,egn));
}
}