

#include <iostream>
#include <set>
using namespace std;



void displayinterval(int const val, set<int> const & c)
{
  int a1 = *c.upper_bound(val);
  int a0 = *(c.lower_bound(val));

  if (a0==a1)
    a0 = *(--(c.lower_bound(val)));

  
  cout << "val=" << val << "  in [";
  cout << a0 << ", " << a1 << "]" << endl;
}



void test01()
{
  cout << "sets partiton a space" << endl;
  cout << "  default is the < partition" << endl;
  int a[9] = 
  {  
    3, 28, 2, 0, 9, 
    67, 6, 14, 15 
  };

  // Insert into the set
  set<int> c(a,a+9);
  // Another insert, as sets only store
  //  unique elements this does nothing.
  for (unsigned int i=0; i<sizeof(a)/sizeof(int); ++i)
    c.insert(a[i]);

  for ( set<int>::iterator i=c.begin();
    i != c.end(); ++i)
  {
    cout << *i << " ";
  }
  cout << endl;

  cout << " 3, 4, 5 < 6" << endl;
  cout << " 6,7,8 is < 9" << endl;

  cout << "Looking at region about a value." << endl;
  cout << "Enter a value:  ";
  int val;
  cin >> val;
  cout << "lower=" << *c.lower_bound(val) << endl;
  cout << "upper=" << *c.upper_bound(val) << endl;

// Note the nature of the lower bound is not the same
// as the mathematical definition/idea. 
// if  *c.lower_bound(val)==val  then the lower bound is in the set,
// else the upper bound is returned.

  displayinterval(val,c);

  set<int>::const_iterator iend(c.end());
  if (c.find(val)!=iend)
    cout << "found" <<endl;
  else
    cout << "not found" << endl;

}


int main(int argc, char** argv)
{

  test01();

  return 0;
}


