[英]C++ For loops through Vectors of Structs (containing more vectors of structs)
我希望这个函数用于循环迭代我的两个向量(结构),将最里面结构中每个对象的余额添加到变量“银行余额”。
我不确定如何正确地循环这个系统来实现这一目标。 我认为我的语法有问题,我试图在结构中调用向量。
typedef struct account
{
string transactionLog;
float balance;
string *pOwner;
int accountNumber;
string label;
};
typedef account* pAccount;
typedef struct user
{
string testUsername;
string customerName;
string testPassword;
bool isCustomer;
bool isTeller;
bool isManager;
user(string username, string testpassword, string customerName, bool iscustomer, bool isteller, bool ismanager)
: testUsername(username), testPassword(testpassword), customerName(customerName), isCustomer(iscustomer),
isTeller(isteller), isManager(ismanager) {}
typedef vector<pAccount> Accounts;
};
typedef user* pUser;
typedef vector<pUser> userVector;
userVector users;
int vectorPos;
double checkBankBalance()
{
double bankBalance;
for (auto &item : users)
{
for (auto &item : users[item].Accounts)
{
bankBalance = bankBalance + item->balance;
}
}
return 0;
}
我真的不知道如何格式化第二个for循环。 任何提示都会非常感激,我已经尝试了我能想到的各种组合,以及我在网上看到的一切。
你的struct不包含vector
,它只有一个typedef
:
typedef vector<pAccount> Accounts;
如果要将Accounts
作为数据成员,请删除typedef
。
vector<pAccount> Accounts;
此外,您应该认真考虑不对嵌套循环的两个级别中的项使用相同的名称:
for (auto& user : users)
{
for (auto& account : user.Accounts)
{
另请注意,您不需要使用typedef
来声明结构。 在typedef struct Foo {};
,typedef被忽略。 它只是增加了代码的混乱。
最后,一目了然,似乎没有理由在代码中使用指针。 如果您存储值,则会大大简化。
在c ++中,声明struct时不需要typedef。 在struct中,在typedef之后声明Account。 Typedef没有声明。
struct user
{
string testUsername;
string customerName;
string testPassword;
bool isCustomer;
bool isTeller;
bool isManager;
user(string username, string testpassword, string customerName, bool iscustomer, bool isteller, bool ismanager)
: testUsername(username), testPassword(testpassword), customerName(customerName), isCustomer(iscustomer),
isTeller(isteller), isManager(ismanager) {}
typedef vector<pAccount> Accounts;
Accounts accounts;
};
在循环内,将Account(对象类型)更改为account(对象本身)。 也不需要引用该项,因为它已经是指针类型。 (你只是复制地址)。
在内部循环中,直接访问用户,因为该范围使您可以直接访问索引处的对象。
for (auto user : users)
{
for (auto account : user.accounts)
{
bankBalance = bankBalance + account->balance;
}
}
double checkBankBalance()
{
double bankBalance;
for (auto &item : users)
{
for (auto &item : users[item].Accounts)
{
bankBalance = bankBalance + item->balance;
}
}
return 0;
}
“typedef”定义了一种类型。 所以你不需要它在“struct”或“class”之前,你在声明变量时绝对不需要它。
为了您自己的理智,请考虑使成员变量名与其他变量不同,并且类/结构名称不同。 通常的做法是使用“m_”作为“member”的前缀,使用upper-camel-case作为类,“s_”表示静态,“g_”表示globals。
struct Account /* Capitalize struct/class names */
{
string m_transactionLog;
float m_balance;
string *m_pOwner; // I've got a bad feeling about this.
int m_accountNumber;
string m_label;
};
当您实现以下内容时,将会出现解决方案:
typedef struct User /* capitalize class names */
{
string m_testUsername;
//...
user(const string& username, const string& password, const string& customerName, bool isCustomer, bool isTelelr, bool isManager)
: m_testUsername(username), m_testPassword(password)
, m_customerName(customerName /* ouch, this was broken before*/)
, isCustomer(isCustomer)
, isTeller(isTeller)
, isManager(isManager)
{}
...
// Look ma: a type definition
//typedef vector<pAccount> Accounts;
// Well, ma, we actually wanted a member, not a type.
vector<pAccount> m_accounts; // Ok, pointers to accounts, I have a bad feeling again.
};
现在checkBankBalance变得相当直观。
double checkBankBalance()
{
double bankBalance = 0; // local and farm bought.
for (auto &user: g_users) // everything in users.
{
// now we want to iterate over the accounts member of the user.
// which will be 'm_accounts'. Since it's a pointer, don't use &
for (auto item : user.m_accounts)
{
bankBalance = bankBalance + item->balance;
}
}
/// do something with bankBalance here
/// ...
///
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.