在 C++ 中用指针初始化迭代器
我在 VC9 中构建库时遇到问题,但之前在 VC6 中成功构建了该库。 这是代码的一部分:
size_t pos2find;
pos2find = J;
std::vector<size_t>::iterator it(&pos2find);
这是错误:
error C2664: 'std::_Vector_iterator<_Ty,_Alloc>::_Vector_iterator(const std::_Vector_iterator<_Ty,_Alloc> &)' : cannot convert parameter 1 from 'size_t *' to 'const std::_Vector_iterator<_Ty,_Alloc> &'
1> with
1> [
1> _Ty=size_t,
1> _Alloc=std::allocator<size_t>
1> ]
1> Reason: cannot convert from 'size_t *' to 'const std::_Vector_iterator<_Ty,_Alloc>'
1> with
1> [
1> _Ty=size_t,
1> _Alloc=std::allocator<size_t>
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
我感谢任何帮助。
编辑: 该代码来自一个名为“surfit”的开源库,它不是我的代码,所以我猜它是矢量类标准中发生变化的东西。 然后迭代器在另一个 std 函数中使用:
std::vector<size_t>::iterator * ptr_from = fault->sort_by_first_begin;
std::vector<size_t>::iterator * ptr;
ptr = std::lower_bound(ptr_from,
fault->sort_by_first_end,
it,
ptr_size_t_less);
编辑: 我想我找到了解决方案。在研究了 std::lower_bound() 后:
template <class ForwardIterator, class T, class Compare>
ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last,
const T& value, Compare comp );
Return iterator to lower bound
Returns an iterator pointing to the first element in the sorted range [first,last) which does not compare less than value. The comparison is done using either operator< for the first version, or comp for the second.
For the function to yield the expected result, the elements in the range shall already be ordered according to the same criterion (operator< or comp).
有了这个,我刚刚消除了 it 迭代器并使用了 lower_bound(first, last, &pos2find, comp);
I am having problems building a library in VC9, but which was previously successfully built in VC6.
Here is a part of the code:
size_t pos2find;
pos2find = J;
std::vector<size_t>::iterator it(&pos2find);
And here is the error:
error C2664: 'std::_Vector_iterator<_Ty,_Alloc>::_Vector_iterator(const std::_Vector_iterator<_Ty,_Alloc> &)' : cannot convert parameter 1 from 'size_t *' to 'const std::_Vector_iterator<_Ty,_Alloc> &'
1> with
1> [
1> _Ty=size_t,
1> _Alloc=std::allocator<size_t>
1> ]
1> Reason: cannot convert from 'size_t *' to 'const std::_Vector_iterator<_Ty,_Alloc>'
1> with
1> [
1> _Ty=size_t,
1> _Alloc=std::allocator<size_t>
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
I appreciate any help.
EDIT:
The code is from an open source library called "surfit", it is not my code, so i guess it's something that changed in the standard of the vector class.
The iterator is then used in another std function:
std::vector<size_t>::iterator * ptr_from = fault->sort_by_first_begin;
std::vector<size_t>::iterator * ptr;
ptr = std::lower_bound(ptr_from,
fault->sort_by_first_end,
it,
ptr_size_t_less);
EDIT:
I think i found a solution. After looking into std::lower_bound() :
template <class ForwardIterator, class T, class Compare>
ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last,
const T& value, Compare comp );
Return iterator to lower bound
Returns an iterator pointing to the first element in the sorted range [first,last) which does not compare less than value. The comparison is done using either operator< for the first version, or comp for the second.
For the function to yield the expected result, the elements in the range shall already be ordered according to the same criterion (operator< or comp).
With this i just eliminated the it iterator and used lower_bound(first, last, &pos2find, comp);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不保证向量中的迭代器是原始指针。在 VS6 中,
vector::iterator
恰好是T*
,因此您可以使用T*< 来初始化迭代器/代码>。
在 VS 的更高版本中,
vector::iterator
的实现更改为类类型(正如它应有的那样),因此做出错误假设的代码现在无法编译。在任何情况下,即使迭代器是 T*,您发布的代码也是有错误的,因为提供的指针不是指向向量的指针。
An iterator into a vector isn't guarantied to be a raw pointer. In VS6
vector<T>::iterator
happened to be aT*
so you could initialize the iterator with aT*
.In later versions of VS the implementation of
vector<T>::iterator
changed to be a class type (as it's entitled to be) so code that made bad assumptions now fails to compile.In any case the code you posted is buggy even if
iterator
is aT*
since the pointer supplied is not a pointer into the vector.好吧,即使在 VC6 中,发布的代码也毫无意义。但也许您正在寻找这样的东西
如果没有那么您最好发布更多代码,这样我们就可以看到您如何尝试使用迭代器。
Well the posted code makes no sense at all even in VC6. But maybe you are looking for something like this
If not then you better post a bit more code, so we can see how you trying to use the iterator.
原始代码依赖于标准无法保证的实现细节,即 std::vector 迭代器可以实现为指针这一事实。我不确定情况是否仍然如此,但在 Visual C++ 的更高版本中,实现更改为特定的类模板。
恐怕您必须检查所有代码并修复问题,更重要的是,正如已经指出的那样,即使考虑到我刚刚写的内容,您的示例也是有问题的。
请注意,对于指针,您可以使用 0 来表示“迭代器未设置”,这与分配
some_vector.end()
不同。如果您遇到类似的情况,我建议您使用 Boost.Optional 来包装你的迭代器。在那里,完成了(但使用不同的编译器)。
The original code relies on an implementation detail that isn't guaranteed by the standard, i.e. the fact that
std::vector
iterators could be implemented as pointers. I'm not sure whether this is still the case, but the implementation changed to a specific class template in later versions of Visual C++.I'm afraid that you have to go through all your code and fix things, more so since, as it has been already pointed out, your example is buggy even taking into account what I just wrote.
Beware that with pointers you can use 0 to mean "iterator not set", which would be different from assigning
some_vector.end()
. If you encounter a similar situation I suggest that you use Boost.Optional to wrap your iterator.Been there, done that (but with a different compiler).
以下似乎是
疯狂代码的预期内容(迭代器的排序数组,真的吗?),并且未经我测试。
The following seems to be what's intended
Crazy code (a sorted array of iterators, really?), and untested by me.