简体   繁体   中英

Initialize a iterator with a pointer in C++

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);

Well the posted code makes no sense at all even in VC6. But maybe you are looking for something like this

std::vector<size_t>::iterator it = some_vector.begin() + pos2find;

If not then you better post a bit more code, so we can see how you trying to use the iterator.

An iterator into a vector isn't guarantied to be a raw pointer. In VS6 vector<T>::iterator happened to be a T* so you could initialize the iterator with a T* . 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 a T* since the pointer supplied is not a pointer into the vector.

The original code relies on an implementation detail that isn't guaranteed by the standard, ie 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

std::vector<size_t> tmp;
tmp.push_back(J);
std::vector<size_t>::iterator it = tmp.begin();

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);

Crazy code (a sorted array of iterators, really?), and untested by me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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