英文:
Get size of an array in C++ 17 using pass by reference
问题
以下是翻译好的代码部分:
是否有一种方法可以通过引用来查找数组的大小。 size函数在主函数内部运行良好。
#include <iostream>
#include <iterator>
using namespace std;
int hello(int arr[]){
cout<<arr<<endl;
//cout<<size(arr)<<endl;
}
int main(){
int arr[] = {1,2,3,4,6,7,2};
hello(arr);
cout<<arr<<endl;
cout<<size(arr)<<endl;
}
请注意,注释部分没有翻译。
英文:
Is there a way to find the size of an array, using pass by reference. The size function works well inside the main function.
#include <iostream>
#include <iterator>
using namespace std;
int hello(int arr[]){
cout<<arr<<endl;
//cout<<size(arr)<<endl;
}
int main(){
int arr[] = {1,2,3,4,6,7,2};
hello(arr);
cout<<arr<<endl;
cout<<size(arr)<<endl;
}
答案1
得分: 3
你正在通过值传递数组,所以 hello
函数以其参数的衰减类型 int*
作为参数。
当你通过引用传递数组时,需要传递一个额外的非类型模板参数,它是数组的长度,并且是推导的,因此在 hello
内部,你甚至不需要使用 std::size
来获取数组的长度,只需使用 N
:
template<class T, size_t N>
int hello(T (&arr)[N])
{
cout << N << endl; // 7
return 0;
}
英文:
You are passing array by value so hello
function takes as its parameter decayed type - int*
.
When you pass array by reference, you need to pass one additional non-type template parameter which is length of array and is deduced, therefore inside hello
you don't even use std::size
to get array's length, just use N
:
template<class T, size_t N>
int hello(T (&arr)[N])
{
cout<< size(arr) <<endl; // 7
cout << N << endl; // 7
return 0;
}
答案2
得分: 0
在将数组传递给函数时,它变成了一个指针,无法从中获取大小(它将始终报告4或8字节 - 指针值的大小!)。
如果您需要在函数中处理数组,您需要将大小作为参数传递给函数。
#include <iostream>
#include <iterator>
using namespace std;
int hello(int arr[], size_t count)
{
for(size_t i = 0; i < count; ++i)
{
std::cout << arr[i] << std::endl;
}
}
int main(){
int arr[] = {1,2,3,4,6,7,2};
hello(arr, sizeof(arr) / sizeof(int));
return 0;
}
在C++中更好的选择是使用std::vector代替(它将大小存储为成员变量)。
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
void hello(const std::vector<int>& arr)
{
for(size_t i = 0; i < arr.size(); ++i)
{
std::cout << arr[i] << std::endl;
}
}
int main(){
std::vector<int> arr = {1,2,3,4,6,7,2};
hello(arr);
return 0;
}
英文:
Once you pass an array to a function, it becomes a pointer, and there is no way to get the size from that (it will always report 4 or 8 bytes - the size of a pointer value!).
If you need to process an array in a func, you will need to pass in the size as an argument to the function.
#include <iostream>
#include <iterator>
using namespace std;
int hello(int arr[], size_t count)
{
for(size_t i = 0; i < count; ++i)
{
std::cout << arr[i] << std::endl;
}
}
int main(){
int arr[] = {1,2,3,4,6,7,2};
hello(arr, sizeof(arr) / sizeof(int));
return 0;
}
The better alternative in C++, is to use std::vector instead. (which will store the size as a member variable)
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
void hello(const std::vector<int>& arr)
{
for(size_t i = 0; i < arr.size(); ++i)
{
std::cout << arr[i] << std::endl;
}
}
int main(){
std::vector<int> arr = {1,2,3,4,6,7,2};
hello(arr);
return 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论