簡體   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