3 // Copyright (C) 2005-2013 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 3, or (at your option) any later
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
21 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
23 // Permission to use, copy, modify, sell, and distribute this software
24 // is hereby granted without fee, provided that the above copyright
25 // notice appears in all copies, and that both that copyright notice
26 // and this permission notice appear in supporting documentation. None
27 // of the above authors, nor IBM Haifa Research Laboratories, make any
28 // representation about the suitability of this software for any
29 // purpose. It is provided "as is" without express or implied
33 * @file basic_multimap_example.cpp
34 * A basic example showing how to use multimaps.
38 * This example shows how to use "multimaps" in the context of a simple
39 * bank account application. Each customer holds a bank account
40 * (or more than one) which holds some balance.
46 #include <ext/pb_ds/assoc_container.hpp>
49 using namespace __gnu_pbds
;
51 // A simple hash functor.
52 // hash could serve instead of this functor, but it is not yet
53 // standard everywhere.
54 struct string_hash
: public unary_function
<string
, size_t>
57 operator()(const string
& r_s
) const
60 string::const_iterator b
= r_s
.begin();
61 string::const_iterator e
= r_s
.end();
65 ret
+= static_cast<size_t>(*(b
++));
73 // Each customer is identified by a string.
74 typedef string customer
;
76 // Each account is identified by an unsigned long.
77 typedef unsigned long account_id
;
79 // The balance in the account is a floating point.
80 typedef float balance_t
;
83 * This is the data structure type used for storing information
84 * about accounts. In this case the primary key is the customer,
85 * and the secondary key is the account id.
87 * A hash-based container maps each customer to a list-based
88 * container that maps each account to the balance it holds.
90 * Note that we could use any combination of primary and secondary
91 * associative-containers. In this case we choose a hash-based
92 * container for the primary keys, since we do not need to store
93 * customers in a sorted order; we choos a list-based container for
94 * the secondary keys, since we expect that the average number of
95 * accounts per customer will be small.
106 // This object will hold all information.
109 // Customer "a" opens empty account 12.
112 // Customer "a" deposits 45 into account 12.
115 // Customer "b" opens account 13 with balance 12.3.
118 // Customer "c" opens empty account 14.
121 // Customer "a" opens account 160 with balance 142.
122 // Note that "a" already holds account 12.
125 // Verify the number of accounts that "a" holds.
126 accounts_t::point_const_iterator it
= acc
.find("a");
127 assert(it
!= acc
.end());
128 assert(it
->second
.size() == 2);
130 // The begining of the month has arrived. We need to give a 3%
131 // interest to all accounts with a positive balance.
133 // First we loop over all customers.
134 accounts_t::iterator cust_it
;
135 for (cust_it
= acc
.begin(); cust_it
!= acc
.end(); ++cust_it
)
137 // For each customer, we loop over the customer's accounts.
138 accounts_t::mapped_type::iterator it
;
139 for (it
= cust_it
->second
.begin(); it
!= cust_it
->second
.end(); ++it
)
144 // Customer "a" closes all accounts.
147 // The bank now has only 2 customers.
148 assert(acc
.size() == 2);