如何找到一组数组的最大值

How to find the max value of group of arrays

本文关键字:一组 数组 最大值 何找      更新时间:2023-10-16

我有六个float类型的数组。对我来说,所有这些数组中的任何特定索引都代表了一组值。例如,如果索引是0,所有六个数组的0索引代表一组值,我需要从中找到最大值。类似地,对于所有其他索引也是如此。

array1  array2  array3  array4  array5  array6    
0       0       0       0       0       0 
1       1       1       1       1       1

等等

我需要找出所有这些数组中特定索引的最大值,并将其存储在另一个数组或列表中

要在标准c++中做到这一点,这里什么都没有。

我试图使算法的实现,reduce_minelement尽可能通用(不假设任何特定的容器类型或值数组的数量等)。

我提供了几个版本,从标准c++98代码开始(其中有一些包,主要是为了将数据初始化到STL容器中)。

标准c++ 98

现场观看:http://ideone.com/2BlsK

#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
template <typename Containers>
    std::vector<typename Containers::value_type::value_type> 
        reduce_minelement(const Containers& containers)
{
    typedef typename Containers::value_type slice_t;
    typedef typename Containers::const_iterator slice_ci;
    typedef typename slice_t::value_type element_t;
    std::vector<element_t> result;
    result.reserve(containers.size()); // pre-allocated
    for (slice_ci it=containers.begin(); it!=containers.end(); ++it)
    {
        result.push_back(*std::max_element(it->begin(), it->end()));
    }
    return result;
}
typedef int ints_t[6];
static const ints_t s_data[] = { 
    { 19152,     1, 21193, 17574,  8484, 30333 },
    { 20189, 18837, 30734,     2, 22440,  3534 },
        { 3, 26118, 19367, 17877, 24605,  7838 },
    { 30885, 20135,    -4, 31316, 11838,  8926 },
    { 26830, 20209, 27286, 16105, 16601, 28304 },
    { 10208, 28062, 15612, 26270, 19234, 21326 },
     { 5208, 17473,  3383, 15659, 32494, 24231 },
    { 31685, 22500, 18860, 21318, 18893, 21385 },
    { 14295, 17163,  8920, 15986, 13448, 21143 },
    { 20199,  8954,   599, 17459,  3884,  8634 },
    { 16768, 20563,  6727, 26305, 11053,  6418 },
     { 7446,  6853,  5283,  6193, 28291,  4205 },
    { 27056, 17514,  5359, 29656, 10910,  6034 },
    { 21984,  1261,  2404, 17644, 25969,  1735 },
      { 797,  8457, 23584, 29363, 26362, 17383 },
      { 768, 11018, 14991,     0, 28720,  6159 },
};
int main()
{
    std::vector<std::vector<int> > data;
    for (const ints_t *it=s_data; it!=s_data+(sizeof(s_data)/sizeof(*s_data)); ++it)
        data.push_back(std::vector<int>(*it+0, *it+sizeof(*it)/sizeof(**it)));
    std::vector<int> reduced = reduce_minelement(data);
    std::copy(reduced.begin(), reduced.end(), std::ostream_iterator<int>(std::cout, ", "));
    return 0;
}

普通标准c++ 11

编译支持c++0x。好处是

  • 更灵活(现在可以是锯齿数组,参见演示数据)
  • 易读性(基于范围的for而不是迭代器循环),auto类型演绎
  • 更少的类型将事物联系在一起

注意不幸的是,因为codepad.org/ideone.com使用的是gcc 4.5.1编译器,基于范围的for还不支持;查看稍微修改的实时版本:http://ideone.com/xevL0

#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
template <typename Containers>
    std::vector<typename Containers::value_type::value_type>
         reduce_minelement(const Containers& containers)
{
    std::vector<typename Containers::value_type::value_type> result;
    result.reserve(containers.size()); // pre-allocate
    for (auto& slice: containers)
        result.push_back(*std::max_element(slice.begin(), slice.end()));
    return result;
}
static const std::vector<std::vector<int> > data = { 
       { 52,    1, 93, 74 },
        { 2,   18, 67, 77 },
       { 85,   35, -4     },
       { 48 },
       { 68,   18, 91,  0 },
};
int main()
{
    auto reduced = reduce_minelement(data);
    std::copy(reduced.begin(), reduced.end(), std::ostream_iterator<int>(std::cout, ", "));
    return 0;
}

c++ 0x与Boost Range

    更简洁的代码
  • 允许直接操作非stl容器(如c风格数组)。这会导致reduce_minelement 中稍微复杂一些的类型推导。抽象是有代价的;)

数据(s_data)已被省略,因为它与上述相同;您可以从其他示例中插入s_data定义中的任何一个,它将编译并运行。

注意没有在线编译器服务支持Boost库头文件,所以你只能在家里看到实时

#include <vector>
#include <boost/range.hpp>
#include <boost/range/algorithm.hpp>
#include <iterator>
using boost::range_value;
using boost::range_iterator;
template <typename Containers>
    std::vector<typename range_value<typename range_value<Containers>::type>::type> 
         reduce_minelement(const Containers& containers)
{
    std::vector<typename range_value<typename range_value<Containers>::type>::type> result;
    result.reserve(boost::size(containers)); // pre-allocate
    for (auto& slice: containers)
        result.push_back(*boost::max_element(slice));
    return result;
}
typedef int ints_t[6];
static const ints_t s_data[] = { 
    { 19152,     1, 21193, 17574,  8484, 30333 },
    { 20189, 18837, 30734,     2, 22440,  3534 },
        { 3, 26118, 19367, 17877, 24605,  7838 },
    { 30885, 20135,    -4, 31316, 11838,  8926 },
    { 26830, 20209, 27286, 16105, 16601, 28304 },
    { 10208, 28062, 15612, 26270, 19234, 21326 },
     { 5208, 17473,  3383, 15659, 32494, 24231 },
    { 31685, 22500, 18860, 21318, 18893, 21385 },
    { 14295, 17163,  8920, 15986, 13448, 21143 },
    { 20199,  8954,   599, 17459,  3884,  8634 },
    { 16768, 20563,  6727, 26305, 11053,  6418 },
     { 7446,  6853,  5283,  6193, 28291,  4205 },
    { 27056, 17514,  5359, 29656, 10910,  6034 },
    { 21984,  1261,  2404, 17644, 25969,  1735 },
      { 797,  8457, 23584, 29363, 26362, 17383 },
      { 768, 11018, 14991,     0, 28720,  6159 },
};
int main()
{
    boost::copy(reduce_minelement(s_data), std::ostream_iterator<int>(std::cout, ", "));
    return 0;
}

泛型:字符串呢?

这三个版本对于元素的类型都是完全泛型的。其他一些地方可能需要调整(如ostream_iterator类型),但实际实现并不介意您是否抛出float s, std::string s, integers或任何类似的类型。

基于最新版本的演示:

typedef std::string elements_t[3];
static const elements_t s_data[] = { 
    { "the", "quick", "fox" },
    { "jumped", "over", "the" },
    { "lazy", "blue", "moon" },
};
int main()
{
    boost::copy(reduce_minelement(s_data), 
       std::ostream_iterator<std::string>(std::cout, ", "));
    return 0;
}
输出:

the, the, moon,

Q.E.D.

希望这对你来说很有趣。