Visual c++ 如何从CStringList中删除重复条目?

Visual c++ 如何从CStringList中删除重复条目?,visual-c++,stl,mfc,atl,Visual C++,Stl,Mfc,Atl,知道如何从CStringList中删除重复条目吗 谢谢,下面是从CStringArray origData中删除重复值的代码,输出将存储在“csaNew”数组中: void RemoveDuplicates(CStringList &lst) { for (POSITION pos=lst.GetHeadPosition(); pos; ) { const CString &strValue = lst.GetNext(pos); // check ne

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


谢谢,

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

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 csaOld,csaNew;
bool-bAdd=false;
csaOld.添加(L“A”);
csaOld.添加(L“A”);
csaOld.添加(L“B”);
csaOld.添加(L“B”);
csaOld.添加(L“C”);
csaNew.Add(csaOld[0]);
对于(int i=1;i
RemoveAt
,与任何其他条目相同。什么是“重复条目”?到目前为止,您尝试了什么?获取错误C2440:“正在初始化”:无法从“struct\uu POSITION*”转换为“int”。您有一个旧的编译器。改变自动档位。我将在我的答案中更改它。请在你的答案中添加一些解释,以便其他人可以从中学习。添加了一些描述,我认为代码是自解释的。该代码如何检查“重复条目”?你能检查一下你的代码如何处理列表“A”、“B”、“A”吗?谢谢你的消息,我对代码做了一些修改。它运行良好,经过测试。
    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]);
    }