如何将std :: array实例地址作为参数传递给std :: min_element? [英] How to pass std::array instance address as parameter to std::min_element?
问题描述
我正在尝试从一大块内存中的范围中读取最小值,我想为函数提供内存范围,然后找到最小值。我需要这样做,因为我无法更改代码或使用动态内存分配。
I'm trying to read the minimum value from a range in a big chunk of memory, I want to provide the memory range to a function and then find the minimum element. I need to do it this way because I can not change the code or use dynamic memory allocation.
我在Win中使用MinGW-W64-builds-4.3.5 7。
I'm using MinGW-W64-builds-4.3.5 in Win 7.
我在 http://www.cplusplus.com/reference/algorithm/min_element/ ,但是它们使用的是C样式数组,我知道我可以将其用作指向内存地址的指针并执行指针算术以指示范围的结束。
I saw an example in http://www.cplusplus.com/reference/algorithm/min_element/ but they are using C style arrays which I know I can use as pointers to memory addresses and perform pointer arithmetic to indicate the end of the range.
// 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做类似的事情。这是我的代码:
I'm tyring to do something similar with std::array but I'm getting compiler errors, due to the iterators, is there a way to do something similar with std::array. Here is my code:
#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()
Note: I have to be very specific with the range of addresses since I'm working with a big multi-dimensional array so I can not use std::begin() or std::end(). Also I cannot work with vector since I'm need to use static memory allocation and not dynamic.
EDIT:或std :: end()。由于我需要使用静态内存分配而不是动态内存,因此我也无法使用vector。 strong>
感谢大家的回答,但我也有一个缺点如此处所述,我传递给该函数的数组大于 N_ELEMENTS
,并且由于类型转换,这还会导致编译错误。因此@PlinyTheElder解决方案对我来说很好用,但我正在寻找一种更现代的C ++(从C ++ 11开始)解决方案。
Thanks everybody for your answers but I'm also having a constraint here, as I mentioned above the array I'm passing to the function is bigger than the N_ELEMENTS
and that also causes a compilation error due to the type conversion. So @PlinyTheElder solution worked fine for me but I'm looking for a more modern C++ (C++11 onwards) like solution.
推荐答案
当然可以!
您可以将指向数据类型的指针传递给std :: min_element函数-它们用作随机迭代器。您不需要使用 begin()
和 end()
函数,任何指向数组的指针都可以使用。示例:
You can pass pointers to your data type into the std::min_element function - they serve as random iterators. You don't need to use the begin()
and end()
functions, any pointers into the array will work. Example:
#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;
}
这篇关于如何将std :: array实例地址作为参数传递给std :: min_element?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!