home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C20 / AssociativeBasics.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.6 KB  |  55 lines

  1. //: C20:AssociativeBasics.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Basic operations with sets and maps
  7. #include "Noisy.h"
  8. #include <iostream>
  9. #include <set>
  10. #include <map>
  11. using namespace std;
  12.  
  13. int main() {
  14.   Noisy na[] = { Noisy(), Noisy(), Noisy(), 
  15.     Noisy(), Noisy(), Noisy(), Noisy() };
  16.   // Add elements via constructor:
  17.   set<Noisy> ns(na, na+ sizeof na/sizeof(Noisy));
  18.   // Ordinary insertion:
  19.   Noisy n;
  20.   ns.insert(n);
  21.   cout << endl;
  22.   // Check for set membership:
  23.   cout << "ns.count(n)= " << ns.count(n) << endl;
  24.   if(ns.find(n) != ns.end())
  25.     cout << "n(" << n << ") found in ns" << endl;
  26.   // Print elements:
  27.   copy(ns.begin(), ns.end(), 
  28.     ostream_iterator<Noisy>(cout, " "));
  29.   cout << endl;
  30.   cout << "\n-----------\n";
  31.   map<int, Noisy> nm;
  32.   for(int i = 0; i < 10; i++)
  33.     nm[i]; // Automatically makes pairs
  34.   cout << "\n-----------\n";
  35.   for(int j = 0; j < nm.size(); j++)
  36.     cout << "nm[" << j <<"] = " << nm[j] << endl;
  37.   cout << "\n-----------\n";
  38.   nm[10] = n;
  39.   cout << "\n-----------\n";
  40.   nm.insert(make_pair(47, n));
  41.   cout << "\n-----------\n";
  42.   cout << "\n nm.count(10)= " 
  43.     << nm.count(10) << endl;
  44.   cout << "nm.count(11)= " 
  45.     << nm.count(11) << endl;
  46.   map<int, Noisy>::iterator it = nm.find(6);
  47.   if(it != nm.end())
  48.     cout << "value:" << (*it).second
  49.       << " found in nm at location 6" << endl;
  50.   for(it = nm.begin(); it != nm.end(); it++)
  51.     cout << (*it).first << ":" 
  52.       << (*it).second << ", ";
  53.   cout << "\n-----------\n";
  54. } ///:~
  55.