Difference between revisions of "CPP/STL/Set"

From ProgrammingExamples
< CPP
Jump to: navigation, search
(Created page with '==Set.cpp== <source lang="cpp"> #include <iostream> #include <set> #include <vector> #include <cstdlib> #include "Height.h" double RandomDouble(); void Report(std::set<double>…')
 
Line 6: Line 6:
 
#include <cstdlib>
 
#include <cstdlib>
  
#include "Height.h"
+
#include <algorithm>
 
+
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[])
 
int main(int argc, char* argv[])
 
{
 
{
   //TestSet();
+
   // Create a set
  //TestMultiSet();
+
 
+
  TestFind();
+
  return 0;
+
}
+
 
+
void TestFind()
+
{
+
  //create a set
+
 
   std::set<unsigned int> S;
 
   std::set<unsigned int> S;
  
   //show the statistics - it should be empty
+
   // Add 10 elements to the set
  Report(S);
+
 
+
  //add 10 integers (0 to 9) to the set
+
 
   for(unsigned int i = 0; i < 10; i++)
 
   for(unsigned int i = 0; i < 10; i++)
 
   {
 
   {
 
     S.insert(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()
+
   // Output all of the elements in the set
{
+
   for(std::set<unsigned int>::iterator it1 = S.begin(); it1 != S.end(); it1++)
   //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());
+
     std::cout << " " << *it1;
 
   }
 
   }
 
 
  //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::cout << "Size: " << S.size() << " Empty? " << S.empty() << std::endl;
   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
+
   return 0;
  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()
+
</source>
{
+
  std::multiset<double> S;
+
  
  Report(S);
+
==CMakeLists.txt==
  for(unsigned int i = 0; i < 10; i++)
+
<source lang="cmake">
  {
+
cmake_minimum_required(VERSION 2.6)
    S.insert(RandomDouble());
+
  }
+
  
  Report(S);
+
Project(Set)
  Output(S);
+
  
  //erase the second element
+
ADD_EXECUTABLE(Set Set.cpp)
  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;
 
}
 
  
 
</source>
 
</source>

Revision as of 10:21, 30 January 2011

Set.cpp

#include <iostream>
#include <set>
#include <vector>
#include <cstdlib>
 
#include <algorithm>
 
int main(int argc, char* argv[])
{
  // Create a set
  std::set<unsigned int> S;
 
  // Add 10 elements to the set
  for(unsigned int i = 0; i < 10; i++)
  {
    S.insert(i);
  }
 
  // Output all of the elements in the set
  for(std::set<unsigned int>::iterator it1 = S.begin(); it1 != S.end(); it1++)
  {
    std::cout << " " << *it1;
  }
 
  std::cout << "Size: " << S.size() << " Empty? " << S.empty() << std::endl;
 
  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
Project(Set)
 
ADD_EXECUTABLE(Set Set.cpp)