【问题标题】:Passing a 2d dynamic array of strings by reference in C++在 C++ 中通过引用传递二维动态字符串数组
【发布时间】:2018-06-30 17:10:47
【问题描述】:

我必须为这个项目使用动态数组,但我不能通过引用传递它,我不知道该怎么做,我初始化了二维动态数组,但不知道如何通过引用传递它。

#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++ 新手,所以解决方案可能非常明显,但我就是看不到。任何帮助将不胜感激。

【问题讨论】:

  • 您需要使用一个&amp;,而不是两个。 void set_up_array(string (&amp;layout_array)[row][col]);
  • R Sahu 它给了我一个错误“错误:没有匹配函数调用'set_up_array'”

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


【解决方案1】:

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

(就像string * &amp;ref

【讨论】:

  • 没有通过引用传递 (&layout_array) 并简单地使用 (layout_array) 给我错误“错误:没有匹配的函数调用 'set_up_array'”引用主函数内部的调用
  • 查看您的代码,我认为您应该将void set_up_array(string (&amp;&amp;layout_array)[row][col]); 更改为void set_up_array(string * layout_array);,因为无法保证您的二维数组大小是多少,在这种情况下为 col * col。
  • 更正:string ** 而不是 string *
【解决方案2】:

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

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
}

【讨论】:

  • 必须是动态数组
  • 如果必须是动态数组,则应使用std::vector&lt;std::vector&lt;std::string&gt;&gt;,而不是尝试在代码中管理内存。
【解决方案3】:

数组总是通过引用传递..你的问题是如何传递一个二维字符串数组...

分析我是如何做到的:

#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


}

【讨论】:

    猜你喜欢
    • 2021-12-27
    • 2010-10-06
    • 1970-01-01
    • 2020-06-29
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    相关资源
    最近更新 更多