开发者

allocating vectors (or vectors of vectors) dynamically

开发者 https://www.devze.com 2023-03-22 03:21 出处:网络
I need to dynamically allocate 1-D and 2-D arrays whose sizes are given at run-time. I managed to \"discover\" std::vector and I think it fits my purposes, but I would like to ask whether what I\'ve

I need to dynamically allocate 1-D and 2-D arrays whose sizes are given at run-time.

I managed to "discover" std::vector and I think it fits my purposes, but I would like to ask whether what I've written is correct and/or can be improved.

This is what I'm doing:

#include <vecto开发者_JAVA百科r>

typedef std::vector< std::vector<double> > matrix;

//... various code and other stuff

std::vector<double> *name = new std::vector<double> (size);
matrix *name2 = new matrix(sizeX, std::vector<double>(sizeY));


Dynamically allocating arrays is required when your dimensions are given at runtime, as you've discovered.

However, std::vector is already a wrapper around this process, so dynamically allocating vectors is like a double positive. It's redundant.

Just write (C++98):

#include <vector>

typedef std::vector< std::vector<double> > matrix;
matrix name(sizeX, std::vector<double>(sizeY));

or (C++11 and later):

#include <vector>

using matrix = std::vector<std::vector<double>>;
matrix name(sizeX, std::vector<double>(sizeY));


You're conflating two issues, dynamic allocation and resizable containers. You don't need to worry about dynamic allocation, since your container does that for you already, so just say it like this:

matrix name(sizeX, std::vector<double>(sizeY));

This will make name an object with automatic storage duration, and you can access its members via name[i][j].


What you're doing should basically work, however:

In general, don't dynamically allocate objects

If you want a vector, do this:

std::vector<double> vec(size);

not this:

std::vector<double>* vec = new std::vector<double>(size);

The latter gives you a pointer, which you have to delete. The former gives you a vector which, when it goes out of scope, cleans up after itself. (Internally, of course, it dynamically allocates objects, but the trick is that this is handled by the class itself, and you don't need to worry about it in your user code).


It is correct but could be made more efficient.

You could use the boost multidimensional arrays: http://www.boost.org/doc/libs/1_47_0/libs/multi_array/doc/user.html

Or, you can implement your own class for it and handle the indexing yourself. Perhaps something like this (which is not well tested):

#include <vector>
#include <cassert>
template <typename T, typename A = std::allocator<T> >
class Array2d
{
public:
    typedef Array2d<T> self;
    typedef std::vector<T, A> Storage;

    typedef typename Storage::iterator       iterator;
    typedef typename Storage::const_iterator const_iterator;
    Array2d() : major_(0), minor_(0) {}
    Array2d(size_t major, size_t minor)
        : major_(major)
        , minor_(minor)
        , storage_(major * minor)
    {}

    template <typename U>
    Array2d(size_t major, size_t minor, U const& init)
        : major_(major)
        , minor_(minor)
        , storage_(major * minor, u)
    {
    }
    iterator begin()                { return storage_.begin(); }
    const_iterator begin() const    { return storage_.begin(); }
    iterator end()                  { return storage_.end(); }
    const_iterator end() const      { return storage_.end(); }
    iterator begin(size_t major) {
        assert(major < major_);
        return storage_.begin() + (major * minor_);
    }
    const_iterator begin(size_t major) const {
        assert(major < major_);
        return storage_.begin() + (major * minor_);
    }
    iterator end(size_t major) {
        assert(major < major_);
        return storage_.begin() + ((major + 1) * minor_);
    }
    const_iterator end(size_t major) const {
        assert(major < major_);
        return storage_.begin() + ((major + 1) * minor_);
    }
    void clear() {
        storage_.clear();
        major_ = 0;
        minor_ = 0;
    }
    void clearResize(size_t major, size_t minor)
    {
        clear();
        storage_.resize(major * minor);
        major_ = major;
        minor_ = minor;
    }
    void resize(size_t major, size_t minor)
    {
        if ((major != major_) && (minor != minor_))
        {
            Array2d tmp(major, minor);
            swap(tmp);

            // Get minimum minor axis
            size_t const dist = (tmp.minor_ < minor_) ? tmp.minor_ : minor_;
            size_t m = 0;
            // copy values across
            for (; (m < tmp.major_) && (m < major_); ++m) {
                std::copy(tmp.begin(m), tmp.begin(m) + dist, begin(m));
            }
        }
    }
    void swap(self& other)
    {
        storage_.swap(other.storage_);
        std::swap(major_, other.major_);
        std::swap(minor_, other.minor_);
    }
    size_t minor() const {
        return minor_;
    }
    size_t major() const {
        return major_;
    }
    T*       buffer()       { return &storage_[0]; }
    T const* buffer() const { return &storage_[0]; }
    bool empty() const {
        return storage_.empty();
    }
    template <typename ArrRef, typename Ref>
    class MajorProxy
    {
        ArrRef arr_;
        size_t major_;

    public:
        MajorProxy(ArrRef arr, size_t major)
        : arr_(arr)
        , major_(major)
        {}

        Ref operator[](size_t index) const {
            assert(index < arr_.minor());
            return *(arr_.buffer() + (index + (major_ * arr_.minor())));
        }
    };
    MajorProxy<self&, T&>
    operator[](size_t major) {
        return MajorProxy<self&, T&>(*this, major);
    }
    MajorProxy<self const&, T const&>
    operator[](size_t major) const {
        return MajorProxy<self&, T&>(*this, major);
    }
private:
    size_t major_;
    size_t minor_;
    Storage storage_;
};


While the points the other answers made were very correct (don't dynamically allocate the vector via new, but rather let the vector do the allocation), if you are thinking terms of vectors and matrices (e.g. linear algebra), you might want to consider using the Eigen matrix library.


You don't allocate containers dynamically. They can automatically manage memory for you if they themselves are not manually managed.

A vector grows when you add new items with push_back (or insert), you can choose its size from the start with arguments to the constructor, and you can resize it later with the resize method.

Creating a vector of vectors with your sizes with the constructor looks like this:

std::vector< std::vector<double> > matrix(size, std::vector<double>(sizeY));

This means: size instances of a std::vector<double>, each containing sizeY doubles (initialized to 0.0).


Sometimes you don't want to fill your stack and your memory requirement is large. Hence you may want to use vector> created dynamically especially while creating a table of a given row and col values.

Here is my take on this in C++11

int main() {
    int row, col;
    std::cin >> row >> col;
    auto *arr = new std::vector<std::vector<int>*>(row);
    for (int i=0; i<row; i++) {
        auto *x = new std::vector<int>(col, 5);
        (*arr)[i] = x;
    }

    for (int i=0; i<row; i++) {
        for(int j=0; j<col; j++) {
            std::cout << arr->at(i)->at(j) << " ";
        }
        std::cout << std::endl;
    }
    return 0;
}


 #include < iostream > 
 #include < vector >

 using namespace std;

int main(){

vector<int>*v = new vector<int>();   // for 1d vector just copy paste  it

v->push_back(5);
v->push_back(10);
v->push_back(20);
v->push_back(25);

for(int i=0;i<v->size();i++){
    cout<<v->at(i)<<" ";
}
cout<<endl;

delete v;

system("pause");
return 0;

     }


If you don't need to resize the array sizes at run time, then you can just use standard arrays (allocated at runtime)!

However, if you do need to resize arrays at runtime, then you can use the following (revised) code:

#include <vector>

typedef std::vector< std::vector<double> > matrix;

//... various code and other stuff

std::vector<double> *name = new std::vector<double> (size);

matrix *name2 = new matrix(sizeX, std::vector<double>(sizeY));

In essence, all I've done is remove a single bracket (().

0

精彩评论

暂无评论...
验证码 换一张
取 消