Professional C__ - Marc Gregoire [220]
{
// Just add the customer/channels pair to the customers map.
mCustomers.insert({name, channels});
}
void CableCompany::addChannel(const string& name, int channel)
{
// Find a reference to the customer.
auto it = mCustomers.find(name);
if (it != mCustomers.end()) {
// We found this customer; set the channel.
// Note that 'it' is a reference to a name/bitset pair.
// The bitset is the second field.
it->second.set(channel);
}
}
void CableCompany::removeChannel(const string& name, int channel)
{
// Find a reference to the customer.
auto it = mCustomers.find(name);
if (it != mCustomers.end()) {
// We found this customer; remove the channel.
// Note that 'it' is a reference to a name/bitset pair.
// The bitset is the second field.
it->second.reset(channel);
}
}
void CableCompany::addPackageToCustomer(const string& name,
const string& package)
{
// Find the package.
auto itPack = mPackages.find(package);
// Find the customer.
auto itCust = mCustomers.find(name);
if (itCust != mCustomers.end() && itPack != mPackages.end()) {
// Only if both package and customer found, can we do the update.
// Or-in the package to the customers existing channels.
// Note that the iterators are references to name/bitset pairs.
// The bitset is the second field.
itCust->second |= itPack->second;
}
}
void CableCompany::deleteCustomer(const string& name)
{
// Remove the customer with this name.
mCustomers.erase(name);
}
bitset throw(out_of_range) { // Find the customer. auto it = mCustomers.find(name); if (it != mCustomers.end()) { // Found it! // Note that 'it' is a reference to a name/bitset pair. // The bitset is the second field. return it->second; } // Didn't find it. Throw an exception. throw out_of_range("No customer with that name"); } Code snippet from CableCompany\CableCompany.cpp Finally, here is a simple program demonstrating how to use the CableCompany class: CableCompany myCC; auto basic_pkg = "1111000000"; auto premium_pkg = "1111111111"; auto sports_pkg = "0000100111"; myCC.addPackage("basic", bitset myCC.addPackage("premium", bitset myCC.addPackage("sports", bitset myCC.newCustomer("Nicholas Solter", "basic"); myCC.addPackageToCustomer("Nicholas Solter", "sports"); cout << myCC.getCustomerChannels("Nicholas Solter") << endl; Code snippet from CableCompany\CableCompanyTest.cpp Note that this example uses a lot of C++11 features. For example, the addPackage() method has the following line: mPackages.insert({packageName, channels}); This line uses C++11 uniform initialization. If your compiler does not support this you need to write it as follows: mPackages.insert(make_pair(packageName, channels)); SUMMARY Now that you are familiar with the containers, the next chapter can illustrate the true power of the STL by discussing the generic algorithms. Chapter 13 Mastering STL Algorithms What algorithms are What lambda expressions are What function objects are The details of the STL algorithms A larger example: auditing voter registrations As Chapter 12 shows, the STL provides an impressive collection of generic data structures. Most libraries stop there. The STL, however, contains an additional assortment of generic algorithms that can, with some exceptions, be applied to elements from any container. Using these algorithms, you can find, sort, and process
This chapter introduced the standard template library containers. It also presented sample code illustrating a variety of uses for these containers. Hopefully, you appreciate the power of the vector, deque, list, array, forward_list, stack, queue, priority_queue, map, multimap, set, multiset, unordered_map, unordered_multimap, unordered_set, unordered_multiset, string, and bitset. Even if you don’t incorporate them into your programs immediately, at least keep them in the back of your mind for future projects.
WHAT’S IN THIS CHAPTER?