简体   繁体   English

c ++编译错误“要求'_InputIterator std :: __ find_if(_InputIterator,_InputIterator,_Predicate,std :: input_iterator_tag)”

[英]c++ compile error “required from '_InputIterator std::__find_if(_InputIterator, _InputIterator, _Predicate, std::input_iterator_tag) ”

I'm trying to challenge problem 210 on leetcode, but I met a compile error, and I cannot find out what's wrong with my code. 我试图挑战leetcode上的问题210,但是遇到编译错误,并且我无法找出我的代码出了什么问题。

The error goes like 错误像

required from '_InputIterator std::__find_if(_InputIterator, _InputIterator, _Predicate, std::input_iterator_tag) 
[with _InputIterator = std::_Rb_tree_iterator<std::pair<const int, bool> >; 
_Predicate = __gnu_cxx::__ops::_Iter_equals_val<const int>]'

After I copied it into IDE, and tried to run it, I still cannot find what's wrong, because the IDE did not show which line caused the compile error. 将其复制到IDE中并尝试运行后,仍然无法找到问题所在,因为IDE并未显示导致编译错误的行。

The problem requires to find out the ordering of courses one should take to finish all courses given the courses prerequisites stored in c++ vector of pairs. 这个问题要求找出课程的顺序,考虑到课程的先决条件存储在成对的c ++向量中,一个课程应该完成所有课程。

To solve it, I firstly try to turn the vector of pair to a vector> that records the edges from one point(course) to another node(course), and record if a node is a son of other node thus it isn't a root of a tree. 为了解决这个问题,我首先尝试将向量对转换为向量>,该向量记录从一个点(路线)到另一节点(路线)的边,并记录一个节点是否是另一个节点的子节点,因此不是一棵树的根。 I try to find out first if there is any ring in the graph. 我尝试先找出图中是否有环。 Then I use a BFS to search the graph and return the visited order of nodes. 然后,我使用BFS搜索图并返回节点的访问顺序。

The vis map is used to record whether a node has been visited in the search to find rings in the graph. 可见贴图用于记录在搜索中是否已访问节点以在图中找到环。 The globalVis map is used to record if a node is visited in search started by other nodes before, so it is not required to search as a start node again. globalVis映射用于记录在其他节点之前开始的搜索中是否访问过某个节点,因此不需要再次作为起始节点进行搜索。

Please tell me which line of my code caused the compile error, and how to correct it, thank you. 请告诉我代码的哪一行导致了编译错误,以及如何纠正它,谢谢。

class Solution {
public:
    vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
        vector<int> res;
        edges = vector<vector<int>>(numCourses,vector<int>());
        for(int i=0;i<prerequisites.size();i++){
            edges[prerequisites[i].first].push_back(prerequisites[i].second);
            isRoot[prerequisites[i].second]=false;
        }           
        for(int i=0;i<numCourses;i++){
            if(find(globalVis.begin(),globalVis.end(),i)==globalVis.end()){
                globalVis[i]=true;
                vis.clear();
                vis[i]=true;
                if(!checkStartNode(i))
                    return res;
            }
        }
        for(int i=0;i<numCourses;i++){
            if(find(isRoot.begin(),isRoot.end(),i)==isRoot.end()){
                toVisit.push(i);
            }
        }
        vis.clear();
        BFS(res);
        return res;
    }

    void BFS(vector<int>& res){
        while(toVisit.size()>0){
            int node = toVisit.top();
            vis[node]=true;
            toVisit.pop();
            res.push_back(node);
            for(int i=0;i<edges[node].size();i++){
                int nextNode = edges[node][i];
                if(find(vis.begin(),vis.end(),nextNode)==vis.end())
                    toVisit.push(nextNode);                
            }
        }
    }

    bool checkStartNode(int node){
        for(int i=0;i<edges[node].size();i++){
            int nextNode = edges[node][i];                               
            if(find(vis.begin(),vis.end(),nextNode)!=vis.end()){
                globalVis[nextNode]=true;
                if(vis[nextNode]==true)
                    return false;
                else{
                    vis[nextNode]=true;
                    if(!checkStartNode(nextNode))
                        return false;
                    vis[nextNode]=false;
                }
            }                
            else{
                vis[nextNode]=true;
                if(!checkStartNode(nextNode))
                    return false;
                vis[nextNode]=false;
            }
        }

    }

    stack<int> toVisit;
    vector<vector<int>> edges;
    map<int,bool> isRoot;
    map<int,bool> vis;
    map<int,bool> globalVis;
};

Use the std::map::find() instead of std::find() . 使用std::map::find()代替std::find() The map has pairs of key and value as values and you cannot search directly by your value. 该映射具有成对的键和值作为值,并且您不能直接根据值搜索。 And it's always preferred to use the member function of the container class instead of the common algorithms, if provided. 而且,始终首选使用容器类的成员函数而不是常用算法(如果提供)。

Replace all occurrences of find(map.begin(), map.end() value) like: 将所有出现的find(map.begin(), map.end() value)替换为:

if (find(globalVis.begin(), globalVis.end(), i) == globalVis.end()) {
    ...

with: 有:

if (globalVis.find(i) == globalVis.end()) {
    ...

Demo 演示

std::find() could be used to find exact match of (key,value) pair for example: std::find()可用于查找(key,value)对的精确匹配,例如:

if(find(vis.begin(), vis.end(), pair<const int, bool>(nextNode, true))!=vis.end()){
    ...

but in your code you just test whether a key is already in the map and you need to search only by key . 但是在您的代码中,您只需测试地图中是否已存在 ,并且您只需要按键搜索即可。

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

相关问题 C ++ std :: string InputIterator代码转换为C代码 - C++ std::string InputIterator code to C code std :: vector(InputIterator是第一个,InputIterator是最后的)线性时间复杂度? - Is std::vector(InputIterator first, InputIterator last) linear time complexity? 为什么std :: next不接受InputIterator? - Why std::next does not accept InputIterator? range::view::transform 产生一个 InputIterator 阻止使用 std::prev - ranges::view::transform produces an InputIterator preventing the use of std::prev 在C ++中,如何创建一个接受InputIterator的函数? - In C++, how do I create a function that accepts an InputIterator? 使用模板时出现奇怪的错误<class inputiterator>字符串(输入迭代器开始,输入迭代器结束);</class> - Strange Error in using template<class InputIterator> string (InputIterator begin, InputIterator end); 随机访问迭代器会转换为InputIterator吗? - Do random access iterator get converted to InputIterator? 取消引用的InputIterator的地址? istream_iterator的情况 - Address of a dereferenced InputIterator? The case of istream_iterator 用于连续内存的InputIterator? - InputIterator for contiguous memory? 在 C++ 中初始化要作为谓词传递给 std::find_if 的 lambda 函数 - Initialise a lambda function to be passed as a predicate to std::find_if in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM