有没有办法复制和粘贴数据,同时每次都增加该数据中的数字?

Is there a way to copy and paste data while simultaneously increasing a number in that data each time?

基本上我的程序里有-

    team[0].game[2] = loadvar[1];
    team[0].game[3] = loadvar[2];
    team[0].game[4] = loadvar[3];
    team[0].game[5] = loadvar[4];
    team[0].game[6] = loadvar[5];
    team[0].game[7] = loadvar[6];
    team[0].game[8] = loadvar[7];
    team[0].game[9] = loadvar[8];
    team[0].game[10] = loadvar[9];
    team[0].game[11] = loadvar[10];
    team[0].game[12] = loadvar[11];
    
    team[1].game[1] = loadvar[13];
    team[1].game[2] = loadvar[14];
    team[1].game[3] = loadvar[15];
    team[1].game[4] = loadvar[16];
    team[1].game[5] = loadvar[17];
    team[1].game[6] = loadvar[18];
    team[1].game[7] = loadvar[19];
    team[1].game[8] = loadvar[20];
    team[1].game[9] = loadvar[21];
    team[1].game[10] = loadvar[22];
    team[1].game[11] = loadvar[23];
    team[1].game[12] = loadvar[24];

    team[2].game[1] = loadvar[26];
    team[2].game[2] = loadvar[27];
    team[2].game[3] = loadvar[28];
    team[2].game[4] = loadvar[29];
    team[2].game[5] = loadvar[30];
    team[2].game[6] = loadvar[31];
    team[2].game[7] = loadvar[32];
    team[2].game[8] = loadvar[33];
    team[2].game[9] = loadvar[34];
    team[2].game[10] = loadvar[35];
    team[2].game[11] = loadvar[36];
    team[2].game[12] = loadvar[37];
    
    team[3].game[1] = loadvar[39];
    team[3].game[2] = loadvar[40];
    team[3].game[3] = loadvar[41];
    team[3].game[4] = loadvar[42];
    team[3].game[5] = loadvar[43];
    team[3].game[6] = loadvar[44];
    team[3].game[7] = loadvar[45];
    team[3].game[8] = loadvar[46];
    team[3].game[9] = loadvar[47];
    team[3].game[10] = loadvar[48];
    team[3].game[11] = loadvar[49];
    team[3].game[12] = loadvar[50];
    
    team[4].game[1] = loadvar[52];
    team[4].game[2] = loadvar[53];
    team[4].game[3] = loadvar[54];
    team[4].game[4] = loadvar[55];
    team[4].game[5] = loadvar[56];
    team[4].game[6] = loadvar[57];
    team[4].game[7] = loadvar[58];
    team[4].game[8] = loadvar[59];
    team[4].game[9] = loadvar[60];
    team[4].game[10] = loadvar[61];
    team[4].game[11] = loadvar[62];
    team[4].game[12] = loadvar[63];

我正在从文本文件中检索数据。 基本上,在程序中,我不得不一遍又一遍地复制和粘贴相同的内容,并增加团队和 loadvar 的数组编号。有没有办法复制粘贴,能帮我增加数量?

如果真的要复制粘贴,可以这样写

int i = 0;
int j = 2;
int k = 1;
team[i].game[j++] = loadvar[k++];
team[i].game[j++] = loadvar[k++];

等等。然而,更短且更不容易出错的方法是两个嵌套的 for 循环。

简单地不要复制+粘贴,而是使用循环来完成递增的数字,例如for

int v = 1;

for (int t = 0; t < 5; ++t)
{
    for (int g = 1; g <= 12; ++g)
    {
        team[t].game[g] = loadvar[v++];
    }
}

我已经按照你的问题将 game 索引从 1-12,但我怀疑你可能是想从 0-11 索引。对于初学者编程,请注意这个常见的错误来源。