gpt4 book ai didi

c++ - 编译错误 : loop through each element of an array inside a c++ function

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:24:43 26 4
gpt4 key购买 nike

我是 C++ 的新手,我试图在 C++ 函数中遍历数组的每个元素,但出现编译时错误。有人可以解释为什么会这样吗?

这是我的代码:

#include <iostream>

int getMax(int arr[])
{
int max = arr[0];
for (int x: arr){
if ( x > max ) max = x;
}
return max;
}


int main()
{

int arr[] = {4,2,5,3,6,8,1};
int max = getMax(arr);

std::cout << max << std::endl;

return 0;
}

这是错误信息:

get_max.cpp: In function 'int getMax(int*)':
get_max.cpp:6:17: error: 'begin' was not declared in this scope
for (int x: arr){
^
get_max.cpp:6:17: note: suggested alternative:
In file included from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/string:51:0,
from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/bits/locale_classes.h:40,
from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/bits/ios_base.h:41,
from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/ios:42,
from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/ostream:38,
from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/iostream:39,
from get_max.cpp:1:
C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/bits/range_access.h:87:5: note: 'std::begin'
begin(_Tp (&__arr)[_Nm])
^
get_max.cpp:6:17: error: 'end' was not declared in this scope
for (int x: arr){
^
get_max.cpp:6:17: note: suggested alternative:
In file included from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/string:51:0,
from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/bits/locale_classes.h:40,
from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/bits/ios_base.h:41,
from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/ios:42,
from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/ostream:38,
from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/iostream:39,
from get_max.cpp:1:
C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/bits/range_access.h:97:5: note: 'std::end'
end(_Tp (&__arr)[_Nm])

最佳答案

您必须通过引用传递数组以保留大小信息:

template <std::size_t N>
int getMax(const int (&arr)[N])
{
int max = arr[0];
for (int x: arr){
if ( x > max ) max = x;
}
return max;
}

否则您必须使用指针传递大小。

int getMax(const int *arr, std::size_t size)
{
assert(arr != nullptr);
assert(size != 0);
return *std::max_element(arr, arr + size);
}

使用 std::vector/std::array 具有更自然的语法。

int getMax(const std::vector<int>& arr)
{
assert(!arr.empty());
#if 1
return *std::max_element(arr.begin(), arr.end());
#else
int max = arr[0];
for (int x: arr){
if ( x > max ) max = x;
}
return max;
#endif
}
int main()
{
const std::vector<int> arr{4,2,5,3,6,8,1};
const int max = getMax(arr);

std::cout << max << std::endl;
}

关于c++ - 编译错误 : loop through each element of an array inside a c++ function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54304530/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com