2014-02-21 153 views
0

如何查看向量或向量(ruleList)列中的元素是否存在于名为ntList(不是向量向量)的另一个向量中。我目前有:查找向量的向量元素是否存在于另一个向量中

for(int j = 0; j < ruleList[0].size(); j++) 
{ 
    for(int i = 0; i < ntList.size(); i++) 
    { 
     if(std::find(ruleList[0][j].begin(), ruleList[0][j].end(), ntList[i]) != ruleList[0][j].end()) 
     { 
      ; 
     } 
     else 
     { 
      errorList.push_back(ERROR1); 
      error = true; 
     } 
    } 
} 

我得到一个错误,但我不完全确定为什么。

error C2678: binary '==' : no operator found which takes a left-hand operand of type 'char' (or there is no acceptable conversion). 

任何帮助,将不胜感激。载体声明:

vector<string> ntList; 
vector< vector<string> > ruleList(100, vector<string> (0, "0")); 
+2

“我得到一个错误,”现在我们还没有。 *发布完整的错误*。 – WhozCraig

+0

错误C2678:二进制'==':找不到操作符找到类型为'char'的左侧操作数(或者没有可接受的转换)。如果我将ntList [i]更改为上面的ntList [i] [0],它会编译得很好,但不会提供我想要的输出。 – user3326306

+0

还发布'ruleList'和'ntList'变量的完整声明。并且请更新*问题*,不要只是将它们放在评论中。 – WhozCraig

回答

0

取决于正是你想要达到的,std::equal什么或(C++ 11只)std::is_permutation可能就足够了。如果您只想检查向量是否具有完全相同的值并且具有相同的大小,那么这些算法已经足够好了。

如果你想做更多,例如在其他地方存储缺失的值,那么用手写循环可能会更好。假设你在这两种情况下处理的std ::载体,而不是使用C++ 11:

for (std::vector<int>::const_iterator iter_rule_list = ruleList[0].begin(); iter_rule_list != ruleList[0].end(); ++iter_rule_list) 
{ 
    int const value = *iter_rule_list; 
    if (std::find(ntList.begin(), ntList.end(), value) == ntList.end()) 
    { 
     // value missing in ntList 
    } 
} 
相关问题