在此代码中,keepSelectedFiles包含5个文本文件。 keepFiles包含keepSelectedFiles中的3个文本文件。我只想比较这两个数组,然后从keepFiles中删除3 keepSelectedFiiles。然后在keepSelectedFiles中只剩下2个文件。该代码不起作用。

 for (int i = 0; i < keepSelectedFiles.GetSize(); i++)

{
    testbox4.AddString(keepSelectedFiles[i]);

    CString s1 = keepSelectedFiles[i];
    CString s2 = keepFiles[i];
    for (int i = 0; i < keepFiles.GetSize(); i++)
    {
        if (s1.Compare(s2) != 0)
        {
            junkFiles.Add(keepSelectedFiles[i]);
        }
    }
}

有人知道如何解决此问题吗?

最佳答案

您可以尝试此代码

for (int i = 0; i < keepSelectedFiles.GetSize(); i++)

{
    bool flag = false;
    CString s1 = keepSelectedFiles[i];
    for (int j = 0; j < keepFiles.GetSize(); j++)// you can use different variable name
    {                                            //so there will be no confusion
        CString s2 = keepFiles[j];
        if (s1.Compare(s2) == 0)           // if s1 equal to s2
        {
            flag = true;
            break;
        }
     }

        if(!flag){
               junkFiles.Add(keepSelectedFiles[i]); // or you can pass s1 here
               keepSelectedFile.RemoveAt(i); // remove index value
               i--;
         }
         else // first empty textbox4 before this loop
            testbox4.AddString(keepSelectedFiles[i]);  // update textbox  value

}

关于c++ - 比较CStringArray元素?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29645987/

10-11 16:05