[英]C++17 fold syntax to test vector composition
向量any
, all
的标准 C++17 实现:
template<class C, class T>
bool contains(const C& c, const T& value) {
return std::find(c.begin(), c.end(), value) != c.end();
}
template<class C, class... T>
bool any(const C& c, T&&... value) {
return (... || contains(c, value));
}
template<class C, class... T>
bool all(const C& c, T&&... value) {
return (... && contains(c, value));
}
用于
std::array<int, 6> data0 = { 4, 6, 8, 10, 12, 14 };
assert( any(data0, 10, 55, 792));
assert( !any(data0, 11));
assert( all(data0, 6, 14, 8));
assert( !all(data0, 6, 7, 8));
是否有类似的方法来定义only
,当且仅当向量的唯一值集与输入值匹配时才返回 true ? 所以下面的断言会成立
std::array<int, 6> data1 = { 1, 1, 2, 1, 2 };
assert( only(data1, 1, 2));
assert( !only(data1, 1));
您可以提供count
function:
template<class C, class T>
auto count(const C& c, const T& value) {
return std::count(c.begin(), c.end(), value);
}
并且only
这样写:
template<class C, class... T>
bool only(const C& c, T&&... value) {
return (count(c, value) + ...) == c.size();
}
这会处理c
中的重复元素,但要求value
s 是唯一的。
这是一个演示。
template<class C, class...Ts>
bool only( C const& c, Ts&&...ts ) {
std::size_t count = (std::size_t(0) + ... + contains(c, ts));
return count == c.size();
}
这会计算c
中的列表ts...
的数量,如果您发现 ear 的数量等于c
的元素,则返回 true。 现在这假定c
和ts
的唯一性。
我们只是将计数移至only
并在 std 算法中进行测试:
template<class C, class...Ts>
bool only( C const& c, Ts&&...ts ) {
using std::begin; using std::end;
auto count = std::count_if( begin(c), end(c), [&](auto&& elem) {
return ((elem == ts) || ...);
} );
return count == c.size();
}
鲍勃是你的叔叔。
我们也可以做一个notcontains
基于不only
的算法,但我认为那更复杂。
它不使用折叠表达式,但它应该可以工作
template<class C, class... T>
bool only(const C& c, T&& ...vals) {
auto ilist = {vals...}; //create initializer_list
for (auto el : c) {
if (!contains(ilist, el)) return false;
}
return true;
}
使用折叠表达式而不是std::initializer_list
的东西
template<class T, class... Ts>
bool is_one_of(const T& val, Ts&& ...vals)
{
return ((val == vals) || ...);
}
template<class C, class... Ts>
bool only(const C& c, Ts&& ...vals)
{
for (const auto& el : c) {
if (!is_one_of(el, vals...)) return false;
}
return true;
}
// or if you hate raw loops
template<class C, class... Ts>
bool only(const C& c, Ts&& ...vals)
{
using std::beign; using std::end;
auto it = std::find_if(begin(c), end(c), [&](const auto& el) {
return !is_one_of(el, vals...);
});
return (it == end(c));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.