删除对象向量中的重复项
Removing duplicate items in a vector of objects
我有一个class个人
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;
}
然后我有第二个 class CCity,它有一个带有 CPerson 对象的向量。
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));
}
};
我已经重载了 == 和 < 这应该是必要的但是当我使用 remove_duplicates 函数时,在我检查矢量内容后重复项仍然存在。这是我在 CCity 构造函数中使用的文件的内容:
Varna
EGN0000001 a
EGN0000001 b
EGN0000002 c
EGN0000003 d
EGN0000004 e
EGN0000004 f
EGN0000005 g
EGN0000006 h
EGN0000001 i
请参考这个link。我认为你正在做的是擦除最后一个独特的元素。
作为答案,我希望这段代码能起作用:
void remove_duplicates()
{
sort(people.begin(), people.end());
vector<int>::iterator it = unique(people.begin(), people.end());
people.resize( std::distance(myvector.begin(),it));
}
希望对你有帮助
错误是我将本应是 egn 的内容插入到 name 中,而将 name 插入到 egn 中。
这个:
CCity(string filename)
{
string name, egn;
ifstream file(filename);
file >> city_name;
while (file >> name >> egn)
{
people.push_back(CPerson(name,egn));
}
}
应该是这样的:
CCity(string filename)
{
string name, egn;
ifstream file(filename);
file >> city_name;
while (file >> egn>> name)
{
people.push_back(CPerson(name,egn));
}
}
我有一个class个人
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;
}
然后我有第二个 class CCity,它有一个带有 CPerson 对象的向量。
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));
}
};
我已经重载了 == 和 < 这应该是必要的但是当我使用 remove_duplicates 函数时,在我检查矢量内容后重复项仍然存在。这是我在 CCity 构造函数中使用的文件的内容:
Varna
EGN0000001 a
EGN0000001 b
EGN0000002 c
EGN0000003 d
EGN0000004 e
EGN0000004 f
EGN0000005 g
EGN0000006 h
EGN0000001 i
请参考这个link。我认为你正在做的是擦除最后一个独特的元素。 作为答案,我希望这段代码能起作用:
void remove_duplicates()
{
sort(people.begin(), people.end());
vector<int>::iterator it = unique(people.begin(), people.end());
people.resize( std::distance(myvector.begin(),it));
}
希望对你有帮助
错误是我将本应是 egn 的内容插入到 name 中,而将 name 插入到 egn 中。 这个:
CCity(string filename)
{
string name, egn;
ifstream file(filename);
file >> city_name;
while (file >> name >> egn)
{
people.push_back(CPerson(name,egn));
}
}
应该是这样的:
CCity(string filename)
{
string name, egn;
ifstream file(filename);
file >> city_name;
while (file >> egn>> name)
{
people.push_back(CPerson(name,egn));
}
}