Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 向变量名添加字符_C++_Variables_Char - Fatal编程技术网

C++ 向变量名添加字符

C++ 向变量名添加字符,c++,variables,char,C++,Variables,Char,在我的例子中,我想要的代码如下所示: anRow0[] = {0,0,0,0,0,0,0} anRow1[] = {0,0,0,0,0,0,0} anRow2[] = {0,0,0,0,0,0,0} int place(int nVar1, int nVar2) //Where nVar1 is the row and nVar2 = current turn { int nVert = 0; while (anRow\i want to add nVar1 here\[nVer

在我的例子中,我想要的代码如下所示:

anRow0[] = {0,0,0,0,0,0,0}
anRow1[] = {0,0,0,0,0,0,0}
anRow2[] = {0,0,0,0,0,0,0}
int place(int nVar1, int nVar2) //Where nVar1 is the row and nVar2 = current turn
{
    int nVert = 0;
    while (anRow\i want to add nVar1 here\[nVert+1] && nVert < 6)
    {
        nVert += 1;
    }
    anRow[nVert] = nVar2;
    return true;
}
std::vector<int*> allRows = {
    anRow0,
    anRow1,
    anRow2
};
anRow0[]={0,0,0,0,0,0}
anRow1[]={0,0,0,0,0,0}
anRow2[]={0,0,0,0,0,0}
int-place(int-nVar1,int-nVar2)//其中nVar1是行,nVar2=当前回合
{
int nVert=0;
while(anRow\n我想在这里添加nVar1\[nVert+1]&&nVert<6)
{
nVert+=1;
}
anRow[nVert]=nVar2;
返回true;
}
如果(nVar1==0),我可以做几个“
”//检查是否有行1[]等,但这似乎效率低下。
有没有办法添加这样的数字?另外,请忽略代码的其余部分,我知道你可以用for替换while,但这不是重点


感谢您的帮助

您可以将阵列放入另一个阵列中,如下所示:

anRow0[] = {0,0,0,0,0,0,0}
anRow1[] = {0,0,0,0,0,0,0}
anRow2[] = {0,0,0,0,0,0,0}
int place(int nVar1, int nVar2) //Where nVar1 is the row and nVar2 = current turn
{
    int nVert = 0;
    while (anRow\i want to add nVar1 here\[nVert+1] && nVert < 6)
    {
        nVert += 1;
    }
    anRow[nVert] = nVar2;
    return true;
}
std::vector<int*> allRows = {
    anRow0,
    anRow1,
    anRow2
};

或者更好地使用
std::array
std::array

std::array<std::array<int, 7>, 3> allRows = {{
    {{ 0, 0, 0, 0, 0, 0, 0 }},
    {{ 0, 0, 0, 0, 0, 0, 0 }},
    {{ 0, 0, 0, 0, 0, 0, 0 }}
}};
std::数组所有行={{
{{ 0, 0, 0, 0, 0, 0, 0 }},
{{ 0, 0, 0, 0, 0, 0, 0 }},
{{ 0, 0, 0, 0, 0, 0, 0 }}
}};

您可以将阵列放在另一个阵列中,如下所示:

anRow0[] = {0,0,0,0,0,0,0}
anRow1[] = {0,0,0,0,0,0,0}
anRow2[] = {0,0,0,0,0,0,0}
int place(int nVar1, int nVar2) //Where nVar1 is the row and nVar2 = current turn
{
    int nVert = 0;
    while (anRow\i want to add nVar1 here\[nVert+1] && nVert < 6)
    {
        nVert += 1;
    }
    anRow[nVert] = nVar2;
    return true;
}
std::vector<int*> allRows = {
    anRow0,
    anRow1,
    anRow2
};

或者更好地使用
std::array
std::array

std::array<std::array<int, 7>, 3> allRows = {{
    {{ 0, 0, 0, 0, 0, 0, 0 }},
    {{ 0, 0, 0, 0, 0, 0, 0 }},
    {{ 0, 0, 0, 0, 0, 0, 0 }}
}};
std::数组所有行={{
{{ 0, 0, 0, 0, 0, 0, 0 }},
{{ 0, 0, 0, 0, 0, 0, 0 }},
{{ 0, 0, 0, 0, 0, 0, 0 }}
}};

您可以使用二维数组而不是3个一维数组。 您可以在此处阅读有关它们的内容:

这里有一个可能的修改:

int an[3][7] = {0};
int place(int nVar1, int nVar2) //Where nVar1 is the row and nVar2 = current turn
{
    int nVert = 0;
    while (anRow[nVar2][nVert+1] && nVert < 6)
    {
        nVert += 1;
    }
    anRow[nVert] = nVar2;
    return true;
}
intan[3][7]={0};
int-place(int-nVar1,int-nVar2)//其中nVar1是行,nVar2=当前回合
{
int nVert=0;
而(anRow[nVar2][nVert+1]&&nVert<6)
{
nVert+=1;
}
anRow[nVert]=nVar2;
返回true;
}

顺便问一下,尽管返回值是int,但为什么返回“true”。这是允许的,但我建议不要这样做。请改为返回1,或者将返回值更改为布尔值。

您可以使用二维数组而不是三维数组。 您可以在此处阅读有关它们的内容:

这里有一个可能的修改:

int an[3][7] = {0};
int place(int nVar1, int nVar2) //Where nVar1 is the row and nVar2 = current turn
{
    int nVert = 0;
    while (anRow[nVar2][nVert+1] && nVert < 6)
    {
        nVert += 1;
    }
    anRow[nVert] = nVar2;
    return true;
}
intan[3][7]={0};
int-place(int-nVar1,int-nVar2)//其中nVar1是行,nVar2=当前回合
{
int nVert=0;
而(anRow[nVar2][nVert+1]&&nVert<6)
{
nVert+=1;
}
anRow[nVert]=nVar2;
返回true;
}
顺便问一下,虽然返回值是int,但为什么要返回“true”。这是允许的,但我建议不要这样做。请改为返回1,或者将返回值更改为布尔值。

看起来您需要一个二维数组,类似这样的数组

int an[3][6] = {{0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}};

int place(int nVar1, int nVar2)
{
    int nVert = 0;
    while (nVert < 6 && an[nVar1][nVert+1])
    {
        nVert += 1;
    }
    an[nVar1][nVert] = nVar2;
    return true;
}
intan[3][6]={0,0,0,0,0,0,0},{0,0,0,0,0,0,0},{0,0,0,0,0};
内部位置(内部nVar1、内部nVar2)
{
int nVert=0;
而(nVert<6&&an[nVar1][nVert+1])
{
nVert+=1;
}
[nVar1][nVert]=nVar2;
返回true;
}
尽管该代码无疑存在缺陷(最好使用
nVert<5
),但修复缺陷仍然是另一个问题。

似乎您需要一个二维数组,类似这样的东西

int an[3][6] = {{0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}};

int place(int nVar1, int nVar2)
{
    int nVert = 0;
    while (nVert < 6 && an[nVar1][nVert+1])
    {
        nVert += 1;
    }
    an[nVar1][nVert] = nVar2;
    return true;
}
intan[3][6]={0,0,0,0,0,0,0},{0,0,0,0,0,0,0},{0,0,0,0,0};
内部位置(内部nVar1、内部nVar2)
{
int nVert=0;
而(nVert<6&&an[nVar1][nVert+1])
{
nVert+=1;
}
[nVar1][nVert]=nVar2;
返回true;
}

尽管该代码无疑存在bug(最好使用
nVert<5
)仍然修复错误是另一个问题。

< P>不,你不能操纵变量、函数或实际上是语法上的名字——如果你真的需要这个C++是错误的语言。使用预处理器宏可能有一些非常有限的功能集,但这不是你想要的。 而是使用容器来存储变量:

std::vector<std::array<int, 7>> rows;
std::向量行;

>P>不,你不能操纵变量、函数或实际上是语法上的名字——如果你真的需要这个C++是错误的语言。使用预处理器宏可能会有一些非常有限的功能集,但这不是你想要的。 而是使用容器来存储变量:

std::vector<std::array<int, 7>> rows;
std::向量行;

这是不可能的,因为C是一种编译语言,变量名被转换为内存地址。在运行时,代码甚至不知道源代码中变量的名称


如果你想用运行时数据来选择一个值,你就必须使用数组。这里有多种选项,C++最好的是使用一些STL容器(STD::St:::vector,STD::List.,…),但您也可以使用C样式的数组。

这是不可能的,因为C是一种编译语言,变量名被转换为内存地址。在运行时,代码甚至不知道源代码中变量的名称


如果你想用运行时数据来选择一个值,你就必须使用数组。这里有多种选项,C++最好的是使用一些STL容器(STD::St:::vector,STD::List.,…)为什么你不使用C数组样式?为什么不使用2D数组?P.S.C++不支持很多元编程<代码> ANR\\我想在这里添加NVAR1 \[NVRT+1 ] < /C>?你如何在C++中格式化一个2D数组?我见过数组[x,y],但是这似乎不起作用。“将nVar的字符转换为anRow的名称。但是,2D数组更好,只是我不知道如何写一个。@ USER 27 98044,当你看到<代码>数组[x,y] < /Cord>你在看一个不同的语言,在C++中,它是代码>数组[x] [y] < /Cord> @ USER 998044。我想知道为什么你知道在一个2D数组更好的时候,把字符添加到一个变量名。为什么不问问2D阵列呢?这是XY问题的经典案例,为什么不使用2d阵列呢?P.S.C++不支持很多元编程<代码> ANRO。我想在这里添加NVAR1 \[NVRT+1 ] < /Cord>?如何格式化2?