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

  • 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)...);
}
主站蜘蛛池模板: 泰宁县| 江安县| 仪陇县| 长春市| 封丘县| 广东省| 东安县| 福州市| 桦川县| 深泽县| 曲周县| 台北市| 开江县| 白城市| 镇康县| 惠安县| 梁平县| 寿宁县| 金沙县| 多伦县| 奉贤区| 沂南县| 济阳县| 台山市| 泸州市| 广元市| 吴旗县| 额济纳旗| 乐平市| 五常市| 孝义市| 青海省| 普安县| 雷州市| 滦南县| 宝兴县| 会理县| 临江市| 阳江市| 宜川县| 轮台县|