将vector<int> const&和替换int size为array_view<const int>.
Anarray_view<T>是一个类,它存储一对指针 ( band e),并公开[]and .size()and begin()and and and end()and front()and back()and empty()。它具有来自std::vector<T>&, std::vector<remove_const_T> const&, T(&)[N], std::array<T,N>&,std::array<remove_const_T,N>const&和 from T*, T*and的隐式构造函数T*, size_t。
类似array_view<T> without_front(size_t=1)和array_view<T> without_back(size_t=1)的方法也很有用。
有一个std::experimental::array_view也支持多维数组,或者你可以自己滚动。 这是我在 stack overflow 上发布的一个,它解决了一个不同的问题。它没有without_front,但这很容易编写(这取决于你希望它有多安全——我会选择完全安全的,它拒绝返回格式错误的数组视图,而是返回一个空视图,因为支票很便宜)。
使用看起来像:
int find_kth(array_view<int const> a, array_view<int const> b, int k){
// ...
find_kth(a, b.without_front(j), k-j);
// ...
}
我觉得很光滑。如果您想传递原始数组,只需{arr, size}将其变为array_view. 如果你想传递一个向量,它会隐式转换。