get vector from iterator

V

Victor Bazarov

Dr. J.K. Becker said:
Is there any way to get a pointer to a vector (or the vector itself) if you
only have an iterator of it?

Not portably, no. Why do you need it? If you need a vector, just pass
the vector into your function, you can always get an iterator from it.
[...]
And another, somewhat related question:

The basics:

class Test
{
public:
double d;
void DoSomething(double t);
};

Now in another file I do:

vector<Test> x;
than I push_back lots of stuff into x and at one point do

x.DoSomething(double t);

This is a syntax error, first of all. Second, 'x' is a 'vector'.
'vector' does not have member 'DoSomething'. Do you mean you do

x[someindex].DoSomething(somedoublevalue);

?
Now in the function DoSomething, can I use 'this' in a nifty way to get a
pointer to the vector x?

No. A contained object does not know it is contained anywhere *unless*
you somehow tell the object where it is contained.

V
 
N

Noah Roberts

Dr. J.K. Becker said:
Hi all,

Is there any way to get a pointer to a vector (or the vector itself) if you
only have an iterator of it?

No. An iterator is like a pointer and you can't retrieve the
originating container from it according to my experience and all
documentation I can find.
 
N

Noah Roberts

Dr. J.K. Becker said:
Now in the function DoSomething, can I use 'this' in a nifty way to get a
pointer to the vector x?
Something like
void DoSomething(double t)
{
vector<Test> *q;
q=this->SomeNiftyThings();
}

With some forward planning:

class Content
{
std::vector<Content&> *container;
public:
Content& set_container(std::vector<Content&> vector)
{
container = &vector;
return *this;
}
};

example use:
std::vector<Content&> x;
Content y;

x.push_back(y.set_container(x));

If you are not holding references but instead copies I think this would
be a little more fun. Also keep in mind that adding the same reference
to different containers breaks that code. You would have to do a lot
more planning than that code represents. That is just a general idea
of you could approach the problem. You could probably use templates
somehow to generalize the approach but it won't be too terribly easy.

If you really need this functionality then do it; but you probably have
other options you have not thought of. I have never needed
functionality like what you are trying to do.
 
D

Dr. J.K. Becker

Hi all,

Is there any way to get a pointer to a vector (or the vector itself) if you
only have an iterator of it? Something like this:

DoSomething(vector<double>::iterator x)
{
vector<double> *q;
//do something to x so that this works
q=x.GiveMeAPointerToTheVector(please);
}

And another, somewhat related question:

The basics:

class Test
{
public:
double d;
void DoSomething(double t);
};

Now in another file I do:

vector<Test> x;
than I push_back lots of stuff into x and at one point do

x.DoSomething(double t);

Now in the function DoSomething, can I use 'this' in a nifty way to get a
pointer to the vector x?
Something like
void DoSomething(double t)
{
vector<Test> *q;
q=this->SomeNiftyThings();
}

Thanks for any help!

Jens
 
D

Dr. J.K. Becker

Hmmm,

Well, at least you all have the same short answer :). Oh well, I have to
figure out something else then. Thanks for the help!

Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top