我在VC9中構建庫時遇到了問題,但之前成功構建於VC6中。 下面是代碼的一部分:使用C++中的指針初始化迭代器
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).
有了這個,我只是消除了它的迭代器和使用LOWER_BOUND(第一,最後,& pos2find,COMP);
作爲一個常規提示:msvc6是如此的破壞和預標準,以致爲它編寫的大量代碼將不能用符合標準的編譯器進行編譯。它甚至不支持正確的循環變量範圍,直到後來的某個Service Pack。 – PlasmaHH
我想盡可能多。但是,也許有人認爲這種情況有一個解決方法。 – Adrian