How to convert between iterators and pointers?

From: Leon (
Date: 12/20/03

Date: 20 Dec 2003 02:45:15 -0800

Hi all.

I'm a bit confused about the use of STL iterators and pointers, and I was
wondering if maybe you could give me some pointers ;)

Is it possible to convert between iterators and pointers pointing to the
same type, e.g. between "std::set<Vertex>::iterator vi" and "Vertex *" ?

I found a quote in the MSVC STL documentation, saying:
  "Remember that an object pointer can always serve as a
  random-access iterator. Therefore, it can serve as any category
  of iterator, as long as it supports the proper read/write access
  to the sequence it designates."

But I can't seem to assign a "Vertex *" to an iterator, like this:
  Vertex v;
  Vertex *pv = &v;
  std::set<Vertex>::iterator vi = pv;

Is there a right way of letting an iterator point to the same object as a

Using an iterator as a pointer also doesn't seem to work. For example:
  // Edge constructor declaration:
  // Edge(Vertex *pv1, Vertex *pv2);
  std::set<Vertex>::iterator vi1, vi2;
  // [Set vi1 and vi2 here]
  Edge e(vi1, vi2); // Won't compile, saying "No user-defined
                     // conversion operator available"

Instead, I have to resort to:
  Edge e(&(*vi1), &(*vi2));

Is this normal, or am I doing something wrong?

Thanks in advance,

Relevant Pages

  • Re: Standard containers (Was: Wiki by the committee closed?)
    ... Since a fixed size is certainly enough for an iterator, ... bad pointers, overflows, and maybe even CONTAINER_ERROR_INDEX), why not ... be reported if a container is accessed out of bounds. ... OUR list elements never change when you add something to our lists. ...
  • Re: abstract containers: does such a thing exist, conceptually?
    ... create a firewall between different types of lists. ... > behaviour by the additional pointers. ... I do not like much STL's style of iteration with iterator objects, ... For example, an array view for containers with ordered elements, ...
  • Re: How to convert between iterators and pointers?
    ... Leon wrote in ... > I'm a bit confused about the use of STL iterators and pointers, ... Therefore, it can serve as any category of iterator, as ...
  • Re: Pointer To A Vector Elements While Still Adding
    ... however there are cases that pointers get invalidated too in deque case. ... "surface" encapsulation about the type of the target container used, ... "surface" encapsulation can be provided by a typedef of the iterator type used. ...
  • Re: Vector.Erase?
    ... So I tried to change it using iterator, but the result does not acting ... only on iterators, not pointers. ... vector::iterator was a pointer in the VC6 STL. ... gratuitously embed expressions with side-effects inside other expressions): ...