简体   繁体   English

C ++无法从const int *转换为const_iterator

[英]C++ Cannot convert from const int* to const_iterator

I've updated to a newer C++ compiler (going from Visual C++ 6.0 to Visual C++ 2015) and I am working on converting a Vector template class to be compatible. 我已经更新到更新的C ++编译器(从Visual C ++ 6.0到Visual C ++ 2015),并且我正在转换Vector模板类以使其兼容。 One error I am encountering involves the vector::erase method and the input type. 我遇到的一个错误涉及vector :: erase方法和输入类型。

  • Template class snippet: 模板类代码段:

     template<class Type> class Vector { public: typedef Type* iterator; typedef const Type* const_iterator; ... iterator erase( const_iterator iBegin ); iterator erase( const_iterator iBegin, iEnd ); private: VectorImpl<Type>* m_pImpl; }; ... template <typename Type> typename Vector<Type>::iterator Vector<Type>::erase( typename Vector<Type>::const_iterator iBegin ) { return m_pImpl->erase( iBegin ); }; 

    ... ...

  • Error: 错误:

C2440: 'initializing': cannot convert from 'const int*' to 'std::_Vector_const_iterator>>' C2440:'正在初始化':无法从'const int *'转换为'std :: _ Vector_const_iterator >>'

I was able to convert a std::vector iterator to an int* by dereferencing the iterator but I'm not sure how to do the inverse: 通过解引用迭代器,我能够将std :: vector迭代器转换为int *,但是我不确定如何进行逆操作:

template <typename Type> typename Vector<Type>::const_iterator Vector<Type>::begin()
{
    Vector<Type>::const_iterator begin = &(*m_pImpl->begin());
    return begin;
};

Q: Is it possible to convert a const int* to a std::vector const_iterator? 问:是否可以将const int *转换为std :: vector const_iterator?

I was able to convert a std::vector iterator to an int* by dereferencing the iterator but I'm not sure how to do the inverse 通过解引用迭代器,我能够将std :: vector迭代器转换为int *,但是我不确定如何进行逆操作

You should bear in mind that pointers and iterators are not the same thing. 您应该记住,指针和迭代器不是一回事。 Pointers are a kind of iterator, but the reverse is not true. 指针是一种迭代器,但事实并非如此。

You can get the address of some element, by taking an iterator to that element, dereferencing the iterator, then taking the address of the result. 您可以通过以下方式获取某个元素的地址:对该元素进行迭代,然后取消对该迭代器的引用,然后获取结果的地址。

But it is not particularly meaningful to transform a raw pointer into an iterator. 但是将原始指针转换为迭代器并不是特别有意义。 In general this would be like pouring some orange juice into water then trying to separate the two afterwards. 通常,这就像将一些橙汁倒入水中,然后尝试将两者分开。 It is advisable to stick with iterators across the board. 建议始终使用迭代器

However, see below. 但是,请参见下文。

Is it possible to convert a const int* to a std::vector const_iterator? 是否可以将const int *转换为std :: vector const_iterator?

Actually, yes. 其实,是。 If you know that the pointer is valid, and you are definitely only using vectors, you can rely on vector data contiguity: 如果知道指针是有效的,并且肯定只使用向量,则可以依靠向量数据连续性:

const int* ptr = ...;
const int* first = &vec[0];
const size_t dist = ptr - first;
auto it = vec.cbegin() + dist;

Now it is what you want. 现在it是你想要的。

But this is hackery relying on pointer and iterator arithmetic, the former of which will only work on a vector, and the latter of which will only be efficient on a sequence container (and requires std::advance instead on other containers, as a result). 但这是黑客依赖于指针和迭代器算法,前者只能在向量上工作,而后者只能在序列容器上有效(因此需要std::advance而不是其他容器) )。

I would not accept this in production code without a very good reason steered by limitations of a third-party library. 如果没有很好的理由受第三方库的限制,我不会在生产代码中接受这一点。

It seems to me that you should replace Vector with std::vector across the board. 在我看来,您应该将std::vector替换为Vector I understand that this may be problematic if you cannot (or do not want to) change the code's outward-facing interface. 我知道,如果您不能(或不想)更改代码的向外接口,则可能会遇到问题。 In that case, frankly, I'd leave std::vector out of it because these are two separate implementations that are going to rub up against one another. 坦率地说,在那种情况下,我将把std::vector排除在外,因为这是两个相互独立的实现。

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

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