多维字符串 c++
我正在编写一个具有二维字符串数组作为输入参数的函数。我初始化了字符串,将其传递给函数,但是当我尝试打印数组时没有任何反应。它说数组的长度是0。我所有的函数都存储在头文件中。这是我的代码:
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int c,i,j,fx,fy;
int color,fields,rows,anim,speed;
string opt[5][50];
string popt[5][50]={
{"caption","asdf","safd","asf"},
{"caption1","dsafa","asdf","asdf"},
{"caption2","asdf","asdf","asdfas"},
{"caption3","sadfa","asdfs","fasdfa"}};
void ini(int focus_text_color, int n_fields,int n_rows, string options[][50], bool animation=false, int animation_speed=10)
{
color=focus_text_color;
fields=n_fields;
for(i=1;i<fields+1;i++)
{
for(j=1;j<rows+1;j++)
{
opt[i][j]=options[i][j];
}
}
}
int drawh()
{
system("cls");
for(i=0;i<fields;i++)
{
for(j=0;j<rows;j++)
{
cout<<opt[i][j]<<setw(opt[i+1][j].length()+5);
}
}
return 0;
}
void main()
{
ini(LIGHTRED,4,4,popt);
drawh();
}
注意:这是代码的一部分,所以我还没有测试它,很抱歉我的英语不好:D
I'm writing a function that has a 2D array of strings as input parameter. I initialized the string, passed it to the function but when I tried to print the array nothing happened. It says that the length of the array is 0. All my functions are stored in a header file. Here's my code:
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int c,i,j,fx,fy;
int color,fields,rows,anim,speed;
string opt[5][50];
string popt[5][50]={
{"caption","asdf","safd","asf"},
{"caption1","dsafa","asdf","asdf"},
{"caption2","asdf","asdf","asdfas"},
{"caption3","sadfa","asdfs","fasdfa"}};
void ini(int focus_text_color, int n_fields,int n_rows, string options[][50], bool animation=false, int animation_speed=10)
{
color=focus_text_color;
fields=n_fields;
for(i=1;i<fields+1;i++)
{
for(j=1;j<rows+1;j++)
{
opt[i][j]=options[i][j];
}
}
}
int drawh()
{
system("cls");
for(i=0;i<fields;i++)
{
for(j=0;j<rows;j++)
{
cout<<opt[i][j]<<setw(opt[i+1][j].length()+5);
}
}
return 0;
}
void main()
{
ini(LIGHTRED,4,4,popt);
drawh();
}
NOTE: This is a part of the code so I haven't tested it, and sorry for my bad English :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了@Oli 的评论。为了更简单,您可以通过引用传递数组。请参阅下面的示例:
现在,
template ini()
为实际的ini()
提供了一个包装器,它在编译时计算数组的行/列。用法非常简单:循环应该从维度
0
开始,而不是从1
开始复制。检查我的方法并修改您的代码。此外,由于传递了错误的维度,您的代码中也存在许多问题(例如,在调用
ini()
时传递4
作为维度,而它应该是5)。Apart from @Oli's comments. To make it simpler, you can pass an array by reference. See below example:
Now,
template ini()
provides a wrapper to actualini()
which calculates the row/column of an array at compile time. Usage is very simple:Your loop should start from dimension
0
and not1
for copying. Check my approach and modify your code.Also there are many problems in your code due to passing wrong dimensions (e.g. passing
4
as dimension while callingini()
, when it should be 5).您没有得到任何输出的原因是您没有初始化全局变量
rows
,因此它保持为0。您的init
函数应该是:The reason why you don't get any output is that you don't initialize the global variable
rows
, so it stays at 0. Yourinit
function should be: