nested stl containers - aliasing stl objects without invokingoperator=

Discussion in 'C++' started by Andrey Vul, Oct 20, 2009.

  1. Andrey Vul

    Andrey Vul Guest

    I'm building a multi-dimensional array using std::vector and I want to
    know there's a way (without using old-fashioned pointers) to alias
    elements from one to another by reference rather than by value (and
    invoking possibly significant overhead from operator=).

    Using vector < vector< ... >& > invokes compiler errors.

    Example:

    vector< vector<unsigned> > foo;
    /* ... initialize elements of foo */
    vector< vector<unsigned> > bar;
    bar[0] = foo[2]; // (*)
    bar[3] = foo[1]; // (*)
    /* ... continue initializing elements of bar */

    Is there a way to do the operation in (*) by using pass-by-reference,
    so that, e.g. &(bar[0]) == &(foo[2]) ?
    Andrey Vul, Oct 20, 2009
    #1
    1. Advertising

  2. Andrey Vul

    James Kanze Guest

    On Oct 20, 10:52 pm, Andrey Vul <> wrote:
    > I'm building a multi-dimensional array using std::vector and I
    > want to know there's a way (without using old-fashioned
    > pointers) to alias elements from one to another by reference
    > rather than by value (and invoking possibly significant
    > overhead from operator=).


    > Using vector < vector< ... >& > invokes compiler errors.


    Correct, since references aren't assignable, and the type used
    to instantiate a vector must be assignable.

    > Example:


    > vector< vector<unsigned> > foo;
    > /* ... initialize elements of foo */
    > vector< vector<unsigned> > bar;
    > bar[0] = foo[2]; // (*)
    > bar[3] = foo[1]; // (*)
    > /* ... continue initializing elements of bar */


    > Is there a way to do the operation in (*) by using
    > pass-by-reference, so that, e.g. &(bar[0]) == &(foo[2]) ?


    No, since any changes in bar (e.g. bar[0]=...) are not allowed
    to affect foo, and vice versa. It sounds like you need
    pointers. (You can, of course, write a thin wrapper class which
    uses pointers in the array, but dereferences them in
    operator[].)

    --
    James Kanze
    James Kanze, Oct 21, 2009
    #2
    1. Advertising

  3. Andrey Vul

    Jeff Flinn Guest

    Jeff Flinn, Oct 21, 2009
    #3
  4. Andrey Vul

    Andrey Vul Guest

    On Oct 21, 9:01 am, Jeff Flinn <> wrote:
    > Andrey Vul wrote:
    > > I'm building a multi-dimensional array using std::vector and I want to
    > > know there's a way (without using old-fashioned pointers) to alias

    >
    > Why not just usehttp://www.boost.org/doc/libs/1_40_0/libs/multi_array/doc/index.html
    >
    > Jeff


    Because the size of the second dimension is not guaranteed.
    boost::multiarry uses N*M[*L*K...] sizes, so the second dimension must
    be a vector, whose elements refer to std::vector<...>, so that foo[x]
    [y][z] can be used rather than foo[x]->at(y)->at(z) , which is why I'm
    asking for reference copy assignment instead of value copy assignment,
    because pointers will force usage of the latter, or even uglier, (*
    (*foo[x])[y])[z] .
    Andrey Vul, Oct 21, 2009
    #4
  5. Andrey Vul

    Andrey Vul Guest

    On Oct 21, 3:42 am, James Kanze <> wrote:
    > On Oct 20, 10:52 pm, Andrey Vul <> wrote:
    >
    > > I'm building a multi-dimensional array using std::vector and I
    > > want to know there's a way (without using old-fashioned
    > > pointers) to alias elements from one to another by reference
    > > rather than by value (and invoking possibly significant
    > > overhead from operator=).
    > > Using vector < vector< ... >& > invokes compiler errors.

    >
    > Correct, since references aren't assignable, and the type used
    > to instantiate a vector must be assignable.
    >
    > > Example:
    > > vector< vector<unsigned> > foo;
    > > /* ... initialize elements of foo */
    > > vector< vector<unsigned> > bar;
    > > bar[0] = foo[2]; // (*)
    > > bar[3] = foo[1]; // (*)
    > > /* ... continue initializing elements of bar */
    > > Is there a way to do the operation in (*) by using
    > > pass-by-reference, so that, e.g. &(bar[0]) == &(foo[2]) ?

    >
    > No, since any changes in bar (e.g. bar[0]=...) are not allowed
    > to affect foo, and vice versa. It sounds like you need
    > pointers.


    Indeed, but foo[x]->at(y)->at(z) feels like a kludge, as does (*(*foo
    [x])[y])[z] .
    Andrey Vul, Oct 21, 2009
    #5
  6. Andrey Vul

    Andrey Vul Guest

    On Oct 21, 3:42 am, James Kanze <> wrote:

    > (You can, of course, write a thin wrapper class which
    > uses pointers in the array, but dereferences them in
    > operator[].)


    Like this:

    template <typename T>
    class pvec {
    private:
    vector<T> *p;
    public:
    T& operator[](unsigned x) {
    return p->at[x];
    }
    pvec& operator=(vector<T> *p2) {
    p = p2;
    return *this;
    }
    pvec& operator=(pvec<T>& pv) {
    p = pv.p;
    return *this;
    }

    unsigned size() const {
    return p->size();
    }
    };

    ?

    Also, would boost::shared_ptr<vector<T>> be better than vector<T> * ?
    Andrey Vul, Oct 21, 2009
    #6
  7. Andrey Vul

    James Kanze Guest

    On Oct 21, 11:07 pm, Andrey Vul <> wrote:
    > On Oct 21, 3:42 am, James Kanze <> wrote:


    > > (You can, of course, write a thin wrapper class which
    > > uses pointers in the array, but dereferences them in
    > > operator[].)


    > Like this:


    > template <typename T>
    > class pvec {
    > private:
    > vector<T> *p;
    > public:
    > T& operator[](unsigned x) {
    > return p->at[x];
    > }
    > pvec& operator=(vector<T> *p2) {
    > p = p2;
    > return *this;
    > }
    > pvec& operator=(pvec<T>& pv) {
    > p = pv.p;
    > return *this;
    > }
    >
    > unsigned size() const {
    > return p->size();
    > }
    > };


    > ?


    > Also, would boost::shared_ptr<vector<T>> be better than vector<T> * ?


    It depends on what you're doing, but probably not. At any
    rate, you couldn't implement the above interface using it. (But
    for the moment, I'm not really sure what you're trying to do. I
    don't see any advantage in using the above rather than simply
    having a reference to the vector.)

    --
    James Kanze
    James Kanze, Oct 22, 2009
    #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. Koen
    Replies:
    1
    Views:
    497
  2. Claudio Jolowicz

    unique objects and stl containers

    Claudio Jolowicz, Apr 9, 2004, in forum: C++
    Replies:
    11
    Views:
    2,040
    James Dennett
    Apr 10, 2004
  3. JackC
    Replies:
    3
    Views:
    589
    Alan Griffiths
    Aug 13, 2004
  4. Replies:
    7
    Views:
    549
    Pete Becker
    Jan 25, 2008
  5. Sebastian Mach
    Replies:
    5
    Views:
    307
Loading...

Share This Page