C ++ For循环结构的向量(包含更多结构向量)

时间:2013-07-30 03:53:35

标签: c++ for-loop c++11 vector struct

我希望这个函数用于循环遍历我的两个向量(结构),将最里面结构中每个对象的余额添加到变量“bank balance”。

我不确定如何正确地遍历此系统以实现此目的。我认为我的语法有问题,我试图在结构中调用向量。

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循环。任何提示都会非常感激,我已经尝试了我能想到的各种组合,以及我在网上看到的一切。

3 个答案:

答案 0 :(得分: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。它只是增加了代码的混乱。

最后,一目了然,似乎没有理由在代码中使用指针。如果你存储了值,它会大大简化。

答案 1 :(得分:1)

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

答案 2 :(得分: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 ++中的类型声明与成员声明。
  5. “typedef”定义了一种类型。所以你不需要它在“struct”或“class”之前,你在声明变量时绝对不需要它。

    为了您自己的理智,请考虑使成员变量名与其他变量不同,并且类/结构名称不同。一种常见的做法是使用“m_”作为“成员”的前缀,使用上层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;
    }