繁体   English   中英

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

[英]How to pass std::array instance address as parameter to std::min_element?

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

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

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

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

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

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

这是我的编译器输出:

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

注意 :由于要使用大型多维数组,因此我必须非常详细地说明地址范围,因此不能使用std :: begin()或std :: end()。 我也不能使用向量,因为我需要使用静态内存分配而不是动态的。

编辑:

谢谢大家的回答,但是我在这里也有一个约束,正如我上面提到的,我传递给函数的数组比N_ELEMENTS大,并且由于类型转换而导致编译错误。 所以@PlinyTheElder解决方案对我来说很好用,但我正在寻找更现代的C ++(从C ++ 11起)的解决方案。

std::array具有beginend成员函数,这些函数使您可以迭代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)

常量

当然可以!

您可以将指向数据类型的指针传递给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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM