【问题标题】:Generic find_if to check an element in the 2D array通用 find_if 检查二维数组中的元素
【发布时间】:2020-06-28 16:01:15
【问题描述】:

使用std::find_if,我们可以找到一个元素是否存在于一个普通的一维数组中。

this question的启发,我想知道我们是否可以提供一个算法函数来检查二维数组中是否存在(即std::vectors的std::vector@ 987654326@y of std::array) 任意类型和任意数量的元素。

是否可以提供一个通用函数来查找二维数组中是否存在元素?

类似:

template<typename Iterator>
auto isIn2DArray(
   Iterator begin, const Iterator end, ElementType ele) noexcept
{
   // returns the iterator pointing to the row, if the element is found, otherwise the end of the array!
}

【问题讨论】:

    标签: c++ algorithm templates c++17 function-templates


    【解决方案1】:

    借助相同的std::find_if,我们可以实现这一目标。

    以下是一个模板函数,它采用迭代器(就像在大多数标准算法函数中一样) 二维数组(std::vector&lt;std::vector&lt;Type&gt;&gt;,或 std::array&lt;std::array&lt;Type, RowSize&gt;, ColSize&gt;) 并返回指向内部数组(元素存在的地方)的迭代器,否则 二维数组的结束迭代器。

    (See Live Online)

    #include <type_traits>   // std::remove_reference_t, std::remove_const_t, std::conditional_t, std::is_fundamental_v
    #include <iterator>      // std::cbegin(), std::cend()
    #include <algorithm>     // std::find_if
    #include <utility>       // std::declval
    
    // traits for finding the inner element type of 2D array(of std::vector or std::array)
    template<typename Iterator>
    using ContainerType = std::remove_const_t<std::remove_reference_t<decltype(*std::declval<Iterator>())>>;
    
    template<typename Iterator>
    using ElementType = std::remove_const_t<std::remove_reference_t<typename ContainerType<Iterator>::value_type>>;
    
    template<typename Iterator>  // optional: ElementType<Iterator> should also be enough!
    using ElementArgumentType = std::conditional_t<std::is_fundamental_v<ElementType<Iterator>>
       , ElementType<Iterator>, ElementType<Iterator> const&>;
    
    template<typename Iterator>
    auto isIn2DArray(
       Iterator begin, const Iterator end, ElementArgumentType<Iterator> val) noexcept
    {
       // used the standard algorithm std::find_if here!
       return std::find_if(begin, end, [val](const auto& row) noexcept {
          return std::find_if(std::cbegin(row), std::cend(row), [val](const auto& element) noexcept {
             return element == val;
             }
          ) != std::cend(row);
          }
       );
    }
    

    或者将一元谓词传递给函数,该函数将用于在数组数组中查找适当的数组。这样噪音会小一点!

    (See Live Online)

    #include <iterator>      // std::cbegin(), std::cend()
    #include <algorithm>     // std::find_if
    
    
    template<typename Iterator, typename UnaryPredicate>
    auto find_if_in_2DArray(
       Iterator begin, const Iterator end, UnaryPredicate unarayPred) noexcept
    {
       // used the standard algorithm std::find_if here!
       return std::find_if(begin, end, [unarayPred](const auto& row) noexcept {
          return std::find_if(std::cbegin(row), std::cend(row), unarayPred) != std::cend(row);
          }
       );
    }
    

    【讨论】:

      猜你喜欢
      • 2012-07-04
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 2017-04-11
      • 1970-01-01
      • 2021-02-03
      • 2019-07-04
      相关资源
      最近更新 更多