Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > get vector from iterator

Reply
Thread Tools

get vector from iterator

 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-03-2005
Dr. J.K. Becker wrote:
> 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
 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      03-03-2005

Dr. J.K. Becker wrote:
> 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.

 
Reply With Quote
 
 
 
 
Dietmar Kuehl
Guest
Posts: n/a
 
      03-03-2005
Dr. J.K. Becker wrote:
> Is there any way to get a pointer to a vector (or the vector itself)

if you
> only have an iterator of it?


No.
--
<(E-Mail Removed)> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      03-03-2005

Dr. J.K. Becker wrote:

> 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.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-03-2005
Noah Roberts wrote:
> [...]
> class Content
> {
> std::vector<Content&> *container;


There are no containers of references.

> [...]

 
Reply With Quote
 
Dr. J.K. Becker
Guest
Posts: n/a
 
      03-03-2005
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

 
Reply With Quote
 
Dr. J.K. Becker
Guest
Posts: n/a
 
      03-04-2005
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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can I use stl vector iterator to delete a vector of pointers? zl2k C++ 27 09-07-2010 11:47 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM



Advertisments