From the standard, std::includes
:
Returns:
true
if[first2, last2)
is empty or if every element in the range[first2, last2)
is contained in the range[first1, last1)
. Returnsfalse
otherwise.
Note: as this is under [alg.set.operations], the ranges must be sorted
Taking this literally, if we let R1=[first1, last1)
and R2=[first2, last2)
, this is evaluating:
∀a∈R2 a∈R1
However, this is not what is actually being evaluated. For R1={1}
and R2={1,1,1}
, std::includes(R1, R2)
returns false:
#include <algorithm> #include <iomanip> #include <iostream> #include <vector> int main() { std::vector<int> a({1}); std::vector<int> b({1,1,1}); // Outputs 'false' std::cout << std::boolalpha << std::includes(a.begin(), a.end(), b.begin(), b.end()) << '\n'; }
Live on Wandbox
This is surprising. I verified it with both libstdc++ and libc++, but it seems unlikely to me that this would be a bug in the standard library implementation, considering it's part of the algorithms library. If this isn't the algorithm that std::includes
is supposed to run, what is?
std::equal_range on bidirectional iterators is extremely slow, because it has to walk step by step through the range. The std::set. find method, on the other hand, uses the tree structure of std::set to find the element really fast. It can, basically, get midpoints of a range really fast.
Sexually transmitted diseases (STDs) — or sexually transmitted infections (STIs) — are generally acquired by sexual contact. The bacteria, viruses or parasites that cause sexually transmitted diseases may pass from person to person in blood, semen, or vaginal and other bodily fluids.
Consider using std::list if: You need to store many items but the number is unknown. You need to insert or remove new elements from any position in the sequence. You do not need efficient access to random elements.
I posted this in the cpplang slack, and Casey Carter responded:
The description of the algorithm in the standard is defective. The intent is to determine [if] every element in the needle appears in order in the haystack.
[The algorithm it actually performs is:] "Returns true if the intersection of sorted sequences R1 and R2 is equal to R2"
Or, if we ensure we are certain of the meaning of subsequence:
Returns: true if and only if [first2, last2) is a subsequence of [first1, last1)
link to Casey Carter's message
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With