From 标准, https://timsong-cpp.github.io/cppwp/n4659/alg.set.operations#includes std::includes
:
返回:true
if [first2, last2)
为空或者范围内的每个元素[first2, last2)
包含在范围内[first1, last1)
。
退货false
否则。
Note: as this is under [alg.set.operations], the ranges must be sorted
从字面上看,如果我们让R1=[first1, last1)
and R2=[first2, last2)
,这是评估:
∀a∈R2 a∈R1
然而,这并不是实际评估的内容。为了R1={1}
and R2={1,1,1}
, std::includes(R1, R2)
返回假:
#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 https://wandbox.org/permlink/1Nogewa39wlVrdn6
这令人惊讶。我用 libstdc++ 和 libc++ 验证了它,但在我看来,这不太可能是标准库实现中的错误,考虑到它是算法库的一部分。如果这不是算法std::includes
应该运行,什么是?