Lesson 5: Standard Library Containers and Algorithms
Activity 19: Storing User Accounts
First, we include the header files for the array class and input/output operations with the required namespace:
#include <array>
An array of ten elements of type int is declared:
array<int,10> balances;
Initially, the values of the elements are undefined since it is an array of the fundamental data type int. The array is initialized using a for loop, where each element is initialized with its index. The operator size() is used to evaluate the size of the array and the subscript operator [ ] is used to access every position of the array:
for (int i=0; i < balances.size(); ++i) { balances[i] = 0; }
We now want to update the value for the first and last user. We can use front() and back() to access the accounts of these users:
balances.front() += 100; balances.back() += 100;
We would like to store the account balance of an arbitrary number of users. We then want to add 100 users to the account list...