Return 二维向量映射中在位置 [r][c] 处具有最大 int 的元素的键
Return key for element that has max int at location [r][c] from a map of 2d vectors
我有多个二维整数数组(所有维度相同)存储在一个映射中,每个数组包含一个分配给数组的唯一字符。例如,我有 std::map<std::array<std::array<int>> , char>
.
我正在尝试查找具有特定二维坐标的最大整数的数组,以及 return 地图中的关联字符。我对此的想法是 return 地图中在坐标 [r][c] 处具有最大 int 的元素,然后访问 element.second 获取字符。我这样做的尝试没有用。我可以使用循环来执行此操作,但它很慢并且希望尽可能避免。
我查看了以下链接,但它们与我的问题大相径庭,我无法使它们正常工作:
How to get min or max element in a vector of objects in c++, based on some field of the object?
How to get the minimum or maximum element in a vector of structures in C++, based on some field in the structure
感谢大家的帮助!
我尝试过的:
二维数组和字符的映射:
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;
}
包含二维数组和字符的对象数组:
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;
}
如果不查看地图中的每个二维数组是不可能做到这一点的,因为您需要比较所有这些数组才能找出给定坐标处的最大整数。 (使用循环是不可避免的)
考虑具有您描述的属性的映射 myMap
,即它具有类型 std::map<std::array<std::array<int>> , char>
并且已经填充。然后此代码将执行您想要的操作:
int max = minimum_int_value;
char myChar = '[=10=]';
for (auto const& entry : myMap) {
if (entry.first[r][c] > max) {
max = entry.first[r][c];
myChar = entry.second;
}
}
我有多个二维整数数组(所有维度相同)存储在一个映射中,每个数组包含一个分配给数组的唯一字符。例如,我有 std::map<std::array<std::array<int>> , char>
.
我正在尝试查找具有特定二维坐标的最大整数的数组,以及 return 地图中的关联字符。我对此的想法是 return 地图中在坐标 [r][c] 处具有最大 int 的元素,然后访问 element.second 获取字符。我这样做的尝试没有用。我可以使用循环来执行此操作,但它很慢并且希望尽可能避免。
我查看了以下链接,但它们与我的问题大相径庭,我无法使它们正常工作:
How to get min or max element in a vector of objects in c++, based on some field of the object?
How to get the minimum or maximum element in a vector of structures in C++, based on some field in the structure
感谢大家的帮助!
我尝试过的:
二维数组和字符的映射:
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;
}
包含二维数组和字符的对象数组:
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;
}
如果不查看地图中的每个二维数组是不可能做到这一点的,因为您需要比较所有这些数组才能找出给定坐标处的最大整数。 (使用循环是不可避免的)
考虑具有您描述的属性的映射 myMap
,即它具有类型 std::map<std::array<std::array<int>> , char>
并且已经填充。然后此代码将执行您想要的操作:
int max = minimum_int_value;
char myChar = '[=10=]';
for (auto const& entry : myMap) {
if (entry.first[r][c] > max) {
max = entry.first[r][c];
myChar = entry.second;
}
}