简体   繁体   English

c++ 中的索引输入 function 的二维数组

[英]2d array with index input function in c++

i have a problem with input array in function;我在 function 中的输入数组有问题; in this code i take an array parameter with the indexes from user and the function will print the table of 2D array with parameter reserve it;在这段代码中,我从用户那里获取一个带有索引的数组参数,function 将打印带有参数保留的二维数组表; here is the code:这是代码:

   #include <iostream>
   #include <windows.h>
   using namespace std ; 


  const char * table[3][2]={{"m1","m2"},{"n1","n2"},{"h1","h2"}} ; 

   void setTable(char *array,int n,int m) {
      for(int i=0 ; i<n;i++){
        for(int j=0 ; j<m;j++) {
          cout<<array[i][j]<<"---" ;       //print array 
           } 
        }
     }
      
   int main(){
    setTable((char * )table,4,2) ; // send array with indexes to function
    return 0;
     }

but i have a error when i run it:但是当我运行它时出现错误

    In function 'void setTable(char*, int, int)':
    [Error] invalid types 'char[int]' for array subscript

An array of arrays cant simply be converted to a single pointer. arrays 数组不能简单地转换为单个指针。

Generally, when you need to do C-style casting (like in (char * )table ) you should take that as a sign that you do something wrong.通常,当您需要进行 C 风格的转换时(例如在(char * )table ),您应该将其视为您做错了什么的标志。

Now to solve your problem... You have to remember that arrays naturally decays to pointers to their first element.现在要解决您的问题...您必须记住 arrays 自然衰减为指向其第一个元素的指针。 That is, table decay to &table[0] .也就是说, table衰减到&table[0] This will have the type "pointer to array of two pointers to char ".这将具有类型“指向两个指向char的指针的数组的指针”。 Or char* (*) [2] .char* (*) [2]

So the argument needs to be declared as所以参数需要声明为

char* (*array)[2]

Then you simply pass table when calling the function:然后在调用 function 时只需传递table

setTable(table, 3, 2);

If you used standard C++ classes and type-aliases then it would be even simpler:如果您使用标准 C++ 类和类型别名,那么它会更简单:

using table_type = std::array<std::array<std::string, 2>, 3>;

table_type table = { ... };

void setTable(table_type& table) { ... }

And of course I don't recommend using global variables, but if you do use them you don't even need to pass them as arguments to your functions at all.当然我不推荐使用全局变量,但如果你使用它们,你甚至不需要将它们作为 arguments 传递给你的函数。

When passing a C-style 2D array to a function, the function need to know the array dimension in order to do the indexing correctly.将 C 样式的二维数组传递给 function 时,function 需要知道数组维度才能正确进行索引。

Like:喜欢:

const char * table[3][2]={{"m1","m2"},{"n1","n2"},{"h1","h2"}} ; 

void setTable(const char * array[][2],int n,int m) {
  for(int i=0 ; i<n;i++){
    for(int j=0 ; j<m;j++) {
      cout<<array[i][j]<<"---" ;
    } 
  }
}
  
int main(){
  setTable(table,4,2) ;
  return 0;
}

BTW: Consider using the C++ container std::vector instead of a C-style array顺便说一句:考虑使用 C++ 容器std::vector而不是 C 样式数组

#include <iostream> using namespace std; const char* table[3][2] = {{"m1","m2"},{"n1","n2"},{"h1","h2"}}; template <typename T, int n, int m> void setTable(T (&arr)[n][m]) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cout << arr[i][j] << "---"; //print array } cout << '\n'; } } int main() { setTable(table); // send array with indexes to function return 0; }

Try renaming "array" to some other word, eg "table".尝试将“array”重命名为其他单词,例如“table”。 I believe visual studio reserves the word "array".我相信视觉工作室保留了“数组”这个词。

You need to convert your function argument from char* to char** one of您需要将 function 参数从char*转换为char**之一

  1. const char * arr[][2] : Passing the array by value which would lead to decay to the pointer type for the first dimension(ie of dimension 3). const char * arr[][2] :按值传递数组,这将导致衰减到第一个维度(即维度 3)的指针类型。 ( reference ) 参考
  2. const char * (&arr)[3][2] : Passing the array by reference will ensure the first dimension does not decay. const char * (&arr)[3][2] :通过引用传递数组将确保第一个维度不会衰减。 ( reference ) 参考
  3. const char* (*arr)[3][2] : Passing a pointer to the array itself which preserves the dimensions as the first dimension is already of pointer type. const char* (*arr)[3][2] :传递一个指向数组本身的指针,它保留维度,因为第一个维度已经是指针类型。 This also requires passing setTable(&arr...) and dereferencing the pointer by (*arr)[i][j] while printing.这还需要在打印时传递setTable(&arr...)并通过(*arr)[i][j]取消引用指针。
#include <iostream>

// void setTable(const char * arr[][2],int n,int m) {
// void setTable(const char* (*arr)[3][2],int n,int m) { // need to use setTable(&table, 4, 2) and (*arr)[i][j]
void setTable(const char * (&arr)[3][2],int n,int m) {
  for(int i=0 ; i<n;i++){
    for(int j=0 ; j<m;j++) {
        std::cout<<arr[i][j]<<"---" ;       //print array 
    } 
    std::cout << "\n";
    }
 }
  
int main(){
    const char * table[3][2]={{"m1","m2"},{"n1","n2"},{"h1","h2"}} ; // 2d array(3x2) of const char*
    setTable(table,3,2) ; // send array with indexes to function
    return 0;
}

Code Link Related Reading代码链接相关阅读

Finally, as mentioned in the other answers using a standard C++ container such as std::array would be a good idea to avoid the array-to-pointer decay.最后,正如其他答案中提到的那样,使用标准 C++ 容器(例如std::array )是避免数组到指针衰减的好主意。 Additionally, you should avoid using namespace std .此外,您应该避免using namespace std

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM