- 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)...);
}
- Boost C++ Application Development Cookbook(Second Edition)
- Java Web開發之道
- 區塊鏈架構與實現:Cosmos詳解
- Selenium Design Patterns and Best Practices
- Java編程指南:基礎知識、類庫應用及案例設計
- Interactive Applications Using Matplotlib
- Mastering JavaScript Design Patterns(Second Edition)
- 深入分布式緩存:從原理到實踐
- 速學Python:程序設計從入門到進階
- 軟件項目管理實用教程
- 創意UI:Photoshop玩轉APP設計
- OpenCV 3計算機視覺:Python語言實現(原書第2版)
- Deep Learning for Natural Language Processing
- SQL Server實例教程(2008版)
- Node.js Web Development