2014-09-21 41 views
0

我想比较list<string>vector<string>中的字符串元素。比较列表中的字符串元素<string>和向量​​<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不起作用。

+0

是图案列出实际正则表达式,或者是他们根本串的两个容器和你要计算差? – WhozCraig 2014-09-21 10:05:32

+0

你需要有一个变量来告诉你,如果你发现内循环内的匹配。然后在该循​​环之外记录结果,如果找不到匹配的话。 – 2014-09-21 10:05:53

+0

@WhozCraig:这两个容器都只是字符串,我试图用regex_search比较它们(成功的比较工作正常,但我想也赶上那些不匹配的文件)。 List容器是使用sort()和unique()函数 – 2014-09-21 10:09:56

回答

1

听起来像解决方案是“记住”,如果你找到了匹配。

所以,在伪代码:

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 ... 
    } 
} 
相关问题