在C++中通过引用传递字符串的二维动态数组

Passing a 2d dynamic array of strings by reference in C++

本文关键字:二维 数组 动态 字符串 C++ 引用      更新时间:2023-10-16

我必须为这个项目使用动态数组,但我无法通过引用传递它,我不确定该怎么做,我初始化了 2d 动态数组,但不确定如何通过引用传递它。

#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
 using namespace std;
#define row 5
#define col 14
 typedef string* StrArrPtr;
 void set_up_array(string (&&layout_array)[row][col]);
 int main()
{
    StrArrPtr *layout_array = new StrArrPtr[col];
    for(int i = 0; i < row; i++)
    {
        layout_array[i] = new string[col];
    }
    //string layout_array[row][col];
    set_up_array(layout_array);
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++)
        {
            if(j == 1 && i > 0)
            {
                cout << right << setw(11);
            }
            cout << "| " << layout_array[i][j] << " "; 
        }
        cout << "|" << endl;
    }
    return 0;
}
void set_up_array(string (&&layout_array)[row][col])
{
    layout_array[0][0] = "Lab Number"; //First Column / first row
    layout_array[1][0] = "1"; // second row
    layout_array[2][0] = "2"; // third row
    layout_array[3][0] = "3"; // fourth row
    layout_array[4][0] = "4"; // fifth row

}

我是 c++ 的新手,所以解决方案可能非常明显,但我只是看不到它。任何帮助将不胜感激。

给定你在set_up_array数组中对数组所做的工作,你不需要通过引用传递实际的string*,因为你只是取消引用它(告诉您的计算机在哪里查找string(并更改位于该索引的字符串的值。当你在 C/C++ 中传递数组时,你只是传入一个 int,所以不要担心引用,除非你修改了指针指向的内容。

(就像string * &ref(

要通过引用传递二维数组,请使用:

void set_up_array(string (&layout_array)[row][col]);

为了能够调用该函数,变量的类型需要为 string [row][col]

main更改为:

int main()
{
    std::string layout_array[row][col];
    set_up_array(layout_array);
    ...
}

由于您在编译时知道数组的大小,因此最好使用 std::array .

#include <iostream>
#include <vector>
#include <array>
#include <string>
#include <iomanip>
using namespace std;
const int row = 5;
const int col = 14;
void set_up_array(std::array<std::array<std::string, col>, row>& layout_array);
int main()
{
   std::array<std::array<std::string, col>, row> layout_array;
   set_up_array(layout_array);
   for(int i = 0; i < row; i++)
   {
      for(int j = 0; j < col; j++)
      {
         if(j == 1 && i > 0)
         {
            cout << right << setw(11);
         }
         cout << "| " << layout_array[i][j] << " "; 
      }
      cout << "|" << endl;
   }
   return 0;
}
void set_up_array(std::array<std::array<std::string, col>, row>& layout_array)
{
   layout_array[0][0] = "Lab Number"; //First Column / first row
   layout_array[1][0] = "1"; // second row
   layout_array[2][0] = "2"; // third row
   layout_array[3][0] = "3"; // fourth row
   layout_array[4][0] = "4"; // fifth row
}
数组

总是通过引用传递的.. 你的问题是如何传递字符串的 2d 数组...

分析我是如何做到的:

#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
 using namespace std;
#define row 5
#define col 14
 typedef string* StrArrPtr;
 void set_up_array(string* layout_array[]);
 int main()
{
    StrArrPtr *layout_array = new StrArrPtr[row];
    for(int i = 0; i < row; i++)
    {
        layout_array[i] = new string[col];
    }
    //string layout_array[row][col];
    set_up_array(layout_array);
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++)
        {
            if(j == 1 && i > 0)
            {
                cout << right << setw(11);
            }
            cout << "| " << layout_array[i][j] << " ";
        }
        cout << "|" << endl;
    }
    return 0;
}
void set_up_array(string* layout_array[])
{
    layout_array[0][0] = "Lab Number"; //First Column / first row
    layout_array[1][0] = "1"; // second row
    layout_array[2][0] = "2"; // third row
    layout_array[3][0] = "3"; // fourth row
    layout_array[4][0] = "4"; // fifth row

}