Question

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

Was it helpful?

Solution

If you have 2d array a n x m, you can put it in a 1d array b n x m 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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top