Using Sets in C++

Sets use generics, which we have introduced for Pascal. Most online introductory examples are for sets of type int. Our first example is a tiny program that declares a set of type char then inserts three characters into it. Code to insert an existing member does not increase the set size. The program shows a way of testing for inclusion in the set. The output follows the code.

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  set<char> charSet;
  charSet.insert('.');
  charSet.insert('_');
  charSet.insert('-');
  charSet.insert('.'); 
  cout <<  "Set size: " << charSet.size() << endl;
  if (charSet.find('.') !=  charSet.end())
    cout <<  ". in set" << endl;
  if (charSet.find('a') ==  charSet.end())
    cout <<  "a not in set" << endl;
  return 0;
}
    

Output:

Set size: 3
. in set
a not in set

Output to the console with cout has similarities Pascal's to writeln in that you can separate different types (by << instead of a comma) and each type will be converted automatically to a string if necessary.

The second example shows how you can initialize a set (of type string) from an array and how to include in colourSet1 members of colourSet2 that are not in colourSet1. We use an iterator (pointer) and dereference when necessary to obtain the set member to which it points. See also how to delete an item. Notice that the strings are arranged by default in alphabetical order.

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  string colours[] = {"red", "orange", "yellow", "green", "blue"};
  set<string> colourSet1 (colours, colours + 5);
  if (colourSet1.find("blue") !=  colourSet1.end()) // without declaring an iterator
    cout <<  "blue in set" << endl;

  set<string>::iterator it;
  it = colourSet1.find("indigo");
  if (it ==  colourSet1.end())
    cout << "indigo not in set" << endl;
  else
    cout << "indigo in set" << endl;
  colourSet1.insert("indigo");
  cout << "indigo inserted" << endl;
  it = colourSet1.find("indigo");
  if (it ==  colourSet1.end())
    cout << "indigo not in set" << endl;
  else
    cout << "indigo in set" << endl;

  cout << "set size: " << colourSet1.size() << endl;
  it = colourSet1.find("orange");
  colourSet1.erase(it);
  cout << "orange removed from set" << endl;
  cout << "size of set now " << colourSet1.size() << endl;
  cout << "colours remaining in colourSet1: " << endl;
  for (it = colourSet1.begin(); it != colourSet1.end(); it++)
    cout << *it << " ";

  string colours2[] = {"indigo", "violet", "white", "black"};
  set<string> colourSet2 (colours2, colours2 + 4);
  cout << endl << "Making colourSet1 the union of colourSet1 and colourSet2" << endl;
  colourSet1.insert(colourSet2.begin(), colourSet2.end());
  cout << "colours in enlarged colourSet1: " << endl;
  for (it = colourSet1.begin(); it != colourSet1.end(); it++)
    cout << *it << " ";
  cout << endl;

  return 0;
}
    
blue in set
indigo not in set
indigo inserted
indigo in set
set size: 6
orange removed from set
size of set now 5
colours remaining in colourSet1:
blue green indigo red yellow
Making colourSet1 the union of colourSet1 and colourSet2
colours in enlarged colourSet1:
black blue green indigo red violet white yellow
Programming - a skill for life!

How to apply your knowledge of Pascal when learning C/C++