How to iterate through a vector of objects?

Oct 14, 2017 at 3:48pm
I am attempting to make a program that stores records of user accounts, including their name, balance, account type etc. Such that the user can enter a line of text denoting the desired action (create an account, withdraw etc), the account name, and account type/amount.

example of input: c josh y

c creates an account

josh is the name

y is a credit account

I am having trouble finding a way of iterating through the listAccounts vector in change_balance() such that the user can find a particular account by its name in vector listAccounts, and change the balance of that particular object in the vector.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
using namespace std;
#include <sstream>
#include <vector>

class Account {
    public:
        string account_name;
        bool account_type;      //'y' = creidt account = 1, 'n' = basic account = 0
        int account_balance;
        static vector<Account> listAccounts;
        
        //member function declaration
        void init_account(string name, char type);
        void change_balance(string name, int balance);
        void remove_account(string name);
        
};

vector<Account> Account::listAccounts;


//member functions definitions
void Account::init_account(string name, char type) {
    
    Account account;
    
    if (type == 'y') {              //setting the account type  (credit/basic)
        account.account_type = 1;
    } else if (type == 'n') {
        account.account_type = 0;
    }

    account.account_name = name;
    account.account_balance = 0;

    listAccounts.push_back(account);
}

void Account::change_balance(string name, int balance) {
    
    Account account;
    //find particular account by name in vector listAccounts, and change balance
}

void Account::remove_account(string name) {
    
    //find particular account by name in vector listAccounts, and remove element
}


//main
int main() {
    
    char action;
    string names;
    char types;
    int balance;
    Account account;
    
    cin >> action >> names;
    
    if (action == 'c') {
        cin >> types;
        account.init_account(names, types);
        
    }
    
    else if (action == 't') {
        cin >> balance;
        account.change_balance(names, balance);  
    }
    
    else (action == 'r'); {
        account.remove_account(names);
    }
    
    return 0;   
}



Similarly, being able to find a vector element of listAccounts by name and removing that element.

Thanks.
Oct 14, 2017 at 4:18pm
Create iterator to the vector of interest:

auto iter = listAccounts.begin();

Go through every element in the vector looking for the relevant one:

1
2
3
4
5
6
7
for ( ; iter !=  listAccounts.end(); iter++)
{
  if ((*iter).account_name == the_name_of_interest)
  {
      // do something; change it or erase
  }
}


You can erase using the iterator; http://www.cplusplus.com/reference/vector/vector/erase/
Oct 14, 2017 at 4:46pm
std::find_if http://www.cplusplus.com/reference/algorithm/find_if/

1
2
3
4
5
6
7
8
9
void foo( string name )
{
  auto it = std::find_if( accounts.begin(), accounts.end(),
                          [&name](const Account& A){ return A.account_name == name; } );
  if ( accounts.end() != it )
  {
    // use it
  }
}
Oct 15, 2017 at 7:33pm
Thanks, very helpful, I rewrote the member functions to iterate through the vector:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
void Account::init_account(string name, char type) {
    
    Account account;
    
    if (type == 'y') {              //setting the account type  (credit/basic)
        account.account_type = 1;
    } else if (type == 'n') {
        account.account_type = 0;
    }

    account.account_name = name;
    account.account_balance = 0;

    listAccounts.push_back(account);

}

void Account::change_balance(string name, int balance) {
    
    auto iter = listAccounts.begin();
    
    for ( ; iter !=  listAccounts.end(); iter++) {
        if ((*iter).account_name == name) {
            (*iter).account_balance = balance;
            cout << (*iter).account_balance;
        }
        if (((*iter).account_type == 0) && ((*iter).account_balance < 0)) {
                cout << "account cannot hold negative balance""\n";
                (*iter).account_balance = (((*iter).account_balance) - (balance));
                cout << (*iter).account_balance;
        } 
    }
}

void Account::remove_account(string name) {
    
    auto iter = listAccounts.begin();
    
    for ( ; iter !=  listAccounts.end(); iter++) {
        if ((*iter).account_name == name) {
            listAccounts.erase(iter);
            break;
        }
    }
    
}


I also want to be able to iterate through the entire array in change_balance() to check if an element with name exists in the first place, but I can't think for the life of me how to search through the array to check if the name exists, if it doesn't, cout "does not exist".
Last edited on Oct 15, 2017 at 8:09pm
Topic archived. No new replies allowed.