如何确定所传递的数组是1-D、2-D还是N-D

时间:2022-07-03 04:16:44

I want to write a function which will accept an array as a input argument. and the function should print all the elements of the array.

我想写一个函数,它接受数组作为输入参数。函数应该打印数组的所有元素。

print_array(arr)
{
  //print all the elemnts of arr.
}

I don't know how to do that.

我不知道怎么做。

I think first we need to findout whether the passed array is 1-D or 2-D or 3-D and so on...array

我认为首先我们需要知道所传递的数组是1-D还是2-D还是3-D等等

Because, to print the elemnts of:

因为,要打印的元素:

                            1-D array, you need only 1 for loop.
                            2-D array, you need only 2 for loop.
                            3-D array, you need only 3 for loop.

But, I dont know how you'll indetify whether its 1-D, 2-D or N-D array. Please Help.

但是,我不知道你如何确定它是1-D, 2-D还是N-D数组。请帮助。

5 个解决方案

#1


16  

You can actually find out the exact number of dimensions pretty easily, with a single overload using C++11's std::rank type trait:

你可以很容易地找到精确的维数,使用c++ 11的std::rank类型特征:

#include <type_traits>
#include <iostream>

template<class T, unsigned N>
void print_dimensions(T (&)[N]){
  static unsigned const dims = std::rank<T>::value + 1;
  std::cout << "It's a " << dims << "-D array, you need "
            << dims << " for-loops\n";
}

However, you don't actually need std::rank at all to print all the elements; this can easily be solved with a simple overload:

然而,您实际上并不需要std::rank,以打印所有元素;这很容易通过简单的重载来解决:

namespace print_array_detail{
template<class T>
void print(T const& v){ std::cout << v << " "; }
template<class T, unsigned N>
void print(T (&arr)[N]){
  for(unsigned i=0; i < N; ++i)
    print(arr[i]);
  std::cout << "\n";
}
}

template<class T, unsigned N>
void print_array(T (&arr)[N]){ print_array_detail::print(arr); }

Live example.

生活的例子。

#2


4  

You can achieve something to this end through templates and overloading in C++. Consider

您可以通过使用c++中的模板和重载来实现这一点。考虑

template<size_t X, size_t Y>
int sum_array_dimensions(int (&arr)[X][Y])
{
  // it's 2d
  return X + Y;
}

template<size_t X, size_t Y, size_t Z>
int sum_array_dimensions(int (&arr)[X][Y][Z])
{
  // it's 3d
  return X + Y + Z;
}

#3


4  

you could encode in a parameter the number of dimensions, and pass a unidimensional array

您可以在一个参数中编码维度的数量,并传递一个一维数组

#define N1 10
#define N2 100
void function(unsigned dimensions, int* array)
{ switch(dimension):
  { case 1:
      for (int i=0;i<N;i++)
      { ... array[i] ...
      }
      break;
    case 2:
      for (int i=0;i<N;i++)
      { for (int j=0;j<N;j++)
        { ... array[i*N+j] ...
        }
      }
      break;
    case 3:
      for (int i=0;i<N;i++)
      { for (int j=0;j<N;j++)
        { for (int k=0;k<N;k++)
          { ... array[i*N2+j*N+k] ...
          }
        }
      }
      break;
  }
}

if N is a power of 2 you can optimize the multiplication with << left shift (x*2^n == x<<n)
edit extended solution

如果N是2的乘方可以优化乘法< <左移(x * 2 ^ n="=" x < n)编辑扩展的解决方案< p>

// the array is 0-indexed
void function(unsigned* dimensions, int* array)
{ //dimensions[0] = number of dimensions
  //dimensions[1 ... dimensions[0] ] the dimensions themselves
  for(int i=1,n=1;i<=dimensions[0];i++)
  { n*=dimensions[i]; }
  /* if the order in the array happens to be the wanted one */
  for(int i=1;i<=n;i++)
  { print( array[i] );
  }
  /* otherwise the dimensions are specified in the dimension array */
  for(int i=1;i<=n;i++)
  { int k=0;
    int temp=i;
    int base=1;       
    for(int j=1;j<=dimensions[0];j++)
    { k+=(temp%dimension[j])*base;
      base*=dimension[j];
      temp/=dimension[j];
    }
    print(array[k]);
  }   
  */

#4


3  

As others have said, the size of array is lost when you pass it to a function (unless you pass by reference). So you can do something like this:

正如其他人所说,当您将数组传递给函数时(除非您通过引用传递),数组的大小就会丢失。你可以这样做:

/* this function does the work */
template <typename T>
void bar(T* arr, size_t n_dims, size_t* sizes)
{
    std::cout << n_dims << " dimension(s)\n";
    for (size_t i = 0; i < n_dims; ++i)   // for each dimension
        for (size_t j = 0; j < sizes[i]; ++j) ; // for each element
}

/* These are helper overloads to call bar with correct arguments. */
/* You'll need to provide one for each number of dimension you want to support */

template<typename T, size_t N>
void foo(T (&arr)[N])
{
    size_t sizes[] = {N};
    bar(arr, 1, sizes);
}

template<typename T, size_t N, size_t M>
void foo(T (&arr)[N][M])
{
    size_t sizes[] = {N, M};
    bar(arr, 2, sizes);
}

template<typename T, size_t N, size_t M, size_t O>
void foo(T (&arr)[N][M][O])
{
    size_t sizes[] = {N, M, O};
    bar(arr, 3, sizes);
}

int main()
{
    int arr1[42];
    int arr2[2][2];
    int arr3[2][3][4];
    foo(arr1);
    foo(arr2);
    foo(arr3);
}

Live example.

生活的例子。

#5


1  

You cannot do it. But you can write 3 different functions with same name and different arguments (diff arrays types), then each function will treat it's own array.

你不能这样做。但是,您可以使用相同的名称和不同的参数(diff数组类型)编写3个不同的函数,然后每个函数将处理它自己的数组。

#1


16  

You can actually find out the exact number of dimensions pretty easily, with a single overload using C++11's std::rank type trait:

你可以很容易地找到精确的维数,使用c++ 11的std::rank类型特征:

#include <type_traits>
#include <iostream>

template<class T, unsigned N>
void print_dimensions(T (&)[N]){
  static unsigned const dims = std::rank<T>::value + 1;
  std::cout << "It's a " << dims << "-D array, you need "
            << dims << " for-loops\n";
}

However, you don't actually need std::rank at all to print all the elements; this can easily be solved with a simple overload:

然而,您实际上并不需要std::rank,以打印所有元素;这很容易通过简单的重载来解决:

namespace print_array_detail{
template<class T>
void print(T const& v){ std::cout << v << " "; }
template<class T, unsigned N>
void print(T (&arr)[N]){
  for(unsigned i=0; i < N; ++i)
    print(arr[i]);
  std::cout << "\n";
}
}

template<class T, unsigned N>
void print_array(T (&arr)[N]){ print_array_detail::print(arr); }

Live example.

生活的例子。

#2


4  

You can achieve something to this end through templates and overloading in C++. Consider

您可以通过使用c++中的模板和重载来实现这一点。考虑

template<size_t X, size_t Y>
int sum_array_dimensions(int (&arr)[X][Y])
{
  // it's 2d
  return X + Y;
}

template<size_t X, size_t Y, size_t Z>
int sum_array_dimensions(int (&arr)[X][Y][Z])
{
  // it's 3d
  return X + Y + Z;
}

#3


4  

you could encode in a parameter the number of dimensions, and pass a unidimensional array

您可以在一个参数中编码维度的数量,并传递一个一维数组

#define N1 10
#define N2 100
void function(unsigned dimensions, int* array)
{ switch(dimension):
  { case 1:
      for (int i=0;i<N;i++)
      { ... array[i] ...
      }
      break;
    case 2:
      for (int i=0;i<N;i++)
      { for (int j=0;j<N;j++)
        { ... array[i*N+j] ...
        }
      }
      break;
    case 3:
      for (int i=0;i<N;i++)
      { for (int j=0;j<N;j++)
        { for (int k=0;k<N;k++)
          { ... array[i*N2+j*N+k] ...
          }
        }
      }
      break;
  }
}

if N is a power of 2 you can optimize the multiplication with << left shift (x*2^n == x<<n)
edit extended solution

如果N是2的乘方可以优化乘法< <左移(x * 2 ^ n="=" x < n)编辑扩展的解决方案< p>

// the array is 0-indexed
void function(unsigned* dimensions, int* array)
{ //dimensions[0] = number of dimensions
  //dimensions[1 ... dimensions[0] ] the dimensions themselves
  for(int i=1,n=1;i<=dimensions[0];i++)
  { n*=dimensions[i]; }
  /* if the order in the array happens to be the wanted one */
  for(int i=1;i<=n;i++)
  { print( array[i] );
  }
  /* otherwise the dimensions are specified in the dimension array */
  for(int i=1;i<=n;i++)
  { int k=0;
    int temp=i;
    int base=1;       
    for(int j=1;j<=dimensions[0];j++)
    { k+=(temp%dimension[j])*base;
      base*=dimension[j];
      temp/=dimension[j];
    }
    print(array[k]);
  }   
  */

#4


3  

As others have said, the size of array is lost when you pass it to a function (unless you pass by reference). So you can do something like this:

正如其他人所说,当您将数组传递给函数时(除非您通过引用传递),数组的大小就会丢失。你可以这样做:

/* this function does the work */
template <typename T>
void bar(T* arr, size_t n_dims, size_t* sizes)
{
    std::cout << n_dims << " dimension(s)\n";
    for (size_t i = 0; i < n_dims; ++i)   // for each dimension
        for (size_t j = 0; j < sizes[i]; ++j) ; // for each element
}

/* These are helper overloads to call bar with correct arguments. */
/* You'll need to provide one for each number of dimension you want to support */

template<typename T, size_t N>
void foo(T (&arr)[N])
{
    size_t sizes[] = {N};
    bar(arr, 1, sizes);
}

template<typename T, size_t N, size_t M>
void foo(T (&arr)[N][M])
{
    size_t sizes[] = {N, M};
    bar(arr, 2, sizes);
}

template<typename T, size_t N, size_t M, size_t O>
void foo(T (&arr)[N][M][O])
{
    size_t sizes[] = {N, M, O};
    bar(arr, 3, sizes);
}

int main()
{
    int arr1[42];
    int arr2[2][2];
    int arr3[2][3][4];
    foo(arr1);
    foo(arr2);
    foo(arr3);
}

Live example.

生活的例子。

#5


1  

You cannot do it. But you can write 3 different functions with same name and different arguments (diff arrays types), then each function will treat it's own array.

你不能这样做。但是,您可以使用相同的名称和不同的参数(diff数组类型)编写3个不同的函数,然后每个函数将处理它自己的数组。