使用C++ 17通过引用获取数组的大小

huangapple go评论129阅读模式
英文:

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 &lt;iostream&gt;
#include &lt;iterator&gt;
using namespace std;
int hello(int arr[]){
  cout&lt;&lt;arr&lt;&lt;endl;
  //cout&lt;&lt;size(arr)&lt;&lt;endl;
}
int main(){
  int arr[] = {1,2,3,4,6,7,2};
  hello(arr);
  cout&lt;&lt;arr&lt;&lt;endl;
  cout&lt;&lt;size(arr)&lt;&lt;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&lt;class T, size_t N&gt;
int hello(T (&amp;arr)[N])
{
  cout&lt;&lt; size(arr) &lt;&lt;endl; // 7
  cout &lt;&lt; N &lt;&lt; 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 &lt;iostream&gt;
#include &lt;iterator&gt;
using namespace std;

int hello(int arr[], size_t count)
{
  for(size_t i = 0; i &lt; count; ++i)
  {
    std::cout &lt;&lt; arr[i] &lt;&lt; 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 &lt;iostream&gt;
#include &lt;iterator&gt;
#include &lt;vector&gt;
using namespace std;

void hello(const std::vector&lt;int&gt;&amp; arr)
{
  for(size_t i = 0; i &lt; arr.size(); ++i)
  {
    std::cout &lt;&lt; arr[i] &lt;&lt; std::endl;
  }
}
int main(){
  std::vector&lt;int&gt; arr = {1,2,3,4,6,7,2};

  hello(arr);

  return 0;
}

huangapple
  • 本文由 发表于 2020年1月3日 13:30:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/59573618.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定