ash ash - 9 months ago 26
C++ Question

How do I map matrices to some integer value?

For example, I want to map

1 2 3
3 4 1 -> 0
1 1 1

1 2 3
1 1 2 -> 1
1 1 1


and so on. Is it possible using std::map?

EDIT: I mean to ask if I have a 2-D array, or an array of 1-D vectors, how do I map it with some integer values.

P0W P0W
Answer Source

Use std::array of std::array like :

std::map< int, std::array<std::array< int , 3>, 3> > m = 
{ {0, {{ { 1,2,3},
         { 3,4,1},
         { 1,1,1} 
      }} 
  }, 
  {1, {{ 
         { 1,2,3},
         { 1,1,2},
         { 1,1,1} 
      }} 
  } 
};

You may use std::vector of std::vector, if rows and columns are not known at compile time

std::map< int, std::vector<std::vector< int > > > m;

For opposite mapping matrix to integer you can just reverse the order :

std::map< std::array<std::array< int , 3>, 3>, int  > m_opp =
{ { {{ { 1,2,3},
         { 3,4,1},
         { 1,1,1} 
      }}, 
    0 
  }, 
  { {{ 
         { 1,2,3},
         { 1,1,2},
         { 1,1,1} 
      }}, 
    1 
  } 
};

or

std::map< std::vector<std::vector< int > >, int > m_opp;