- The Modern C++ Challenge
- Marius Bancila
- 219字
- 2021-06-25 22:01:27
20. Container any, all, none
The requirement to be able to check the presence or absence of a variable number of arguments suggests that we should write variadic function templates. However, these functions require a helper function, a general-purpose one that checks whether an element is found in a container or not and returns a bool to indicate success or failure. Since all these functions, which we could call contains_all, contains_any, and contains_none, do is apply logical operators on the results returned by the helper function, we would use fold expressions to simplify the code. Short circuit evaluation is enabled after the expansion of the fold expression, which means we are evaluating only the elements that lead to a definitive result. So if we are looking for the presence of all 1, 2, and 3, and 2 is missing, the function will return after looking up value 2 in the container without checking value 3:
template<class C, class T>
bool contains(C const & c, T const & value)
{
return std::end(c) != std::find(std::begin(c), std::end(c), value);
}
template<class C, class... T>
bool contains_any(C const & c, T &&... value)
{
return (... || contains(c, value));
}
template<class C, class... T>
bool contains_all(C const & c, T &&... value)
{
return (... && contains(c, value));
}
template<class C, class... T>
bool contains_none(C const & c, T &&... value)
{
return !contains_any(c, std::forward<T>(value)...);
}
- LabVIEW Graphical Programming Cookbook
- Machine Learning with R Cookbook(Second Edition)
- Microsoft System Center Orchestrator 2012 R2 Essentials
- 小程序開發原理與實戰
- Learning SciPy for Numerical and Scientific Computing(Second Edition)
- Learning Concurrent Programming in Scala
- PySpark Cookbook
- 計算機應用基礎實踐教程
- 微服務架構深度解析:原理、實踐與進階
- Microsoft 365 Certified Fundamentals MS-900 Exam Guide
- PHP 7從零基礎到項目實戰
- Raspberry Pi Robotic Blueprints
- R語言:邁向大數據之路(加強版)
- MySQL從入門到精通
- Web前端開發精品課:HTML5 Canvas開發詳解