4

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
Tomer
  • 531
  • 7
  • 19
  • 2
    Are you familiar with operator overloading? – StoryTeller - Unslander Monica May 02 '18 at 09:07
  • Don't just ask us to do your homework without posting something that you've tried. It'll get downvoted faster than North Koreas nuclear program. – UKMonkey May 02 '18 at 09:07
  • @UKMonkey Hey I am not asking to do my H.W. this is just a very small part of the HW and also I didnt ask to solve it just to know how am I suppose to work on it... I will add that part that I tried to do. By the way I agree with you that people shouldnt just ask for others to do their HW, I just wanted to explain why my situation is diff. – Tomer May 02 '18 at 09:12
  • @StoryTeller yes but this is suppose to overload also the brackets and also the assignment operator... (also I was not sure that I am suppose to do overloading) but now I will check how to overload both operators together – Tomer May 02 '18 at 09:13
  • Are you familiar with any of the standard library containers? Particularly `std::map`? – Caleth May 02 '18 at 09:16
  • 1
    the `{}` are a shortcut for this class http://en.cppreference.com/w/cpp/utility/initializer_list if you're meant to overload the operator specifically; but it can also be passed into the constructor of other classes (see answer below) – UKMonkey May 02 '18 at 09:19
  • @UKMonkey `{}` could also be aggregate initialization – Caleth May 02 '18 at 09:20

2 Answers2

7

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
            }
    };
Tomer
  • 531
  • 7
  • 19
Tyker
  • 2,971
  • 9
  • 21
2

Your first attempt, was pretty close to working actually. The issue is that the [] operator for the vector takes an integer index into where in the vector you want to change (and the vector must be large enough for it to exist). What you wanted however is a map; which will create the item and assign it for you. Thus a std::map<std::vector<int>, char> would've got you what you wanted. (although it might not have the best performance).

Your second attempted failed for the same reason as the first (index needs to be an integer) and the 3rd is corrected by the answer by Tyker.

UKMonkey
  • 6,941
  • 3
  • 21
  • 30
  • Using a std::pair or std::tuple as the map key might be more suitable, as each position within a board will have the same number of dimensions. – Sean Burton May 02 '18 at 11:49
  • @SeanBurton Using a custom class is probably more suitable yet; but since the example below demonstrates that; and I was answering to improve the understanding on why the attempts didn't work I didn't see the point in mentioning it. – UKMonkey May 02 '18 at 11:54