Reputation: 1447
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!
Upvotes: 3
Views: 393
Reputation: 18915
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.
Upvotes: 4
Reputation: 47498
If you don't need the iterator that set::find
returns (you're just testing existence, not actually going to access the fdsocket), here's an alternative:
if(fdsockets.count(fd))
{
// my code
}
Upvotes: 0
Reputation: 32428
It sounds like you want set::find()
if( fdsockets.find(fd) != fdsockets.end() )
{
// my code
}
Upvotes: 4
Reputation: 20759
There is no operator[] in std::set.
You probably mean
if(fdsockets.find(fd) != fdsockets.end())
Upvotes: 2