CPP/STL/Set

From ProgrammingExamples
< CPP
Revision as of 16:24, 22 June 2010 by Daviddoria (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Set.cpp

#include <iostream>
#include <set>
#include <vector>
#include <cstdlib>
 
#include "Height.h"
 
double RandomDouble();
 
void Report(std::set<double> &S); //show the size of the set and whether or not it is empty
void Report(std::set<unsigned int> &S); //show the size of the set and whether or not it is empty
void Output(std::set<double> &S); //show all of the elements in the set
void Report(std::multiset<double> &S); //show the size of the multiset and whether or not it is empty
void Output(std::multiset<double> &S); //show all of the elements in the multiset
 
void TestSet();
void TestMultiSet();
 
void TestFind(); //look for an element in a set
 
int main(int argc, char* argv[])
{
  //TestSet();
  //TestMultiSet();
 
  TestFind();
  return 0;
}
 
void TestFind()
{
  //create a set
  std::set<unsigned int> S;
 
  //show the statistics - it should be empty
  Report(S);
 
  //add 10 integers (0 to 9) to the set
  for(unsigned int i = 0; i < 10; i++)
  {
    S.insert(i);
  }
 
  //show the statistics - it should have 10 elements
  Report(S);
 
  //find the element '5' - it should be there!
  std::set<unsigned int>::iterator it;
  it = S.find(5);
  std::cout << "5 was " ;
  if(it!=S.end())
  {
    std::cout << " found!" << std::endl;
  }
  else
  {
    std::cout << " NOT found!" << std::endl;
  }
 
 
  //find the element '20' - it should NOT be there!
  it = S.find(20);
  std::cout << "20 was ";
  if(it!=S.end())
  {
    std::cout << " found!" << std::endl;
  }
  else
  {
    std::cout << " NOT found!" << std::endl;
  }
}
 
void TestSet()
{
  //create a set
  std::set<double> S;
 
  //show the statistics - it should be empty
  Report(S);
 
  //add 10 random elements to the set
  for(unsigned int i = 0; i < 10; i++)
  {
    S.insert(RandomDouble());
  }
 
  //show the statistics - it should have 10 elements and not be empty
  Report(S);
 
  //show all of the elements in the set
  Output(S);
 
  //erase the second element
  std::set<double>::iterator it1 = S.begin();
  it1++;
  S.erase(it1);
 
  //show the size of the set to be 9 and display all of the elements
  Report(S);
  Output(S);
 
  //throw away all of the elements in the set
  S.clear();
 
  //show that it is now empty
  Report(S);
 
}
 
 
void TestMultiSet()
{
  std::multiset<double> S;
 
  Report(S);
  for(unsigned int i = 0; i < 10; i++)
  {
    S.insert(RandomDouble());
  }
 
  Report(S);
  Output(S);
 
  //erase the second element
  std::set<double>::iterator it1 = S.begin();
  it1++;
  S.erase(it1);
 
  Report(S);
  Output(S);
 
  S.clear();
 
  Report(S);
 
}
 
 
void TestHeightSet()
{
  std::set<Height> S;
 
  for(unsigned int i = 0; i < 10; i++)
  {
    S.insert(Height(RandomDouble()));
  }
 
}
 
double RandomDouble()
{
  //produce a random double between 0 and 1
  return drand48();
}
 
void Output(std::set<double> &S)
{
  //for(list<double>::iterator it1 = L.begin(); it1 != L.end(); ++it1)
  for(std::set<double>::iterator it1 = S.begin(); it1 != S.end(); it1++)
  {
	      std::cout << " " << *it1;
  }
 
  std::cout << std::endl;
}
 
void Output(std::multiset<double> &S)
{
  //for(list<double>::iterator it1 = L.begin(); it1 != L.end(); ++it1)
  for(std::set<double>::iterator it1 = S.begin(); it1 != S.end(); it1++)
  {
    std::cout << " " << *it1;
  }
 
  std::cout << std::endl;
}
 
void Output(std::vector<double> &V)
{
  for(unsigned int i = 0; i < V.size(); i++)
  {
    std::cout << " " << V[i];
  }
 
  std::cout << std::endl;
}
 
void Report(std::set<double> &S)
{
  std::cout << "Size: " << S.size() << " Empty? " << S.empty() << std::endl;
}
 
void Report(std::set<unsigned int> &S)
{
  std::cout << "Size: " << S.size() << " Empty? " << S.empty() << std::endl;
}
 
void Report(std::multiset<double> &S)
{
  std::cout << "Size: " << S.size() << " Empty? " << S.empty() << std::endl;
}