gpt4 book ai didi

c++ - 通用 find_if 检查二维数组中的元素

转载 作者:行者123 更新时间:2023-12-01 14:47:27 24 4
gpt4 key购买 nike

使用 std::find_if 我们可以找到一个元素是否存在于普通的一维数组中。
灵感来自 this question ,我想知道,我们是否可以提供一个算法函数来检查任何类型和任意数量元素的二维数组(即 std::vectorstd::vector s 或 std::arra y 的 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!
}

最佳答案

同的帮助下std::find_if ,我们可以做到这一点。
以下是一个采用迭代器的模板函数(就像大多数标准算法函数一样)
二维数组( std::vector<std::vector<Type>> ,或std::array<std::array<Type, RowSize>, ColSize> ) 并返回指向内部数组(元素存在的地方)的迭代器,否则
二维数组的结束迭代器。
( 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);
}
);
}

关于c++ - 通用 find_if 检查二维数组中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62624977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com