I have multiple 2d arrays (all same dimension) of integers that are stored within a map, each array contains a unique character that is assigned to the array. E.g., I have std::map<std::array<std::array<int>> , char>
.
I am trying to find the array that has the maximum integer for a specific 2d coordinate, and return the associated character from the map. My thought for this would be to return the element in the map that has the maximum int at coordinate [r][c], then access element.second for the char. My attempts to do this have been of no avail. I can do this using a loop, but it is slow and would like to avoid if possible.
I've reviewed the following links, but they are different enough from my question that I cannot get them to work:
How to get min or max element in a vector of objects in c++, based on some field of the object?
Thank you for all of your help!
What I've tried:
Map of 2d arrays and chars:
int main(void)
{
std::array<std::array<int, 3>, 3> arr1 =
{
{
{4, 7, 3},
{2, 6, 0},
{6, 4, 7}
}
};
std::array<std::array<int, 3>, 3> arr2 =
{
{
{5, 8, 2},
{8, 3, 1},
{5, 3, 9}
}
};
std::map<std::array<std::array<int, 3>, 3>, char> myMap;
myMap.insert({ arr1, 'A' });
myMap.insert({ arr2, 'B' });
// Somehow access char that is associated with the array that has max int at location[r][c]
// Anything I've tried with std::max_element does not work here
return 0;
}
Array of objects that hold 2d arrays and chars:
class testClass
{
private:
std::vector<std::vector<int>> myVector;
public:
int score;
char mainChar;
testClass(std::vector<std::vector<int>> inpVector, int inpScore, char inpChar)
{
this->myVector = inpVector;
score = inpScore;
mainChar = inpChar;
}
int returnItem(int row, int col)
{
return myVector[row][col];
}
};
// A pred function to adjust according to your score
bool comparator(const testClass& s1, const testClass& s2)
{
return s1.score < s2.score;
}
int main(void)
{
std::vector<testClass> testVector;
testClass vectorOne
(
{
{ 1,2,3 },
{ 4,5,6 }
}
, 1, 'a');
testClass vectorTwo
(
{
{ 1,2,3 },
{ 4,5,6 }
}
, 2, 'b');
testVector.push_back(vectorOne);
testVector.push_back(vectorTwo);
std::cout << testVector[0].returnItem(0,0);
auto element = std::max_element(testVector.begin(),
testVector.end(), comparator);// I don't know how to get element [r][c] here
return 0;
}
Aucun commentaire:
Enregistrer un commentaire