简体   繁体   English

按名称查找重复项时出错

[英]Error while finding duplicates by name

I have written a program which would find duplicates by name. 我编写了一个程序,该程序可以按名称查找重复项。 Eg:Lets say we have file a.txt,b.txt,c.txt in various directories.I need to find the files which are duplicated only by their name.The output would be the filename which are present in various directories. 例如:假设我们在各个目录中都有文件a.txt,b.txt,c.txt。我需要查找仅按名称重复的文件。输出将是各个目录中存在的文件名。

Code: 码:

fs::path tempHolder;
int isDuplicate=0;//To check whether duplicate or not.
vec::const_iterator tempit (v.begin());//i already have vector v which has all the filenames
for (vec::const_iterator it (v.begin()); it != v.end();)
{

   tempHolder=*it;
   isDuplicate=0;
   tempit=it;
   while((++tempit)!=v.end() && tempHolder==(*(++it)))
   {
      isDuplicate=1;
   }
   if(isDuplicate==1)
   {
      cout<<"Duplicate:"<<tempHolder<<endl;
   }
 }

The problem is the for loop is going recursively but it prints the duplicate filename properly.Thanks. 问题是for循环递归进行,但可以正确打印重复的文件名。

This loop seems strange to me: 这个循环对我来说似乎很奇怪:

tempit=it;
while((++tempit)!=v.end() && tempHolder==(*(++it)))
{
    isDuplicate=1;
}

If it is the last valid iterator of the container, the first condition will be false, so short-circuit evaluation will cause ++it to never be executed. 如果it是容器的最后一个有效迭代器,则第一个条件为false,因此短路评估将导致++it永远不会执行。 The loop will run indefinitely. 循环将无限期运行。

while((++tempit)!=v.end() && tempHolder==*tempit)

should work. 应该管用。

while((++tempit)!=v.end() && tempHolder==(*(++it)))

You manipulate your iterator that you use in your big loop (it) Better: 您可以操纵在大循环中使用的迭代器(更好):

while((++tempit)!=v.end() && tempHolder==(*tempit))

If I understand your code correctly. 如果我正确理解您的代码。

Edit: And your it needs to be incremented in your first loop. 编辑:并且您需要在第一个循环中增加it

I personally would rather write something like that: 我个人更愿意这样写:

while ((++tempit)!= v.end())
    if (*tempit = *it)
    {
        duplicate = true;
        break;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM