官术网_书友最值得收藏!

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)...);
}
主站蜘蛛池模板: 清水县| 云和县| 绥江县| 尼木县| 徐汇区| 大名县| 景宁| 磴口县| 临猗县| 鄂伦春自治旗| 万宁市| 常山县| 大连市| 师宗县| 黄冈市| 杨浦区| 广河县| 平潭县| 勃利县| 新巴尔虎左旗| 中西区| 西华县| 灵寿县| 铅山县| 龙里县| 米泉市| 桐城市| 伊吾县| 福州市| 望谟县| 饶阳县| 抚州市| 平和县| 和顺县| 响水县| 资兴市| 黄陵县| 东丰县| 登封市| 修文县| 玉树县|