【问题标题】:Finding if elements of a vector of vector is present in another vector查找向量的向量的元素是否存在于另一个向量中
【发布时间】:2014-02-21 20:31:30
【问题描述】:

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

【问题讨论】:

  • “我遇到了一个错误”现在我们没有。 发布完整的错误
  • 错误 C2678: 二进制 '==' : 未找到采用 'char' 类型的左侧操作数的运算符(或没有可接受的转换)。如果我将上面的 ntList[i] 更改为 ntList[i][0],它编译得很好,但没有给出我想要的输出。
  • 同时发布ruleListntList 变量的完整声明。并且请更新问题,不要只是将它们放在评论中。
  • @user3326306 您的错误非常稀疏,或者这不是完整的错误。编译器不会无缘无故向你吐出文本,错误文本是有意义的!

标签: c++ vector


【解决方案1】:

根据您想要实现的具体目标,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
    }
}

【讨论】:

    猜你喜欢
    • 2014-05-21
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多