문제

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 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];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top