Discussion:
[std-proposals] std::contains algorithm
k***@gmail.com
2018-11-04 22:38:47 UTC
Permalink
std::set has a find member function. This member function uses the nature of a set to achieve O(log n) complexity. There exists a std::find algorithm that can be used with any container and has a complexity of O(n).

C++20 added a contains member function to some containers. Once again, the contains member function of std::set uses the nature of a set to achieve O(log n) complexity. However, there isn't (to my knowledge) a generic std::contains algorithm for containers that don't have a contains member function.

Personally, the absence of std::contains seems like an oversight. Is it?
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/689fd0dc-0ddf-464f-b041-f5c071d643df%40isocpp.org.
Arthur O'Dwyer
2018-11-06 02:21:58 UTC
Permalink
Post by k***@gmail.com
std::set has a find member function. This member function uses the nature
of a set to achieve O(log n) complexity. There exists a std::find algorithm
that can be used with any container and has a complexity of O(n).
C++20 added a contains member function to some containers. Once again, the
contains member function of std::set uses the nature of a set to achieve
O(log n) complexity. However, there isn't (to my knowledge) a generic
std::contains algorithm for containers that don't have a contains member
function.
Personally, the absence of std::contains seems like an oversight. Is it?
The point of .contains(), I think, is to be able to write

if (myset.contains("foo")) { do a thing; }
if (mymap.contains("foo")) { do a thing; }
if (myvector.contains("foo")) { do a thing; } // whoops, this doesn't
exist in C++2a yet!

I wouldn't want to write

if (std::contains(myvector.begin(), myvector.end(), "foo")) { do a
thing; }

using a generic algorithm like that. That's just super verbose. I'd rather
write a helper function

if (my::contains(myvector, "foo")) { do a thing; }

in which case the existence of a generic range-based algorithm in std::
wouldn't help me at all, because I could just as easily implement
`my::contains` in terms of the already-existing std::find as in terms of
the hypothetical std::contains.

my $.02,
–Arthur
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/75210e6c-0ed6-4743-bd17-878d0cf128a3%40isocpp.org.
k***@gmail.com
2018-11-06 03:09:01 UTC
Permalink
I agree. Writing my::contains is trivial. However, you raised one of my criticisms of the standard algorithms. The verbosity. Every time I use a standard algorithm, I have to wrap the call into multiple lines (I don't like to go over 80 characters). Its ugly!

The only way to clean up the call site is to create helper functions like you suggested.

I used to think the Ranges TS will let me do this:
std::find(myvec.range(), "foo")
but then I actually read about Ranges and I was kind of disappointed.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/c424b255-437d-40b6-a9e9-652dc4b1c8f9%40isocpp.org.
p***@gmail.com
2018-11-07 09:22:23 UTC
Permalink
Post by k***@gmail.com
I agree. Writing my::contains is trivial. However, you raised one of my
criticisms of the standard algorithms. The verbosity. Every time I use a
standard algorithm, I have to wrap the call into multiple lines (I don't
like to go over 80 characters). Its ugly!
The only way to clean up the call site is to create helper functions like you suggested.
std::find(myvec.range(), "foo")
but then I actually read about Ranges and I was kind of disappointed.
In Ranges TS you can do
std::find(myvec, "foo")
which is even less verbose than what you hoped for.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/11bbf69f-81ac-4783-953a-e47da3c1eee0%40isocpp.org.
k***@gmail.com
2018-11-07 11:40:04 UTC
Permalink
It looks like I misread something. Yep, you're right. Ranges are gonna be
great! std::contains makes much more sense with Ranges. Maybe it makes
sense to add std::contains after all.
Post by p***@gmail.com
Post by k***@gmail.com
I agree. Writing my::contains is trivial. However, you raised one of my
criticisms of the standard algorithms. The verbosity. Every time I use a
standard algorithm, I have to wrap the call into multiple lines (I don't
like to go over 80 characters). Its ugly!
The only way to clean up the call site is to create helper functions like you suggested.
std::find(myvec.range(), "foo")
but then I actually read about Ranges and I was kind of disappointed.
In Ranges TS you can do
std::find(myvec, "foo")
which is even less verbose than what you hoped for.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/37a59148-47ad-4973-8788-b6dffb5ca0b5%40isocpp.org.
Continue reading on narkive:
Loading...