任何想法如何从CStringList删除重复的条目?
谢谢,
void RemoveDuplicates(CStringList &lst)
{
for (POSITION pos=lst.GetHeadPosition(); pos; )
{
const CString &strValue = lst.GetNext(pos);
// check next hits in the list
for (POSITION pos2=pos; pos2; )
{
// Save current pointer
POSITION posToDelete = pos2;
const CString &strValue2 = lst.GetNext(pos2);
if (strValue==strValue2)
{
// remove duplicate
lst.RemoveAt(posToDelete);
// There is a chance that we just deleted the follower from the outer loop
if (posToDelete==pos)
pos = pos2;
}
}
}
}
下面是从CStringArray origData中删除重复值的代码,输出将存储在newData数组中:
CStringArray origData, newData;
CString csOld;
for(int i=0; i< origData.GetSize(); i++)
{
if(i == 0 || csOld.Compare(origData[i] != 0)
{
csOld = origData[i];
newData.Add(csOld);
}
}