STL Container Class Set

◀ Queue and Stack▶ String
Amazon Set is one of the associative containers. An associative container associates a data item with a key and uses the key to find the corresponding data item. In a set, the key is the value itself. Here are the most important properties of a set:
  • Every value is unique within the set.
  • Values come out sorted in the ascending order.
The following program demonstrates these properties.
#include <iostream>
#include <set>
using namespace std;

int main ()
{
  int intArray[] = {5,4,1,3,2};
  set<int> setOfInt (intArray, intArray+5);

  set<int>::iterator it;

  cout << "setOfInt contains";
  for ( it=setOfInt.begin() ; it != setOfInt.end(); it++ )
    cout << " " << *it;

  cout << endl;

  return 0;
}
The program outputs the following:
setOfInt contains 1 2 3 4 5
You can change the sorting function, or the comparison function, by creating one and referencing it through a function pointer in the set’s constructor. You can find many examples online.

In addition there are several properties that mathematics defines for a set and they are embodied by the STL set as well. The union of two sets is a set that combines everything two sets cover, and each element is unique. The intersection of two sets is a set that consists of only the values that are common to both sets. The difference of two sets is a set that consists of the values that the first set has but the second one doesn’t.

To use set, include <set> (formerly <set.h>) in the header.
In the following table, abegin is an iterator pointing to the starting position of set A; aend is an iterator pointing to the ending position of set A; bbegin is an iterator pointing to the starting position of set B; bend is an iterator pointing to the ending position of set B; oiterator is an output iterator identifying the location for the resulting set to copy to. i and j are iterators.
FunctionDescription
set_union(abegin, aend, bbegin, bend, oiterator);returns the union of range [abegin, aend) and range [bbegin, bend)
set_intersection(abegin, aend, bbegin, bend, oiterator);returns the intersection of range [abegin, aend) and range [bbegin, bend)
set_difference(abegin, aend, bbegin, bend, oiterator);returns the difference of range [abegin, aend) and range [bbegin, bend)
A.lower_bound(k);returns an iterator to the first element whose key is equal to or greater than k
A.upper_bound(k);returns an iterator to the first element whose key is greater than k
A.insert(v);inserts v in A
A.insert(i, j);inserts range [i, j] in A
One thing you may be wondering about is how the fifth argument of the first three functions works. It is an output iterator, which specifies the location to store the resulting set. If it is ostream_iterator, that means that the set is printed onscreen.

However, if you want to copy the resulting set to a new set, say C, then you can use insert_iterator. Here is a sample program illustrating how set works:
#include <iostream>
#include <string>
#include <set>
using namespace std;

ostream & operator<<(ostream & os, set<string> s) {
	set<string>::iterator sti;
	for(sti = s.begin(); sti != s.end(); sti++)
		os << *sti << \' \';
	return os;
}

int main() {
	set<string> set1, set2, set3;
	int i;

	string ts="value#";
	string res;
	for(i=0;i<5;i++){
		res=ts;
		res+=(\'a\'+i);
		set1.insert(res);
	}
	for(i=0;i<10;i++){
		res=ts;
		res+=(\'a\'+i);
		set2.insert(res);
	}

	cout<<"set1: "<<set1<<"\nlower_bound: "<<*(set1.lower_bound("value#b"))<<
"\nupper_bound: "<<*(set1.upper_bound("value#b"))<<endl;
	cout<<"set2: "<<set2<<endl;
	set_union(set1.begin(), set1.end(), set2.begin(), set2.end(),
insert_iterator<set<string> >(set3, set3.begin()));
	cout<<"set3: "<<set3<<endl;
	return 0;
}
As you can see from the program output, set3 covers everything set1 and set2 cover. Now that you have acquired one more powerful tool, STL, you should not be afraid to use it from now on. Use it well and it can solve many problems for you.

Next we’ll look at one of the most powerful data types provided by C++, string! You cannot live without string because it is practically everywhere!

A CD contains error-checking code that can handle many scratches on its surface. So don’t worry when you scratch it.
◀ Queue and Stack▶ String

fShare
Questions? Let me know!