[英]Passing a 2d dynamic array of strings by reference in C++
I have to use a dynamic array for this project but I can't pass it by reference, I am not sure what to do, I initialized the 2d dynamic array, but not sure how to pass it by reference. 我必须为此项目使用动态数组,但无法通过引用传递它,我不确定该怎么做,我初始化了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
}
I am new to c++ so the solution might be very obvious but I just can't see it. 我是c ++的新手,所以解决方案可能非常明显,但是我看不到它。 Any help would be appreciated. 任何帮助,将不胜感激。
Given what you are doing with the array in set_up_array
, you don't need to pass the actual string*
by reference, because you're simply dereferencing it (telling your computer where to look for a string
) and altering the value of string located at that index. 鉴于您正在使用set_up_array
的数组,您不需要通过引用传递实际的string*
,因为您只需取消引用(告诉计算机在哪里寻找string
)并更改所位于的字符串的值即可。在那个索引。 When you pass an array in C/C++, you're just passing in an int, so don't worry about references unless you're modifying what the pointer is pointing to. 当您使用C / C ++传递数组时,您只是传递一个int值,因此除非您正在修改指针所指向的内容,否则不必担心引用。
(It would be like string * &ref
) (这就像string * &ref
)
To pass a 2D array by reference, use: 要通过引用传递2D数组,请使用:
void set_up_array(string (&layout_array)[row][col]);
In order to be able call that function, the variable needs to be of type string [row][col]
. 为了能够调用该函数,变量必须为string [row][col]
。
Change main
to: 将main
更改为:
int main()
{
std::string layout_array[row][col];
set_up_array(layout_array);
...
}
Since you know the sizes of the arrays at compile time, it will be better to use std::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
}
Arrays are always pass by reference.. your problem is how to pass a 2d array of string... 数组总是按引用传递..您的问题是如何传递字符串的二维数组...
analyze how i did it: 分析我是怎么做到的:
#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.