// search through the elements.
for (auto& p : mAccounts) {
if (p.second.getClientName() == name) {
// found it!
return p.second;
}
throw out_of_range("No account with that name.");
void BankDB::mergeDatabase(BankDB& db)
{
// Just insert copies of all the accounts in the old db
// to the new one.
mAccounts.insert(db.mAccounts.begin(), db.mAccounts.end());
// Now delete all the accounts in the old one.
db.mAccounts.clear();
Code snippet from BankAccount\BankDB.cpp
Note that this code uses a couple of C++11 features. For example, take the following line from the addAccount() method:
auto res = mAccounts.insert(make_pair(acct.getAcctNum(), acct));
If your compiler does not support the C++11 auto keyword, this should be written as follows:
pair::iterator, bool> res;res = mAccounts.insert(make_pair(acct.getAcctNum(), acct));Code snippet from BankAccount\BankDB.cppYou can test the BankDB class with the following code:BankDB db;db.addAccount(BankAccount(100, "Nicholas Solter"));db.addAccount(BankAccount(200, "Scott Kleper"));try {auto acct = db.findAccount(100);cout << "Found account 100" << endl;acct.setClientName("Nicholas A Solter");auto acct2 = db.findAccount("Scott Kleper");cout << "Found account of Scott Kelper" << endl;auto acct3 = db.findAccount(1000);} catch (const out_of_range&) {cout << "Unable to find account" << endl;}Code snippet from BankAccount\BankDBTest.cppThe output should be as follows:Found account 100Found account of Scott KelperUnable to find accountmultimapThe multimap is a map that allows multiple elements with the same key. Like maps, multimaps support the C++11 uniform initialization. The interface is almost identical to the map interface, with the following changes:multimaps do not provide operator[]. The semantics of this operator does not make sense if there can be multiple elements with a single key.Inserts on multimaps always succeed. Thus, the multimap insert() method that adds a single element returns only an iterator.multimaps allow you to insert identical key/value pairs. If you want to avoid this redundancy, you must check explicitly before inserting a new element.The trickiest aspect of multimaps is looking up elements. You can’t use operator[], because it is not provided. find() isn’t very useful because it returns an iterator referring to any one of the elements with a given key (not necessarily the first element with that key).However, multimaps store all elements with the same key together and provide methods to obtain iterators for this subrange of elements with the same key in the container. The lower_bound() and upper_bound() methods each return a single iterator referring to the first and one-past-the-last elements matching a given key. If there are no elements matching that key, the iterators returned by lower_bound() and upper_bound() will be equal to each other.In case you don’t want to call two separate methods to obtain the iterators bounding the elements with a given key, multimaps also provide equal_range(), which returns a pair of the two iterators that would be returned by lower_bound() and upper_bound().The example in the next section illustrates the use of these methods.The lower_bound(), upper_bound(), and equal_range() methods exist for maps as well, but their usefulness is limited because a map cannot have multiple elements with the same key.multimap Example: Buddy ListsMost of the numerous online chat programs allow users to have a “buddy list” or list of friends. The chat program confers special privileges on users in the buddy list, such as allowing them to send unsolicited messages to the user.One way to implement the buddy lists for an online chat program is to store the information in a multimap. One multimap could store the buddy lists for every user. Each entry in the container stores one buddy for a user. The key is the user and the value is the buddy. For example, if Harry Potter and Ron Weasley had each other
res = mAccounts.insert(make_pair(acct.getAcctNum(), acct));
You can test the BankDB class with the following code:
BankDB db;
db.addAccount(BankAccount(100, "Nicholas Solter"));
db.addAccount(BankAccount(200, "Scott Kleper"));
try {
auto acct = db.findAccount(100);
cout << "Found account 100" << endl;
acct.setClientName("Nicholas A Solter");
auto acct2 = db.findAccount("Scott Kleper");
cout << "Found account of Scott Kelper" << endl;
auto acct3 = db.findAccount(1000);
} catch (const out_of_range&) {
cout << "Unable to find account" << endl;
Code snippet from BankAccount\BankDBTest.cpp
The output should be as follows:
Found account 100
Found account of Scott Kelper
Unable to find account
multimap
The multimap is a map that allows multiple elements with the same key. Like maps, multimaps support the C++11 uniform initialization. The interface is almost identical to the map interface, with the following changes:
multimaps do not provide operator[]. The semantics of this operator does not make sense if there can be multiple elements with a single key.
Inserts on multimaps always succeed. Thus, the multimap insert() method that adds a single element returns only an iterator.
multimaps allow you to insert identical key/value pairs. If you want to avoid this redundancy, you must check explicitly before inserting a new element.
The trickiest aspect of multimaps is looking up elements. You can’t use operator[], because it is not provided. find() isn’t very useful because it returns an iterator referring to any one of the elements with a given key (not necessarily the first element with that key).
However, multimaps store all elements with the same key together and provide methods to obtain iterators for this subrange of elements with the same key in the container. The lower_bound() and upper_bound() methods each return a single iterator referring to the first and one-past-the-last elements matching a given key. If there are no elements matching that key, the iterators returned by lower_bound() and upper_bound() will be equal to each other.
In case you don’t want to call two separate methods to obtain the iterators bounding the elements with a given key, multimaps also provide equal_range(), which returns a pair of the two iterators that would be returned by lower_bound() and upper_bound().
The example in the next section illustrates the use of these methods.
The lower_bound(), upper_bound(), and equal_range() methods exist for maps as well, but their usefulness is limited because a map cannot have multiple elements with the same key.
multimap Example: Buddy Lists
Most of the numerous online chat programs allow users to have a “buddy list” or list of friends. The chat program confers special privileges on users in the buddy list, such as allowing them to send unsolicited messages to the user.
One way to implement the buddy lists for an online chat program is to store the information in a multimap. One multimap could store the buddy lists for every user. Each entry in the container stores one buddy for a user. The key is the user and the value is the buddy. For example, if Harry Potter and Ron Weasley had each other