代码片段
std::vector<int> v{ 3, 8, 2, 5, 1, 4, 7, 6};
Partition(v.begin(), v.end(), v.begin());
应重新排列 vector v,使前 n 个元素小于或等于 3,而后面的元素大于 3。生成的 vector 应如下所示:
1,2,3,5,8,4,7,6
我如何测试 Partition 是否在口头断言以下内容的断言语句中产生正确的结果:
Assert that the first part of the vector only contains elements that are smaller than or equal to x and that the seconds part contains only elements that are greater than x
?
最佳答案
感谢委员会中的好人,您对要划分的 vector 的要求可以表示为“vector 是否已划分?”,这很好地映射到标准 std::is_partitioned
算法。生活不是很美好吗?
assert(std::is_partitioned(v.begin(), v.end(), [](int e) { return e <= 3; }));
关于c++ - 断言 vector 按值 x 划分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42828750/