-1

知道如何从 CStringList 中删除重复的条目吗?

谢谢,

4

2 回答 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;
      }
    }
  }
}
于 2013-10-29T13:32:04.123 回答
0

下面是从 CStringArray origData 中删除重复值的代码,输出将存储在“csaNew”数组中:

    CStringArray csaOld, csaNew;
    bool bAdd = false;
    csaOld.Add(L"A");
    csaOld.Add(L"A");
    csaOld.Add(L"B");
    csaOld.Add(L"B");
    csaOld.Add(L"C");
    csaNew.Add(csaOld[0]);
    for (int i = 1; i < csaOld.GetSize(); i++)
    {
        bAdd = true;
        for (int j = 0; j < csaNew.GetSize(); j++)
        {
            if (csaOld[i].Compare(csaNew[j]) == 0)
            {
                bAdd = false;
                break;
            }
        }

        if (bAdd)
            csaNew.Add(csaOld[i]);
    }
于 2020-04-12T03:34:50.550 回答