比较list <string>和vector <string> </string> </string>中的字符串元素

时间:2014-09-21 09:51:06

标签: c++ regex string

我想比较list<string>vector<string>中的字符串元素。

这样的代码工作正常:

list<string> FileNamesPatternsList; // In this list there are about 100 files
vector<string> FilesToBeSearched;   // In this vector there are about 300 files

list<string>::iterator compareIterator;
vector<string>::iterator compareIterator2;

for( compareIterator = FileNamesPatternsList.begin(); compareIterator != FileNamesPatternsList.end(); compareIterator++){
for( compareIterator2 = FilesToBeSearched.begin(); compareIterator2 != FilesToBeSearched.end(); compareIterator2++)
            {
                smatch result;
                if(regex_search(*compareIterator2,result,regex(*compareIterator))){
                MyFilewithResults << "File: " << result[0] << "fit to" << *compareIterator2 << endl;
                }
            }

    }

虽然结果只有匹配的元素存储到txt文件(MyFilewithResults)。

如何存储不匹配的结果?添加if()...else在这种情况下无效。

1 个答案:

答案 0 :(得分:1)

听起来像解决方案是&#34;记住&#34;如果你找到了匹配。

所以,在伪代码中:

for(every element)
{
  found = false
  for(each thing to match with)
  {
    if (is a match)
    {
       found = true;
       ... do other stuff here ... 
    }
  }
  if (!found)
  {
     ... do whatever you do if it wasn't in the list ...
  }
}