如何查找数组的最大值

时间:2011-08-25 12:11:31

标签: c++ visual-c++

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

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

等等。

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

1 个答案:

答案 0 :(得分:2)

接受在标准c ++中执行此操作的挑战,这里什么也没做。

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

我提供了几个版本,从标准的c ++ 98代码开始(它有一些kludges,主要用于将数据初始化为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类型扣除
  • 更少的typedef将事物联系在一起

注意不幸的是,因为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

  • 更简洁的代码
  • 允许直接在非STL容器(如C样式数组)上运行。 这会导致reduce_minelement 中的类型扣除稍微复杂一些。抽象是有代价的;)

注意数据(s_data)已被省略,因为它与上述相同;您可以从其他示例中删除其中一个s_data定义,然后编译并运行。

注意没有在线编译器服务支持Boost库头,因此您只能在家中看到 live

#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类型),但实际实施并不介意您是否抛出floatstd::stringintegers或者实际上,任何类似的类型。

演示,基于最新版本:

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;
}

输出:

  

,月亮,

Q.E.D。

希望这对你来说很有趣。这是给我的。