Search code examples
c++bracketscurly-bracessquare-bracket

creating a class that can receive board1[{1,1}]='X'; ? (curly brackets inside square brackets)


I got H.W. that in one of the lines of the main.cpp I am suppose to support:

board1[{1,1}]='X';

the logical meaning behind this is to assign to a "game board" the char 'X' at the position of (1,1). I have no clue how to create an array that receives curly brackets such as [{int,int}].

How can I do this?

P.S. since these are symbols and not chars (and since I don't recognize any term that belongs to this problem) it is very difficult searching for this type of problem in google, so this might be a duplicate :-( , hope not.

I tried to do:

first try:

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

2nd try:

int mat[3][3];
//maybe map
mat[{1,1}]=1;

3rd try:

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

Solution

  • you need to do something like:

        struct coord {
            int x;
            int y;
        };
    
        class whatever
        {
            public:
                //data being what you have in your board
                data& operator[] (struct coord) {
                    //some code
                }
        };