text cover

Data Abstraction and Problem Solving with C++

Walls and Mirrors

by Frank M. Carrano

Addison Wesley Logo

example640.cpp

Go to the documentation of this file.
00001 
00015 #include <set>
00016 #include <iostream>
00017 #include <string>
00018 #include <algorithm>
00019 
00020 using namespace std;
00021 
00022 int main()
00023 {
00024    // declares three sets
00025    set<string> checkingAccounts;
00026    set<string> savingAccounts;
00027    set<string> bankAccounts;
00028 
00029    // declare a set iterator
00030    set<string>::const_iterator i;
00031 
00032    // insert customers with checking accounts
00033    checkingAccounts.insert("Marlow");
00034    checkingAccounts.insert("Johnson");
00035    checkingAccounts.insert("Garner");
00036    checkingAccounts.insert("Smith");
00037 
00038    // print checking account customers
00039    cout << "Checking accounts: " << endl;
00040    for (i = checkingAccounts.begin();
00041         i != checkingAccounts.end(); ++i)
00042    {
00043       cout << *i << " ";
00044    }  // end for
00045    cout << endl << endl;
00046 
00047    // insert customers with savings accounts
00048    savingAccounts.insert("Johnson");
00049    savingAccounts.insert("Abbott");
00050    savingAccounts.insert("Stricker");
00051    savingAccounts.insert("Marlow");
00052 
00053    // print saving account customers
00054    cout << "Savings accounts: " << endl;
00055    for (i = savingAccounts.begin();
00056         i != savingAccounts.end(); ++i)
00057    {
00058       cout << *i << " ";
00059    }  // end for
00060    cout << endl << endl;
00061 
00062    // Union of checking and savings accounts
00063    // The inserter is an output iterator that inserts elements
00064    // into a set before printing the set
00065    //
00066    set_union(checkingAccounts.begin(), checkingAccounts.end(),
00067              savingAccounts.begin(), savingAccounts.end(),
00068              inserter(bankAccounts,bankAccounts.begin()));
00069 
00070    cout << "All bank customers: " << endl;
00071    for (i = bankAccounts.begin(); i != bankAccounts.end(); ++i)
00072    {
00073       cout << *i << " ";
00074    }  // end for
00075    cout << endl << endl;
00076 
00077    // Erase all elements of the result set for the next operation
00078    bankAccounts.erase(bankAccounts.begin(), bankAccounts.end());
00079 
00080    // Intersection of checking and saving accounts
00081    set_intersection(checkingAccounts.begin(),
00082                     checkingAccounts.end(),
00083                     savingAccounts.begin(),savingAccounts.end(),
00084                     inserter(bankAccounts,bankAccounts.begin()));
00085 
00086    cout << "Customers with checking and savings accounts: "
00087         << endl;
00088    for (i = bankAccounts.begin(); i != bankAccounts.end(); ++i)
00089    {
00090       cout << *i << " ";
00091    }  // end for
00092    cout << endl;
00093 
00094    return 0;
00095 }  // end main

Generated on Sun Aug 27 22:03:18 2006 for AWLogo by  doxygen 1.4.6