C++ 如何获得2D std::vector给定列的最大值?

C++ 如何获得2D std::vector给定列的最大值?,c++,vector,C++,Vector,我知道如何获得2D std::vector行的最大值,如下所示: std::vector<std::vector<int>> a = {{ 1, 45, 54}, {71, 76, 12 }, {74451, 756, 125 }};} std::cout << " The max value of row 0 is: " << *max_element(begin(a[0]), end(a[0])); 如何调用max_元素

我知道如何获得2D std::vector行的最大值,如下所示:

std::vector<std::vector<int>> a = {{ 1, 45, 54}, {71, 76, 12 }, {74451, 756, 125 }};}

std::cout << " The max value of row 0 is: " << *max_element(begin(a[0]), end(a[0]));
如何调用max_元素来获取二维向量列的最大值?不将列复制到一维向量中


谢谢

假设需要查找其最大值的列是c。然后,您可以遍历该列

int m = a[0][c]; // initialize the maximum to be value of column c in first row.
for(int i = 1; i < a.size(); i++){ // loop through all rows.
    m = max(m, a[i][c]);
}
cout<<"Maximum value in column "<<c<<" is "<<m<<endl;

假设需要求其最大值的列是c。然后,您可以遍历该列

int m = a[0][c]; // initialize the maximum to be value of column c in first row.
for(int i = 1; i < a.size(); i++){ // loop through all rows.
    m = max(m, a[i][c]);
}
cout<<"Maximum value in column "<<c<<" is "<<m<<endl;
作为std::max_元素的替代方法,您可以使用向量迭代器的向量对任何列进行迭代,并找到最大值,如:

int maxValue(const std::vector<std::vector<int>> &a, size_t col)
{
    int max_value = 0; //assuming non negative values in the vector

    try
    {
        for (auto it = a.begin(); it != a.end(); it++)
            max_value = it->at(col) > max_value ? it->at(col) : max_value;
    }
    catch (std::out_of_range &e)
    {
        return -1; // or whatever you'd like
    }

    return max_value;
}

int main()
{
    std::vector<std::vector<int>> a = {{1, 45, 54}, {71, 76, 12}, {74451, 756, 125}};

    std::cout << maxValue(a, 0) << " "; // first column
    std::cout << maxValue(a, 1) << " "; // second column
    std::cout << maxValue(a, 2) << " "; // third column
    std::cout << maxValue(a, 3);        // out of range
}
作为std::max_元素的替代方法,您可以使用向量迭代器的向量对任何列进行迭代,并找到最大值,如:

int maxValue(const std::vector<std::vector<int>> &a, size_t col)
{
    int max_value = 0; //assuming non negative values in the vector

    try
    {
        for (auto it = a.begin(); it != a.end(); it++)
            max_value = it->at(col) > max_value ? it->at(col) : max_value;
    }
    catch (std::out_of_range &e)
    {
        return -1; // or whatever you'd like
    }

    return max_value;
}

int main()
{
    std::vector<std::vector<int>> a = {{1, 45, 54}, {71, 76, 12}, {74451, 756, 125}};

    std::cout << maxValue(a, 0) << " "; // first column
    std::cout << maxValue(a, 1) << " "; // second column
    std::cout << maxValue(a, 2) << " "; // third column
    std::cout << maxValue(a, 3);        // out of range
}

您可以做的是为std::max_元素提供一个用户定义的比较器,该比较器通过搜索所需列中的值来比较a中的每个std::vector

像这样:

std::vector<std::vector<int>> a = {{ 1, 45, 54}, {71, 76, 12 }, {74451, 756, 125 }};

std::cout << " The max value of row 0 is: " << *max_element(begin(a[0]), end(a[0])) << '\n';

auto max_col_0 = (*std::max_element(begin(a), end(a), [](auto& a, auto& b){ return a[0] < b[0]; }))[0];
auto max_col_1 = (*std::max_element(begin(a), end(a), [](auto& a, auto& b){ return a[1] < b[1]; }))[1];

std::cout << " The max value of col 0 is: " << max_col_0 << '\n';
std::cout << " The max value of col 1 is: " << max_col_1 << '\n';
std::max_元素函数使用向量a存储的每个向量调用比较器,以便可以比较每个向量中较小的相关列


返回的迭代器指向获胜的std::vector,在取消引用迭代器后,您需要从中提取获胜列。

您可以做的是为std::max_元素提供一个用户定义的比较器,该比较器根据要搜索的所需列中的值对a中的每个std::vector进行比较

像这样:

std::vector<std::vector<int>> a = {{ 1, 45, 54}, {71, 76, 12 }, {74451, 756, 125 }};

std::cout << " The max value of row 0 is: " << *max_element(begin(a[0]), end(a[0])) << '\n';

auto max_col_0 = (*std::max_element(begin(a), end(a), [](auto& a, auto& b){ return a[0] < b[0]; }))[0];
auto max_col_1 = (*std::max_element(begin(a), end(a), [](auto& a, auto& b){ return a[1] < b[1]; }))[1];

std::cout << " The max value of col 0 is: " << max_col_0 << '\n';
std::cout << " The max value of col 1 is: " << max_col_1 << '\n';
std::max_元素函数使用向量a存储的每个向量调用比较器,以便可以比较每个向量中较小的相关列



返回的迭代器指向获胜的std::vector,在取消引用迭代器后,您需要从中提取获胜列。

通常,不要使用向量的向量来实现2D向量如果其中一行的长度不同会发生什么情况?您可以使用for循环遍历数组的该列。您是希望元素有一个迭代器,还是只需要值?a可以是空的吗?任何内部向量都可以是空的吗?一般来说,不要使用向量向量来实现2D向量。如果其中一行的长度不同,会发生什么情况?您可以使用for循环遍历数组的该列。您是希望元素有一个迭代器,还是只需要值?a可以是空的吗?任何内部向量都可以是空的吗?如果a是空的,或者a中的第一个内部向量是空的,会发生什么情况?这也不会向查找到的元素返回迭代器,这是查找特定列的max元素的正常方法。我认为OP有一个非零常量大小的2D数组。另外,OP想要的是实际的最大值,而不是最大值的迭代器。由于内部和外部容器都是一个向量,我希望有一个健壮的算法来处理其中任何一个都是空的情况——但你可能是对的。我觉得我的眼睛太累了,我认为你的假设是可以的。@TEDLYNMO谢谢!如果a为空或a中的第一个内部向量为空,会发生什么情况?这也不会向查找到的元素返回迭代器,这是查找特定列的max元素的正常方法。我认为OP有一个非零常量大小的2D数组。另外,OP想要的是实际的最大值,而不是最大值的迭代器。由于内部和外部容器都是一个向量,我希望有一个健壮的算法来处理其中任何一个都是空的情况——但你可能是对的。我觉得我的眼睛太累了,我认为你的假设是可以的。@TEDLYNMO谢谢@泰德:哈哈,那可能会给我带来麻烦,泰。不客气!我正要写排印错误,但不再有趣了:-@TedLyngmo lol,那可能会给我带来麻烦,泰。不客气!我正要写排版,但不再有趣了:-