To add an element to a collection, call the add function. Consider the following example:
//main.cpp - main file
#include <iset.h>
#include <iostream.h>
#include "person.h" //person.h from the previous examples
typedef ISet<Person> AddressList;
void main()
{
AddressList Business;
Person A("Peter Black","714-50706");
Person B("Carl Render","714-540321");
Person C("Sandra Summers","214-660012");
Business.add(A);
Business.add(B);
Business.add(C);
Business.add(A); //Person A is added for the second time
cout << "\nThe set now contains " << Business.numberOfElements()
<<" entries!\n";
}
If you run the program, the set will only contain 3 different entries. In a set, each element is unique. No two elements can be the same. To illustrate the difference between sets and bags, run the program using a bag rather than a set.
![]()
Introduction
to the Collection Classes
Adding
Elements
Removing
Elements
Replacing
Elements
![]()
Class Template Naming
Conventions
Possible
Implementation Paths
Choosing One of the
Provided Implementation Variants
Removing an Element
from a Collection
Taking Advantage of the
Abstract Class Hierarchy
Using Collection
Notification
Instantiating the
Collection Classes
Troubleshooting Problems
while Using the Collection Class Library