vector<vector<double>> v2d(2, vector<double>(3,0.0));
How later on reset all elements back to 0.0?.
How to clear vec entirely, free memory?.
Are there any performance degradation in using vector arrays verus conventionl arrays using new or malloc?..
What can be done to minize such degradation if any?.

How later on reset all elements back to 0.0?

There are a number of ways. One that comes to mind is:

v2d.assign(2, vector<double>(3));

This will completely replace the contents of the vectors with default initialized objects. However, that can be troublesome if you have pointers into the original objects. If that's the case, you're stuck with some variant of a value-replacement loop to retain the same addresses for elements:

for (auto& x : v2d)
{
    for (auto& y : x)
    {
        y = 0;
    }
}

How to clear vec entirely, free memory?

C++11 added a shrink_to_fit member function that is intended to correct the capacity based on the vector's size. However, it's not a hard requirement that memory is released, so we're still in a position where certain tricks are the only guaranteed way to ensure memory is released. For example:

vector<vector<double>>().swap(v2d);

This is a full clear and release, v2d will erase all of its elements. Alternatively, you can just let the object go out of scope and its destructor will release memory for you.

If you want to reset the vector to its original initialized state, that can be done as well given the provided definition of v2d:

vector<vector<double>>(2, vector<double>(3, 1.0)).swap(v2d);

Are there any performance degradation in using vector arrays verus conventionl arrays using new or malloc?

In theory, yes. A carefully written dynamic array can outperform std::vector for specialized code. In the general case, it's actually quite difficult to write a dynamic array that's competitive with std::vector. The C++ libraries are highly optimized by very talented programmers.

What can be done to minize such degradation if any?

Typically any 'degradation' results in unused memory. A vector will hold on to allocated memory for later usage when you remove elements. If you find yourself removing elements but not adding an equal number back in short order, it would be a good idea to make sure the capacity of the object is clean using something like the swap trick from before.

Other than that, you're pretty solid.

I notice that you're making a vector of vectors of doubles. Are you planning to use this like a matrix to do algebra on and such? Or, are there other constriants (all the "rows" have to be the same length, or something)? If these things are true, then you might want to consider using/writing a simple matrix class that has the functionality that you require. It will be more readable and maintainable in the long-run.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.