An example of a set {1 2 3}
The class Set will have the following characteristics:
Attributes:
elements (a vector of int values)
Constructors:
Set() // creates an empty Set
Set(int element) // creates a Set having only the given element
I really need help defining my default constructor.
The first lines of code in main are:
Set s; //initially empty set
istringstream in("{1 2 3 4 5 6 7 8 9 10}");
in >> s;
Set t;
cout << "Enter a set of integers eg {1 2 3}\n";
cin >> t;
I do not know how to define set so that it can take in {1 2 3}. I tried defining it as
Set(){
elements;
}
but I know that's probably wrong. I thought of including a string to handle the characters '{' and '}' but again, I'm not sure how to implement it.
A set is a collection of values of the same type. For example, {1 2 3} is a set whose elements are 1, 2, and 3. A set is not allowed to contain duplicate elements. For example, {1 1} is the set {1} of size 1. You are going to define a Set class which holds a collection of integer values as well as some operations on sets. We will define these operations by overloading C++ operators. The specifications for the assignment are below:
The class Set will have the following characteristics:
Attributes:
elements (a vector of int values)
Constructors:
Set() // creates an empty Set
Set(int element) // creates a Set having only the given element
Some examples of operator overloading functions in the class:
- operator[] takes in an index and returns the element at the given index. It should not change the calling object. If the given index is out of bounds, print an error message and call exit(1) to terminate the program.
- operator+= computes the union of the calling object Set and the Set object passed in and stores the resulting union as the calling object.
The union of two sets is the set formed by including the elements of both sets into one big set. For example, the set union of {1 2} and {4 3 2} is the set {1 2 3 4}.