简体   繁体   中英

How to use random_shuffle to shuffle a 2D array C++

I have a 2D vector of objects and I want to shuffle them. What is the syntax for this?

    class node{
    public:
        string hex_type = "E";// empty to start
        int Xcoordinate;
        int Ycoordinate;
        vector<reference_wrapper<node>> neighbors;


    };
nodes = vector<vector<node> >(size, vector <node>(size));//filled elsewhere
random_shuffle(&nodes[0][0], &nodes[size-1][size-1]);

This generates an error that is is an access violation reading

If you have 2d array anxm, you can put it in a 1d array bnxm like this:

b[i * m + j] = a[i][j];

Then you can shuffle array b and user reverse transformation

a[i / m][i % m] = b[i];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM