创建一个可以接收board1 [{1,1}] ='X'的类; ?(方括号内的花括号)

Tom*_*mer 4 c++ brackets curly-braces square-bracket

我得到HW,在main.cpp的一行中,我想支持:

board1[{1,1}]='X';
Run Code Online (Sandbox Code Playgroud)

这背后的逻辑意义是在(1,1)的位置将"X"分配给"游戏板".我不知道如何创建一个接收大括号的数组,如[{int,int}].

我怎样才能做到这一点?

PS因为这些是符号而不是字符(因为我不认识任何属于这个问题的术语)在谷歌搜索这类问题非常困难,所以这可能是重复的:-(,希望不是.

我试着这样做:

第一次尝试:

vector<vector<int> > matrix(50);
for ( int i = 0 ; i < matrix.size() ; i++ )
    matrix[i].resize(50);
matrix[{1,1}]=1;
Run Code Online (Sandbox Code Playgroud)

第二次尝试:

int mat[3][3];
//maybe map
mat[{1,1}]=1;
Run Code Online (Sandbox Code Playgroud)

第3次尝试:

class _mat { // singleton
    protected:
       int i ,j;

    public:
        void operator [](string s)
        {
            cout << s;
        }
};

_mat mat;
string s = "[{}]";
mat[s]; //this does allow me to do assignment also the parsing of the string is a hustle
Run Code Online (Sandbox Code Playgroud)

Tyk*_*ker 7

你需要做一些事情:

    struct coord {
        int x;
        int y;
    };

    class whatever
    {
        public:
            //data being what you have in your board
            data& operator[] (struct coord) {
                //some code
            }
    };
Run Code Online (Sandbox Code Playgroud)