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

Finding if elements of a vector of vector is present in another vector

本文关键字:向量 存在 是否 元素 另一个 查找 一个      更新时间:2023-10-16

我如何查看向量或向量列(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"));

根据你想要实现的目标,std::equal或(仅限C++11)std::is_permutation可能就足够了。如果您只想检查向量中是否存储了完全相同的值并且具有相同的大小,那么这些算法就足够了。

如果你想做更多的事情,例如将缺失值存储在其他地方,那么你最好使用手写循环。假设您在这两种情况下都处理 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
    }
}