如何在字符串(名称)中使用循环变量

时间:2021-01-10 19:40:07

I am doing object oriented programming and I have to give different names (physBox1, physBox2..... physBox10) to 10 different boxes (represented by 10 element array Box[10]) in C++ language.

我正在进行面向对象的编程,我必须在C ++语言中给10个不同的框(由10个元素数组Box [10]表示)给出不同的名称(physBox1,physBox2 ..... physBox10)。

for (G4int i=0; i<10; i++) 
{
    new G4PVPlacement(0, Box[i],"phyBox[i]");
}

Here G4PVPlacement is some class which takes three values - second value is Box[i] indicating the 10 boxes and physBox[i] are names of that boxes. Here I am confused whether physBox[i] will be treated as a single string or here [i] can run from 0 to 9 according to "for loop".

这里G4PVPlacement是一个带三个值的类 - 第二个值是Box [i],表示10个盒子,physBox [i]是这些盒子的名称。在这里我很困惑physBox [i]是否会被视为单个字符串,或者[i]可以根据“for循环”从0到9运行。

1 个解决方案

#1


"phyBox[i]" is a string literal, within which i is simply the letter i. If you want to make a string containing a run-time value, you'll have to do it yourself:

“phyBox [i]”是一个字符串文字,其中我只是字母i。如果你想创建一个包含运行时值的字符串,你必须自己做:

"phyBox[" + std::to_string(i) + "]"

#1


"phyBox[i]" is a string literal, within which i is simply the letter i. If you want to make a string containing a run-time value, you'll have to do it yourself:

“phyBox [i]”是一个字符串文字,其中我只是字母i。如果你想创建一个包含运行时值的字符串,你必须自己做:

"phyBox[" + std::to_string(i) + "]"