#include <iostream>
#include <tuple>
#include <utility>
template<int E, int H, int... Rest>
struct find_helper {
static constexpr auto rest() -> std::size_t {
if constexpr(H==E)
return std::tuple_size_v<decltype(std::make_tuple(Rest...))>;
else
return find_helper<E, Rest...>::rest();
}
};
template<int E, int H>
struct find_helper<E, H> {
static constexpr auto rest() -> std::size_t {
if constexpr(H==E)
return 0;
else
return -1;
}
};
template<int E, int... Values>
auto find_first() -> std::size_t {
return sizeof...(Values) - find_helper<E, Values...>::rest() - 1;
}
int main() {
std::cout << find_first<0, 1, 1, 1, 0, 0>();
return 0;
}
如果找得到,回传index,范围0~N-1,N为Array 长度。
如果找不到,回传N。
如果Array为空,编译错误。
前提是Array要编译期就知道内容。
不过如果你是面试IC厂,我想应该是动态Array。
如果里面的值都是0跟1
我猜应该是储存的时候就用shift存成Bits
然后使用找第一个0的bit的指令去找
这是IC厂会干的事
内存量最少、计算最快。