在C ++中通过引用传递2d动态字符串数组

时间:2018-01-22 05:47:18

标签: c++ multidimensional-array pass-by-reference dynamic-arrays

我必须为这个项目使用动态数组,但是我不能通过引用传递它,我不知道该怎么做,我初始化了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 ++的新手,所以解决方案可能非常明显,但我无法看到它。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

鉴于你在set_up_array中使用数组做了什么,你不需要通过引用传递实际的string*,因为你只是取消引用它(告诉你的计算机在哪里寻找) a string)并改变位于该索引处的字符串的值。当您在C / C ++中传递数组时,您只是传入一个int,所以除非您正在修改指针指向的内容,否则不要担心引用。

(就像string * &ref

答案 1 :(得分:0)

要通过引用传递2D数组,请使用:

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
}

答案 2 :(得分:0)

数组总是通过引用传递..你的问题是如何传递一个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


}