我知道下面的代码可以从阵列构建STL向量:std :: vector构造函数 - 为什么int而不是int *?
// the iterator constructor can also be used to construct from arrays:
int myints[] = {16,2,77,29};
std::vector<int> fifth (myints, myints + sizeof(myints)/sizeof(int));
(来源:cppreference)
使用的构造应是
template <class InputIterator>
vector (InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type());
如果<类InputIterator的>是< int>在上面的例子中,为什么不是InputIterator第一个是一个整数指针吗?数组名“myints”衰变成一个指向第一个元素,因为它相当于& myints [0]
我想到了一个正确的版本将是
template <class InputIterator>
vector (InputIterator *first, InputIterator *last,
const allocator_type& alloc = allocator_type());
在你的情况下,'InputIterator'是'int *'。 – PlasmaHH 2013-03-05 10:04:11
迭代器不是指针。指针可以对迭代器概念进行建模,但其他方式不能以任何方式保证。 – Xeo 2013-03-05 10:05:41