【发布时间】:2017-05-11 03:29:28
【问题描述】:
问题:我想知道是否可以使用循环从数组中的实例读取值并将它们存储或添加到另一个变量。
例如有一个类叫item
class item{
public:
string name;
int value;
};
如果有一个名为inventory 的数组包含item 的实例。 (假装他们有价值观)
item inventory[20] = {item.name, item.value};
是否可以滚动该数组以获取实例的特定值类型?如果是这样,是否可以将 for 循环专门搜索的实例值添加到另一个变量?
//is it possible to do this?
for(i = 0; i < 20; ++i){ //not sure if that should be i++ or ++i
//scan inventory[i] here
//check for a specific value of an instance such as item.value
//then add that to a variable
}
【问题讨论】:
-
不确定应该是
i++还是++i在这种情况下,这没关系,但我看到的通常 C++ 风格在那里使用++i。另外,对于库存,您不想要items 的数组吗? (item inventory[20] = /*blahblahblah*/) -
你尝试的时候发生了什么?数组不能是 int 类型,否则它只是一个循环。
-
@InternetAussie 有道理,我会利用这些信息 :)
-
@SamiKuhmonen 问题是我不知道该怎么做。
标签: c++ arrays class for-loop instance