【发布时间】:2017-05-13 12:37:30
【问题描述】:
在进行代码审查时发现此代码:
std::array<uint, 10> expArray2 = {92,130,169,951,634,187,377,233,865,944};
copy(&expArray2[0], &expArray2[10], back_inserter(expected));
^~~~Is this Undefined behavior as ArrayIndexOutOfBoundAccess?
和(我建议的)一样吗:
std::copy ( expArray2, expArray2+10, expected.begin() );
我的同事说 1.) 两者都相同 2.) 没有未定义的行为?
我向他解释,指针和迭代器是different 的两个东西,还有std::copy 签名:
template <class InputIterator, class OutputIterator>
OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result);
有人可以向我确认/解释一下吗如果我在任何地方都错了?
【问题讨论】:
-
指针是一种迭代器。您提供的链接中甚至提到了这一点。
-
是的,但我主要关心的是 arrayIndexOutOfBoundAccess。
-
似乎这个问题的核心是关于表达式
&expArray2[10]。在这种情况下,这看起来很相关:stackoverflow.com/questions/988158/… -
是的,你没看错。
标签: c++ pointers iterator copy indexoutofboundsexception