Vector of vectors in contiguous memory I have a class Vector3d with 3 double components and want to store several of them in a Field. For reasons of efficiency when sending a Field over a network via MPI, I would like to allocate contiguous memory in the Field class and let the Vector3ds store their components in that chunk of memory. In addition, Vector3ds should also be able to live without a Field; in this case, they have to care about their own memory allocation.

#include &lt;cstddef&gt;
#include &lt;iostream&gt;
#include &lt;vector&gt;

class Field;

class Vector3d
{
    friend class Field;

    public:
        Vector3d() : components_(new double[3]) {}
        ~Vector3d() { delete[] components_; }
        Vector3d(const Vector3d &amp;v) : components_(new double[3])
        {
            components_[0] = v.components_[0]; 
            components_[1] = v.components_[1]; 
            components_[2] = v.components_[2];
        }
        double &amp; operator()(const std::size_t index) { return components_[index]; }

    private:
        Vector3d(double *ptr) : components_(ptr) {};
        double *components_;
};

class Field
{
    public:    
        Field(std::size_t size);
        ~Field();
        Vector3d &amp; operator()(const std::size_t index) { return *(vectors_[index]); }

    private:
        std::vector&lt;double&gt; components_;
        std::vector&lt;Vector3d *&gt; vectors_;
};

Field::Field(std::size_t size) : components_(std::vector&lt;double&gt;(3 * size)), vectors_(std::vector&lt;Vector3d *&gt;(size, new Vector3d(0)))
{
    double *ptr = &amp;components_[0];
    for (auto it = vectors_.begin(); it != vectors_.end(); ++it)
    {
        (*it)-&gt;components_ = ptr;
        ptr += 3;
    }
} 

Field::~Field()
{
    for (auto it = vectors_.begin(); it != vectors_.end(); ++it)
        (*it)-&gt;components_ = 0;
}

int main(int argc, char *argv[])
{
    Field field(2);
    Vector3d &amp;v = field(1);
    v(2) = 5;

    Vector3d v2 = v;
    v2(2) = 2;

    std::cout &lt;&lt; field(1)(2) &lt;&lt; std::endl;

    return 0;
}


I would like to hear what you think about this piece of code. Can this be considered a good solution or what would you change?
When it comes to multidimensional vectors I usually suggest two approaches.
1. vector/array (C++11 and later)
This only works, when the inner dimensions are fixed and only the outer is runtime dynamic. In the end it comes down to something like this:
std::vector&lt;std::array&lt;T, N&gt;&gt; field;

In your case a Vector3d would either derive from std::array&lt;double, 3&gt; or contain one.
2. simple vector
This approach simply creates a one dimensional vector with X * Y size.
The element (x, y) can then be addressed with vec[y * X + x]. This works also with the older C++ standard. Although I think this solution might not work in your setup.
Bonus
I just want to highlight @Yakk's comment. Your Vector3d could look like this:
class Vector3d
{
public:
    // stuff
    // ...
private:
    double components_[3];
};

This way the memory would also be contigous.