Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在二维向量中找到结构元素?_C++_Search_Vector_Stl_Find - Fatal编程技术网

C++ 如何在二维向量中找到结构元素?

C++ 如何在二维向量中找到结构元素?,c++,search,vector,stl,find,C++,Search,Vector,Stl,Find,我有一个结构: struct node { string val; int count; }; 我用以下方式定义了我的向量: typedef std::vector<node> StringVector; typedef std::vector<StringVector> StringVector2D; 我用过这个 StringVector::iterator it = find(twoD[0].begin().val, twoD[0].end().v

我有一个结构:

struct node
{
    string val;
    int count;
};
我用以下方式定义了我的向量:

typedef std::vector<node> StringVector;
typedef std::vector<StringVector> StringVector2D;
我用过这个

StringVector::iterator it = find(twoD[0].begin().val, twoD[0].end().val, "ben");

但它没有起作用。谢谢你的建议

编辑

我定义了自己的搜索:

  struct find_word
    {
        string val;
        find_word(string val) : val(val) {}
        bool operator () ( const find_word& m ) const
        {
            return m.val == val;
        }
};
在这里叫它:

StringVector::iterator it = find_if(twoD[0].begin()->val, twoD[0].end()->val, find_word("ben"));

但无法使其工作。

您需要更改
find\u if
的comparer functor

struct find_word {
    string val;
    find_word(string val) 
      : val(val) {}
    bool operator()(const node& m) const { return m.val == val; }
}; //                     ^^^^ the change is here 
使用
find_if
版本如下:

StringVector::iterator it = find_if(twoD[0].begin(), twoD[0].end(), find_word("ben"));
//                              the change is here ^     and here ^

find_的比较器函子,如果
操作符()中作为参数接收要查找的容器元素。在这种情况下,
twoD[0]。begin()
twoD[0]。end()
允许您访问内部向量的元素,并且接收的参数是内部向量
节点中元素存储的类型
twoD[0]
的元素是
节点。您正在搜索字符串。
节点与字符串匹配意味着什么?编译器怎么会知道这些?@IgorTandetnik这是我的问题。在这种情况下,我们应该怎么做?为什么需要一个将自身作为参数的比较器?您想将字符串与
节点
进行比较,否?@igortandtnik有一个键入错误,我修复了它。是的,我正在寻找一个字符串与节点内的val进行比较。
find\u word::operator()
仍然将
const find\u word&
作为参数,并且没有以任何方式提及
node
StringVector::iterator it = find_if(twoD[0].begin()->val, twoD[0].end()->val, find_word("ben"));
struct find_word {
    string val;
    find_word(string val) 
      : val(val) {}
    bool operator()(const node& m) const { return m.val == val; }
}; //                     ^^^^ the change is here 
StringVector::iterator it = find_if(twoD[0].begin(), twoD[0].end(), find_word("ben"));
//                              the change is here ^     and here ^