简体   繁体   中英

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.

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.

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.

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.

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.

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() . 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:

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:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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