Search a pair in unordered_set of pairs from first element in C++

143 Views Asked by At

I want to erase the pair: (x,"whatever"), and i want: 1)find an iterator position of the pair with x as first element 2)after that erase the element in the position of the iterator

typedef pair<int, int> pairs;

// Hash function
struct hashFunction{
   size_t operator()(const pairs &x) const{return x.first ^ x.second;}
};

//Comparator function
struct comp{
    bool operator()(pairs a , pairs b){     
        return a.first < b.first ? a : b;
    }
};

//The function that I want to implement
void delEdge(unordered_set<pairs>, hashFunction, comp> adj[], int u, int v){
    adj[v].erase(/*a function that finds the position of the pair (u,_)*/);
    adj[u].erase(/*a function that finds the position of the pair (v,_)*/);
}

I found that, it exists this function:

find_if(adj[v].begin(), adj[v].end(), [](auto& el){ return el.first == u; });

from: How can I search for a element in unordered_set of pairs on the basis of first element>

But as I have been searching, I found that this find_if complexity is O(n) and not O(1) on average.

Question: How to implement: s.find(x), (s as an unordered_set of pairs) that finds the position of the (x,_)pair in the same complexity as the original find method (in O(1) on average)?

0

There are 0 best solutions below