简体   繁体   English

如何在C ++中将字符串数据存储在字符串2D数组中

[英]How to store the string data in string 2D array in C++

I want to store data in the first element at the first index of 2D array t[0][0]! 我想将数据存储在2D数组t [0] [0]的第一个索引的第一个元素中! I write this code: 我写这段代码:

int main()
{
    string input;
    cout << "Enter string of code:\n";
    cin >> input;
    queue < string > m;
    string t[100][3];
    while (input != "^")
    {
        int i = 0;

        t[i][0] = input;
        m.push(t[i][0]);
        if (input == " ")
        {
            t[i + 1][0];
            break;
        }

        cin >> input;
        i++;

    }
    int c = 1;
    while (!m.empty())
    {
        int i = 0;
        t[i][0] = m.front();
        string temp;
        temp = t[i][0];
        t[i][1] = check(temp);
        //cout <<c<<" "<<t[i][0]<<" Is: " << t[i][1] << endl;
        c++;
        m.pop();
        i++;
    }
    cout << endl;

    cout << c << " " << t[0][0] << " Is: " << t[0][1] << endl;

    system("Pause");
    return 0;
}

I have problem with this statement 我对这个陈述有疑问

cout << c << " " << t[0][0] << " Is: " << t[0][1] << endl; cout << c <<“”<< t [0] [0] <<“是:”<< t [0] [1] << endl;

this doesn't print the value in the array! 这不会打印数组中的值!

Declare and initialize i above the while loop, like this: 在while循环上声明并初始化 i ,如下所示:

int i = 0;
while (!m.empty())
{
    t[i][0] = m.front();
    ...

Let me explain what happens (in your code) here: 让我解释一下(在你的代码中)会发生什么:

while (!m.empty())
{
    int i = 0;
    t[i][0] = m.front();
    string temp;
    temp = t[i][0];
    t[i][1] = check(temp);
    //cout <<c<<" "<<t[i][0]<<" Is: " << t[i][1] << endl;
    c++;
    m.pop();
    i++;
}

Every time you enter the body of the loop, a variable named i is declared and initialized to 0. The scope of i is the body of the loop. 每次进入循环体时,都会声明一个名为i的变量并将其初始化为0. i的范围是循环体。

You increment i at the end of the loop, but as soon as the loop runs again, i will be again be initialized to 0 (you also re-declare it). 你在循环结束时递增i ,但是一旦循环再次运行, i将再次被初始化为0(你也重新声明它)。 So if we were in the second loop, you would want to i to have a value of 1, but because you initialize to 0 every time you enter the loop, it will always have a value of 0 as you are executing the body of the loop. 因此,如果我们处于第二个循环中,您希望i的值为1,但是因为每次进入循环时初始化为0,所以当您执行循环体时,它将始终具有值0环。


Moreover, this line: 而且,这一行:

t[i + 1][0];

should issue a warning of this kind: 应发出此类警告:

main.cpp:22:23: warning: expression result unused [-Wunused-value]
            t[i + 1][0];
            ~~~~~~~~ ~^

I got by compiling your code like this: 我通过编译你的代码得到了这样的:

g++ -Wall main.cpp g ++ -Wall main.cpp

where the flag Wall enables all warnings, pretty cool, make this your friend. 国旗墙上的所有警告,非常酷,让这个你的朋友。

It is helpful, as it happens now, since that line does nothing that has impact on your code. 现在发生这种情况很有帮助,因为该行不会对您的代码产生任何影响。


PS: Of course, I don't know what is the check() you have in your code, so this is all I can say . PS:当然,我不知道你的代码中的check()是什么,所以这就是我能说的。

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

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