0
的名單,我有以下結構的列表結構獨特
struct Element
{
int id;
int groupID;
};
我想知道有多少獨特的羣體有
例如
list<Element> myElements;
Element e;
e.id = 0;
e.groupID = 2;
myElements.push_back(e);
e.id = 1;
e.groupID = 0;
myElements.push_back(e);
e.id = 2;
e.groupID = 2;
myElements.push_back(e);
e.id = 3;
e.groupID = 1;
myElements.push_back(e);
這裏有4種元素但只有3個唯一的組ID ids 0,1,2
我試圖尋找一種有效的方式來做到這一點因爲我的名單越來越大。
我想這
struct groupID_unique {
bool operator() (Element first, Element second)
{ return (first.groupID != second.groupID); }
};
myElements.unique(groupID_unique());
但這返回到我2未重複IDS 0.1
'unique'是錯誤的方式去更多的原因。它只對有序列表非常有用(只有連續的重複被認爲是重複的)並且具有破壞性(它刪除了它找到的重複項) – user4581301