get vector from iterator

Discussion in 'C++' started by Victor Bazarov, Mar 3, 2005.

  1. 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
     
    Victor Bazarov, Mar 3, 2005
    #1
    1. Advertising

  2. Victor Bazarov

    Noah Roberts Guest

    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.
     
    Noah Roberts, Mar 3, 2005
    #2
    1. Advertising

  3. 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.
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.contendix.com> - Software Development & Consulting
     
    Dietmar Kuehl, Mar 3, 2005
    #3
  4. Victor Bazarov

    Noah Roberts Guest

    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.
     
    Noah Roberts, Mar 3, 2005
    #4
  5. Noah Roberts wrote:
    > [...]
    > class Content
    > {
    > std::vector<Content&> *container;


    There are no containers of references.

    > [...]
     
    Victor Bazarov, Mar 3, 2005
    #5
  6. 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
     
    Dr. J.K. Becker, Mar 3, 2005
    #6
  7. 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
     
    Dr. J.K. Becker, Mar 4, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    24,109
  2. Replies:
    8
    Views:
    1,996
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    618
    James Kanze
    Sep 4, 2007
  4. zl2k
    Replies:
    27
    Views:
    1,657
    Francesco S. Carta
    Sep 7, 2010
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    392
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page