简体   繁体   English

将 std::begin 和 std::end 与具有运行时大小的数组一起使用

[英]Using std::begin and std::end with array with run-time size

For a coding test, I have the following function:对于编码测试,我有以下 function:

static bool exists (int ints[], int size, int k)

The goal is to return true if k is in ints else return false .如果kints中,目标是返回true ,否则返回false

Using the std library, one could do this:使用 std 库,可以这样做:

static bool exists (int ints[], int size, int k) {
    return std::binary_search(std::begin(ints), std::end(ints), k);
}

However, std::begin() only works with C-style arrays if the size is known to the compiler, from what I've been able to gather.但是,如果编译器知道大小,则std::begin()仅适用于 C 风格的 arrays,据我所知。 I'm not sure why that is the case though.我不确定为什么会这样。

Is there a way to achieve what I want without writing a custom iterator to handle it?有没有办法在不编写自定义迭代器来处理它的情况下实现我想要的? Is it even possible to write such an iterator?甚至可以编写这样的迭代器吗?

Using std::vector instead is not an option, since I can't change the function definition.使用std::vector不是一个选项,因为我无法更改 function 定义。 Copying the array to a std::vector before calling std::binary_search() also seems like a waste of CPU time.在调用std::binary_search()之前将数组复制到std::vector似乎也浪费了 CPU 时间。

You cannot use std::begin() or std::end() as you want to because, as Andrieux pointed out in a comment, what appears to be an array-type in the function's argument-list is just a pointer.您不能随意使用std::begin()std::end() ,因为正如 Andrieux 在评论中指出的那样,函数参数列表中似乎是数组类型的只是一个指针。 This is confirmed by the fact that the second argument to the function is the number of elements. function 的第二个参数是元素的数量这一事实证实了这一点。 Such an argument would not be necessary if the information needed by std::end() were present in ints[] .如果std::end()所需的信息存在于ints[]中,则不需要这样的论点。

As each of the first four comments that I saw at the time of this writing indicated, however, the pointer that is passed in is an iterator.然而,正如我在撰写本文时看到的前四条评论中的每一条都指出的那样,传入的指针是一个迭代器。 In fact, an ordinary pointer has all of the properties of a random-access iterator.事实上,普通指针具有随机访问迭代器的所有属性。

So, yes, you don't need to come up with your own kind of iterator.所以,是的,你不需要想出你自己的迭代器。 Just use the passed-in pointer as the commenters indicated.只需按照评论者的指示使用传入的指针。

static bool exists (int ints[], int size, int k) {
  return std::binary_search(ints, ints + size, k);
}

However, calling std::binary_search() is the right thing to do only if the elements of ints[] be ordered so that std::binary_search() will work.然而,调用std::binary_search()是正确的做法,只有当ints[]的元素被排序以便std::binary_search()可以工作时。 You did not state that in your post.您没有在您的帖子中提到 state。

As to whether it be possible to write an iterator like what you might want, the answer is "yes," but it is unnecessary because a pointer is already the right kind of iterator to input to std::binary_search() .至于是否可以像您想要的那样编写迭代器,答案是“是”,但这是不必要的,因为指针已经是输入到std::binary_search()的正确迭代器。

You are right that using std::vector in the body would be a waste of time.你是对的,在正文中使用std::vector会浪费时间。

You could have done exactly as you originally wanted if both the input-array were ordered, and the signature of the function had instead been如果两个输入数组都是有序的,并且 function 的签名改为

template<int size> bool exists (int (&ints)[size], int k);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM