如何将std :: array实例地址作为参数传递给std :: min_element?

时间:2018-09-20 19:14:15

标签: c++ arrays c++11 gcc

我正在尝试从大块内存中的范围中读取最小值,我想为函数提供内存范围,然后找到最小元素。我需要这样做,因为我无法更改代码或使用动态内存分配。

我在Win 7中使用MinGW-W64-builds-4.3.5。

我在http://www.cplusplus.com/reference/algorithm/min_element/中看到了一个示例,但是它们使用的是C样式数组,我知道我可以将其用作指向内存地址的指针并执行指针算术以指示范围的结束。

SELECT CONVERT(energy USING utf8)
FROM tableenergy

我打算用std :: array做类似的事情,但是由于迭代器,我遇到编译器错误,有没有办法用std :: array做类似的事情。这是我的代码:

// min_element/max_element example
#include <iostream>     // std::cout
#include <algorithm>    // std::min_element, std::max_element

bool myfn(int i, int j) { return i<j; }

struct myclass {
  bool operator() (int i,int j) { return i<j; }
} myobj;

int main () {
  int myints[] = {3,7,2,5,6,4,9};

  // using default comparison:
  std::cout << "The smallest element is " << *std::min_element(myints,myints+7) << '\n';
  std::cout << "The largest element is "  << *std::max_element(myints,myints+7) << '\n';

  // using function myfn as comp:
  std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myfn) << '\n';
  std::cout << "The largest element is "  << *std::max_element(myints,myints+7,myfn) << '\n';

  // using object myobj as comp:
  std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myobj) << '\n';
  std::cout << "The largest element is "  << *std::max_element(myints,myints+7,myobj) << '\n';

  return 0;
}

这是我的编译器输出:

#define N_ELEMENTS (128u)
short int FindMinElement(const std::array<short int, N_ELEMENTS>& array)
{
    return *(std::min_element(array, array+N_ELEMENTS));
}

注意:由于我正在处理大型多维数组,因此我必须非常明确地说明地址范围,因此我不能使用std :: begin()或std :::结束()。同样,我无法使用向量,因为我需要使用静态内存分配而不是动态内存。

编辑:

感谢大家的回答,但是我在这里也有一个约束,正如我上面提到的,我传递给函数的数组大于> Executing task: g++.exe -Wall -g c:\Users\uidr8361\Desktop\C++\Tmp\ReadArcPixels.cpp < c:\Users\uidr8361\Desktop\C++\Tmp\ReadArcPixels.cpp: In function 'short int FindMinElement(const std::array<short int, 128>&)': c:\Users\uidr8361\Desktop\C++\Tmp\ReadArcPixels.cpp:41:55: error: no matching function for call to 'min_element(const std::array<short int, 128>&, const std::array<short int, 128>*)' return *(std::min_element(array, &array+N_ELEMENTS)); ^ In file included from C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/algorithm:62, from c:\Users\uidr8361\Desktop\C++\Tmp\ReadArcPixels.cpp:4: C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:5610:12: note: candidate: 'template<class _FIter> constexpr _FIter std::min_element(_FIter, _FIter)' inline min_element(_ForwardIterator __first, _ForwardIterator __last) ^~~~~~~~~~~ C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:5610:12: note: template argument deduction/substitution failed: c:\Users\uidr8361\Desktop\C++\Tmp\ReadArcPixels.cpp:41:55: note: deduced conflicting types for parameter '_FIter' ('std::array<short int, 128>' and 'const std::array<short int, 128>*') return *(std::min_element(array, &array+N_ELEMENTS)); ^ In file included from C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/algorithm:62, from c:\Users\uidr8361\Desktop\C++\Tmp\ReadArcPixels.cpp:4: C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:5635:5: note: candidate: 'template<class _FIter, class _Compare> constexpr _FIter std::min_element(_FIter, _FIter, _Compare)' min_element(_ForwardIterator __first, _ForwardIterator __last, ^~~~~~~~~~~ C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:5635:5: note: template argument deduction/substitution failed: c:\Users\uidr8361\Desktop\C++\Tmp\ReadArcPixels.cpp:41:55: note: deduced conflicting types for parameter '_FIter' ('std::array<short int, 128>' and 'const std::array<short int, 128>*') return *(std::min_element(array, &array+N_ELEMENTS)); ,并且由于类型转换。因此,@ PlinyTheElder解决方案对我来说效果很好,但是我正在寻找一种更现代的C ++(从C ++ 11起)的解决方案。

2 个答案:

答案 0 :(得分:3)

std::array具有beginend成员函数,这些成员函数使您可以迭代数组的开头和结尾。您可以改用类似的

#define N_ELEMENTS (128u)
short int FindMinElement(const std::array<short int, N_ELEMENTS>& array)
{
    return *std::min_element(array.begin(), array.end());
}

获取整个范围,或者如果需要子部分

#define N_ELEMENTS (128u)
short int FindMinElement(const std::array<short int, N_ELEMENTS>& array)
{
    return *std::min_element(array.begin(), array.begin() + N_ELEMENTS);
}

您可能还想考虑类似的东西

constexpr auto N_ELEMENTS = 128u;

代替

#define N_ELEMENTS (128u)

常量。

答案 1 :(得分:1)

当然可以!

您可以将指向数据类型的指针传递给std :: min_element函数-它们用作随机迭代器。您不需要使用begin()end()函数,任何指向数组的指针都可以使用。示例:

#include <array>
#include <algorithm>
#include <iostream>

int main()
{
    std::array<int,1000> arr;

    size_t size = arr.size();
    int* start = &arr[0];
    int* finish = start + size;

    for (int* i = start; i < finish; ++i) *i = rand();

    int* minP = std::min_element(start, finish);

    int minVal = *minP;

    std::cout << minVal;
}