在 C++ 中使用循环创建顺序变量名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13520365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create sequential variable names using a loop in C++
提问by chuckieDub
I'm trying to create variable names using a loop.
我正在尝试使用循环创建变量名称。
Specifically, I am using this structure:
具体来说,我正在使用这种结构:
struct card{
string rank;
string suit;
};
This is the rest of my code as it stands, and where it says "card+i" is where I need it to say "card1", or "card2", etc.
这是我的代码的其余部分,它说“card+i”的地方是我需要它说“card1”或“card2”等的地方。
string aSuit[4] = {" Hearts"," Clubs"," Diamonds"," Spades"};
string aRank[13] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
string aDeck[52];
int main(int argc, char *argv[])
{
int i = 0;
for (int s=0; s<4; s++) {
for (int r=0; r<13; r++) {
card card+i;
card+i.rank = aRank[r];
card+i.suit = aSuit[s];
cout << card + i.rank << card + i.suit << endl;
i++;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
回答by Sidharth Mudgal
Use arrays instead:
改用数组:
card cards[52];
int main(int argc, char *argv[])
{
int i = 0;
for (int s = 0; s<4; s++) {
for (int r = 0; r<13; r++) {
cards[i].rank = aRank[r];
cards[i].suit = aSuit[s];
cout << cards[i].rank << cards[i].suit << endl;
i++;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
回答by Ed S.
You thinkthis is the best solution, but I can assure you, it is not. Tying your logic to the names of variables is a bad, bad idea, from a logical as well as maintenance standpoibnt. What you really want is a collection which can associate one piece of data (in this case, a string) with another.
您认为这是最好的解决方案,但我可以向您保证,事实并非如此。从逻辑和维护角度来看,将您的逻辑与变量名称联系起来是一个糟糕的主意。您真正想要的是一个可以将一个数据(在本例中为字符串)与另一个相关联的集合。
Look into a data structure like a map
查看像地图这样的数据结构
回答by paxdiablo
You need to use arrays for this, they allow a single variable to contain a group of items.
您需要为此使用数组,它们允许单个变量包含一组项目。
Then you simply use the variable name with an index, such as:
然后您只需使用带有索引的变量名称,例如:
int xyzzy[5];
for (int i = 0; i < 5; i++)
xyzzy[i] = i * 7;
// gives 0, 7, 14, 21, 28
In fact, your code already hasarrays representing the suits and card face values, not really that different from what you need here, so I'm not certain why you didn't make the logic-leap to using them for the cards as well. But that's basically how you'd do it.
事实上,你的代码已经有代表花色和卡片面值的数组,与你在这里需要的没有什么不同,所以我不确定你为什么不进行逻辑飞跃,将它们也用于卡片. 但这基本上就是你要做的。
回答by Joseph Mansfield
This isn't possible. When you compile a C++ program, any concept of variable names completely disappears. It can't generate variable names at run time because they just don't exist.
这是不可能的。当你编译一个 C++ 程序时,任何变量名的概念都完全消失了。它不能在运行时生成变量名,因为它们不存在。
However, there is a way to do this, of course. This is precisely what arrays are for. They give you a whole set of objects that you can index by number (i
, in your case). You already have a variable named aDeck
but I think you've defined it incorrectly. Did you perhaps want:
但是,当然有一种方法可以做到这一点。这正是数组的用途。它们为您提供了一整套可以按编号索引的对象(i
在您的情况下为 )。您已经有一个命名的变量,aDeck
但我认为您对它的定义不正确。你可能想要:
card aDeck[52];
Now you'll have a deck of 52 cards. Each card in this deck has suit
and rank
members. You can access each member of the array with aDeck[i]
. So change the inside of your loop to:
现在您将拥有一副 52 张牌。这套牌中的每张牌都有suit
和rank
成员。您可以使用 访问数组的每个成员aDeck[i]
。因此,将循环内部更改为:
aDeck[i].rank = aRank[r];
aDeck[i].suit = aSuit[s];