如何从CStringList中删除重复项?

问题描述 投票:-1回答:2

任何想法如何从CStringList删除重复的条目?

谢谢,

visual-c++ stl mfc atl
2个回答
1
投票
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;
      }
    }
  }
}

0
投票

下面是从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);
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.