Board index » delphi » Iterators or Pointers?

Iterators or Pointers?

This question is a bit philosophical. Suppose I have a vector, v1, of
"structs". Each "struct" contains another vector v2 that contains iterators
to elements of v1

vector<a_struct> v1;

struct a_struct
{
  vector<vector<a_struct>::iterator> v2;

Quote
};

If I change these iterators to pointers

struct a_struct
{
 vector<a_struct*> v2;

Quote
};

this would work too. But can I always trust this last approach to work? Is
it "really" C++? Shouldn't I always use iterators in container classes?

What would you write?

/ Ola Weistrand

 

Re:Iterators or Pointers?


Quote
Ola Weistrand wrote:

> What would you write?

How are you going to use all this?

Re:Iterators or Pointers?


Quote
> Actually the question came up when I was/am implementing an algorithm for
my
> research in the field of Image Analysis. The "structs" in the first vector
> holds (among other things) three coordinates and represents a discrete
> two-dimensional surface. The internal vector of pointers/iterators points
to
> the "neighbors" of the element of the surface. This gives me direct access
> to the neighbors, which is crucial for my algorithm. I hope this
description
> was possible to follow.

> /Ola
"Thomas Maeder" <mae...@glue.ch> wrote in message

news:3C1E3DA9.9010401@glue.ch...
Quote
> Ola Weistrand wrote:

> > What would you write?

> How are you going to use all this?

Re:Iterators or Pointers?


Quote
Ola Weistrand wrote:
>> Actually the question came up when I was/am implementing an algorithm for
> my
>> research in the field of Image Analysis. The "structs" in the first vector
>> holds (among other things) three coordinates and represents a discrete
>> two-dimensional surface. The internal vector of pointers/iterators points
> to
>> the "neighbors" of the element of the surface. This gives me direct access
>> to the neighbors, which is crucial for my algorithm. I hope this
> description
>> was possible to follow.

I hope I got it.

One decision criterion will be that you can always get a pointer from an
iterator, but not the other way round.

Another one is that it's not exactly clear to me if this construct

struct a_struct
{
  vector<vector<a_struct>::iterator> v2;

Quote
};

is legal C++. Standard containers are only to be instantiated for
complete types, which a_struct is clearly not at the point where v2 is
declared.

Other Threads