Search code examples
c++iteratorstdset

Searching in std::set without iterator C++


I have std::set which contains come int values. Now i use iterator to find out whether set contans value.

But my application use this search very ofter and search using iterator too slow, can i do something like that:

std::set<int> fdsockets;

void myfunc(int fd)
{
    if(fdsockets[fd] != fdsockets.end())
    {
            // my code
    }
}

But i have error when compile using G++

no match for 'operator[]' in 'fdsockets[fd]'

Maybe i can use something instead of std::set?

Thanks!


Solution

  • std::unorered_set or an ordered vector with binary search are more effective for simple membership test. If the maximum value of intergers is low a lookup table might be an alternative.