by rtpg · 7y ago · 0 comments
At one point hacking around these issues become a bigger cost than just using some C library
Though personally I think the abstraction ceiling on C is so low I can’t imagine that cost ever being that high, other people are more comfortable with macro-based APIs
Yes, you need to be aware of object lifetimes in c++, but so do you in C. C++ makes it far easier to manage, though.
std::vector<T> x_Vec;
if (!(x_Vec.find(another_x) == std::vector<T>::npos) {
//some code
}
Also the ugly std::get<N> syntax.. auto x = std::get<0>(myTuple);
Why not - auto x = myTuple.0 if (x_Vec.find(another_x) != x_Vec.end()) {
//some code
}
The reason why vector doesn't have contains() is because it would be an O(N) operation for a vector. STL is generally designed in such a way that, when something needs to do an O(N) lookup, it has to deal with iterators, making the iteration happening in the background a bit more explicit.But, for example, for std::set and map, you can just do:
if (a_set.count(x)) { ... }
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.