Re:Why should I have written ZeroMQ in C, not C++ (2012)
by rwbt on Thursday December 13, 2018 @11:33AM
(#18673592)
Yes, I'd need to check for end iterator in vector. But my point still stands - C++ does have some weird ergonomics.
Re:Why should I have written ZeroMQ in C, not C++ (2012)
by LgWoodenBadger on Thursday December 13, 2018 @09:35AM
(#18672538)
but isn't find() an O(n) operation already?
Re:Why should I have written ZeroMQ in C, not C++ (2012)
by int_19h on Thursday December 13, 2018 @09:09PM
(#18677852)
Indeed! Which is why find() is not a member function on vector (so my snippet above is wrong - I just missed that part). It's a global function, so you need to do:
if (find(x, v.begin(), v.end()) != v.end()) ...
For maps and sets, find is a member function though. Same principle - if it is implemented in some optimized way, the class provides it directly, but not otherwise.
But, for example, for std::set and map, you can just do:
It's not a good pattern for multiset and multimap, since it won't stop at the first occurrence. C++20 finally added contains() for that reason - but, again, only to associative containers.