多维字符串 c++

发布于 2024-11-09 06:54:13 字数 1098 浏览 0 评论 0原文

我正在编写一个具有二维字符串数组作为输入参数的函数。我初始化了字符串,将其传递给函数,但是当我尝试打印数组时没有任何反应。它说数组的长度是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只有影子陪我不离不弃 2024-11-16 06:54:13

除了@Oli 的评论。为了更简单,您可以通过引用传递数组。请参阅下面的示例:

template<unsigned int ROW, unsigned int COL>
void ini (string (&s)[ROW][COL])  // psuedo code for 'ini'; put extra params to enhance
{
  ini(s, ROW, COL);
}

现在,template ini() 为实际的 ini() 提供了一个包装器,它在编译时计算数组的行/列。用法非常简单:

string s[10][5];
ini(s); // calls ini(s,10,5);

循环应该从维度 0 开始,而不是从 1 开始复制。检查我的方法并修改您的代码。

for(int i = 0; i < ROW; i++)
  for(int j = 0; j < COL; j++)
    s1[i][j] = s2[i][j];

此外,由于传递了错误的维度,您的代码中也存在许多问题(例如,在调用 ini() 时传递 4 作为维度,而它应该是5)。

Apart from @Oli's comments. To make it simpler, you can pass an array by reference. See below example:

template<unsigned int ROW, unsigned int COL>
void ini (string (&s)[ROW][COL])  // psuedo code for 'ini'; put extra params to enhance
{
  ini(s, ROW, COL);
}

Now, template ini() provides a wrapper to actual ini() which calculates the row/column of an array at compile time. Usage is very simple:

string s[10][5];
ini(s); // calls ini(s,10,5);

Your loop should start from dimension 0 and not 1 for copying. Check my approach and modify your code.

for(int i = 0; i < ROW; i++)
  for(int j = 0; j < COL; j++)
    s1[i][j] = s2[i][j];

Also there are many problems in your code due to passing wrong dimensions (e.g. passing 4 as dimension while calling ini(), when it should be 5).

把梦留给海 2024-11-16 06:54:13

您没有得到任何输出的原因是您没有初始化全局变量rows,因此它保持为0。您的init函数应该是:

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;
    rows = n_rows;   //-- ADDED LINE
 ....

The reason why you don't get any output is that you don't initialize the global variable rows, so it stays at 0. Your init function should be:

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;
    rows = n_rows;   //-- ADDED LINE
 ....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文