简体   繁体   English

查找向量中向量的元素是否存在于另一个向量中

[英]Finding if elements of a vector of vector is present in another vector

How would I see if the elements in column of vectors or vectors (ruleList) are present in another vector called ntList (not a vector of vectors). 我如何看待向量列或向量列(ruleList)中的元素是否存在于另一个称为ntList的向量(不是向量的向量)中。 I currently have: 我目前有:

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;
        }
    }
}

I'm getting an error, but I'm not entirely sure why. 我遇到了错误,但我不确定原因。

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

Any help would be appreciated. 任何帮助,将不胜感激。 Declarations of vectors: 向量的声明:

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

Depending on what exactly you want to achieve, std::equal or (C++11 only) std::is_permutation may be sufficient. 根据您要实现的目标, std :: equal或(仅C ++ 11) std :: is_permutation可能就足够了。 Those algorithms are good enough if you just want to check if the vectors have exactly the same values stored in them and have the same size. 如果您只想检查向量中存储的向量是否完全相同且大小相同,这些算法就足够了。

If you want to do more, eg storing the missing values somewhere else, then you may be better off with a hand-written loop. 如果您想做更多的事情,例如将丢失的值存储在其他位置,那么使用手写循环可能会更好。 Assuming you are dealing with std::vector in both cases and are not using C++11: 假设您在两种情况下都使用std :: vector并且未使用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
    }
}

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

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