2013-07-30 32 views
0

我希望此函数使用for循环来遍历我的两个向量(结构体),将最内层结构中每个对象的平衡添加到变量“银行存款余额”。C++通过构造向量循环(包含更多的结构向量)

我不确定如何正确地循环通过此系统来实现此目的。我想我的语法有问题,我试图在结构中调用该向量。

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; 
} 

我真的不知道如何格式化第二个循环。任何提示将不胜感激,我已经尝试了我能想到的所有事情的组合,以及我在网上看到的所有内容。

+1

为什么要在向量中存储指针?你只是把RAII扔出窗外... – Borgleader

回答

1

在C++中,声明结构时不需要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; 
    } 
} 
1

你的结构不包含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被忽略。它只是增加了代码的混乱。

最后,一眼就可以看到没有理由在您的代码中使用指针。如果您存储值,它将会大大简化。

0
double checkBankBalance() 
{ 
double bankBalance; 
for (auto &item : users) 
{ 
    for (auto &item : users[item].Accounts) 
    { 
     bankBalance = bankBalance + item->balance; 
    } 
} 

return 0; 
} 
  1. 你没有初始化 “bankBalance”
  2. 你没有返回(或使用)bankBalance,
  3. 当您添加一个成员变量, “bankBalance” 你结构体,它在这个函数里面是不可见的。
  4. 你还没有掌握C++中的类型声明与成员声明。

“typedef”定义了一个类型。所以你不需要它的“结构”或“类”的前面,你肯定不希望它声明变量。

为了您自己的理智,考虑使成员变量名与其他变量不同,并且类/结构名不同。通常的做法是使用“m_”作为“member”的前缀,类别使用upper-camel-case,静态使用“s_”,全局使用“g_”。

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; 
} 
+0

谢谢大家的回复,我提出了你的建议修改,它的效果非常好。只要我达到要求,我就会立刻起来。 – Wenzel745