繁体   English   中英

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

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

我必须为此项目使用动态数组,但无法通过引用传递它,我不确定该怎么做,我初始化了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

要通过引用传递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
}

数组总是按引用传递..您的问题是如何传递字符串的二维数组...

分析我是怎么做到的:

#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


}

暂无
暂无

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

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