You need a function that accepts a column number and loops through each row calculating the maximum for that column.
You only need one loop because you can access whichever column you want by indexing the vector like an array (as long as you check that the vector has enough elements)
There are two ways to do this: the C++17 way where you can assign objects with multiple values directly to multiple variables, and the old way where you reference the members of std::pair instead.
The C++17 way:
#include <map>
#include <vector>
#include <utility>
#include <iostream>
std::pair<int,float> column_max(const auto & m, int column)
{
int index = -1;
float maximum = -std::numeric_limits<float>::max();
for (const auto & [key,value] : m)
if (value.size() > column && value[column] > maximum)
{
index = key;
maximum = value[column];
}
return {index,maximum};
}
int main()
{
const std::map<int,std::vector<float>> m =
{
{0, { 1, 5, 10, 22}},
{1, {31, 5, 10, 12}},
{2, { 1, 15, 18, 12}}
};
for (int i=0; i<4; i++)
{
const auto [index,maximum] = column_max(m,i);
std::cout << "#" << i << ": " << maximum << " " << index << "\n";
}
return 0;
}
Try it here: https://onlinegdb.com/O5dmPot34
And the equivalent, but older way:
#include <map>
#include <vector>
#include <utility>
#include <iostream>
#include <float.h>
std::pair<int,float> column_max(const std::map<int,std::vector<float>> & m, int column)
{
int index = -1;
float maximum = -FLT_MAX;
for (std::map<int,std::vector<float>>::const_iterator entry=m.begin(); entry!=m.end(); entry++)
if (entry->second.size() > column && entry->second[column] > maximum)
{
index = entry->first;
maximum = entry->second[column];
}
return std::make_pair(index,maximum);
}
int main()
{
const std::map<int,std::vector<float>> m =
{
{0, { 1, 5, 10, 22}},
{1, {31, 5, 10, 12}},
{2, { 1, 15, 18, 12}}
};
for (int i=0; i<4; i++)
{
std::pair<int,float> value = column_max(m,i);
std::cout << "#" << i << ": " << value.second << " " << value.first << "\n";
}
return 0;
}
Try it here: https://onlinegdb.com/273dnHRZK