Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用STL算法在表(向量向量,2D数组)中查找最小/最大值的优雅方法_C++_Algorithm_Stl - Fatal编程技术网

C++ 使用STL算法在表(向量向量,2D数组)中查找最小/最大值的优雅方法

C++ 使用STL算法在表(向量向量,2D数组)中查找最小/最大值的优雅方法,c++,algorithm,stl,C++,Algorithm,Stl,假设我们有一个向量向量,希望找到一个最小(或最大)值(不需要知道它们在表中的位置)。实现这一目标的优雅方式是什么 下面包含的显而易见的解决方案是在循环中的每一行运行std::min_元素。但是,是否可以使用一条语句而不用循环使用lambda函数呢 注意,已经有一个类似的问题了,但实际上并不是关于我在这里要问的问题 更新:理想的解决方案是只使用STL,但若不能做到这一点,那个么看看其他选项会很有趣 #include <algorithm> #include <iostream&g

假设我们有一个向量向量,希望找到一个最小(或最大)值(不需要知道它们在表中的位置)。实现这一目标的优雅方式是什么

下面包含的显而易见的解决方案是在循环中的每一行运行std::min_元素。但是,是否可以使用一条语句而不用循环使用lambda函数呢

注意,已经有一个类似的问题了,但实际上并不是关于我在这里要问的问题

更新:理想的解决方案是只使用STL,但若不能做到这一点,那个么看看其他选项会很有趣

#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
    std::vector<std::vector<double> > test_array=
         {
          {1., 2., 3., 4.},
          {7., 4., 6., 8.},
          {5., -1., 3., 1}
         };
    double min_val(test_array[0][0]);
    double max_val(test_array[0][0]);
    for(auto& row : test_array)
    {
        min_val = std::min(min_val, *std::min_element(row.begin(), row.end()));
        max_val = std::max(max_val, *std::max_element(row.begin(), row.end()));
    }

    cout << "Minimum = " << min_val << std::endl;
    cout << "Maximum = " << max_val << std::endl;
    return 0;
}
#包括
#包括
#包括
int main()
{
std::向量测试数组=
{
{1., 2., 3., 4.},
{7., 4., 6., 8.},
{5., -1., 3., 1}
};
双最小值(测试数组[0][0]);
双最大值(测试数组[0][0]);
用于(自动行:测试数组(&W)
{
min_val=std::min(min_val,*std::min_元素(row.begin(),row.end());
max_val=std::max(max_val,*std::max_元素(row.begin(),row.end());
}

cout有多个选项,例如,以下选项使用
std::accumulate
分别返回包含最小和最大元素的一对数字:

auto res = std::accumulate(a.begin(), a.end(), std::make_pair(a[0][0], a[0][0]),
    [](const auto& current, const auto& v) {
        auto minmax = std::minmax_element(v.begin(), v.end());
        return std::make_pair(std::min(current.first, *minmax.first),
                              std::max(current.second, *minmax.second));
    });
现场演示:

使用,您可能会有一个带有
范围::视图::加入的展平视图

std::vector<std::vector<double> > test_array =
{
    {1., 2., 3., 4.},
    {7., 4., 6., 8.},
    {5., -1., 3., 1}
};
auto flatten_view = test_array | ranges::view::join;
const auto p = std::minmax_element(begin(flatten_view), end(flatten_view));
std::cout << "Minimum = " << *p.first << std::endl;
std::cout << "Maximum = " << *p.second << std::endl;
std::向量测试\u数组=
{
{1., 2., 3., 4.},
{7., 4., 6., 8.},
{5., -1., 3., 1}
};
自动展平视图=测试数组|范围::视图::连接;
const auto p=std::minmax_元素(开始(展平视图),结束(展平视图));

问题是算法返回一个迭代器,因此不能执行
std::max_元素(begin(array)、end(array)、0、[](){})
因为您需要返回向量上的迭代器,而不是元素上的迭代器:/With,有
ranges::view::join
将范围范围视为展平范围,那么一个简单的
std::minmax_元素就足够了。@Jarod42有机会根据它得到答案吗?@MatthieuBrucher:不确定OP使用e是否合适xternal lib。我会投票支持@Jarod42。找到一种方法,通过一个迭代器来查看向量向量。这可以通过一个了解所有向量的迭代器来完成。或者你删除向量的向量来获得一个简单的向量。有趣的解决方案。我接受的答案是形式正确的。它基本上回答了我的问题——这是可能的,但答案是正确的ult既不优雅也不容易理解。谢谢。@1_2_3同意,我们不能两者兼得。希望ranges能进入C++20。谢谢。我对你的解决方案投了更高的票,因为它确实优雅且易于理解,尽管它目前在STL之外。嗯,我想一个人不能两者兼得。