gpt4 book ai didi

c++ - QString::startWith() 和 QStringList::removeAll() 未按预期运行

转载 作者:行者123 更新时间:2023-11-30 03:05:53 25 4
gpt4 key购买 nike

我有两个列表。两者包含相同的值:

 QStringList filePaths;
filePaths.append("C:/backup");
filePaths.append("C:/download/file1");
filePaths.append("D:");
filePaths.append("C:/program");
filePaths.append("C:/download");

QStringList refinedPaths;

int size = filePaths.size();

for(int i = 0; i < size; i++)
{
refinedPaths.append(filePaths.at(i));
}

for(int i = 0; i < size; i++)
{
QString str1 = filePaths.at(i);

for(int j = 0; j < size; j++)
{
QString str2 = filePaths.at(j);

if(str2 == str1)
{
continue;
}

if(str2.startsWith(str1))
{
refinedPaths.removeAll(str2);
}
}
}

我期望发生的是: * 遍历列表中的字符串,比较列表中的每个项目。 * 如果string1以string2开头(因此string2是string1的父目录) * 从“精炼”字符串列表中删除该字符串。

但是,发生的事情是 if(str2.startsWith(str1)) 每次都返回 true,而 refinedPaths.removeAll(str2); 却没有删除列表中的任何字符串。

有什么想法吗?

最佳答案

下面的片段根据需要合理化了列表。

   foreach (const QString& path, filePaths)
{
foreach (const QString& other, filePaths)
{
if (other != path && other.startsWith(path))
filePaths.removeOne (other);
}
}

关于c++ - QString::startWith() 和 QStringList::removeAll() 未按预期运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7334909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com