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>…')
 
 
(One intermediate revision by the same user not shown)
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 TestSet();
void TestMultiSet();
+
void TestSetIsInserted();
 
+
void TestFind(); //look for an element in a set
+
  
 
int main(int argc, char* argv[])
 
int main(int argc, char* argv[])
 
{
 
{
 
   //TestSet();
 
   //TestSet();
   //TestMultiSet();
+
   TestSetIsInserted();
 +
 
 
    
 
    
  TestFind();
 
 
   return 0;
 
   return 0;
 
}
 
}
  
void TestFind()
+
void TestSet()
 
{
 
{
   //create a set
+
   // 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
+
   // Output all of the elements in the set
   Report(S);
+
   for(std::set<unsigned int>::iterator it1 = S.begin(); it1 != S.end(); it1++)
 
+
  //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;
+
     std::cout << " " << *it1;
 
   }
 
   }
  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()
+
   std::cout << "Size: " << S.size() << " Empty? " << S.empty() << std::endl;
{
+
  //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 TestSetIsInserted()
void TestMultiSet()
+
 
{
 
{
   std::multiset<double> S;
+
  // Create a set
 +
   std::set<unsigned int> S;
  
   Report(S);
+
   typedef std::pair<std::set<unsigned int>::iterator,bool> ReturnType;
  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);
+
   // Add 10 elements to the set
  Output(S);
+
 
+
  S.clear();
+
 
+
  Report(S);
+
 
+
}
+
 
+
 
+
void TestHeightSet()
+
{
+
  std::set<Height> S;
+
 
+
 
   for(unsigned int i = 0; i < 10; i++)
 
   for(unsigned int i = 0; i < 10; i++)
  {
+
    {
     S.insert(Height(RandomDouble()));
+
     ReturnType inserted = S.insert(i);
  }
+
    if(inserted.second)
        
+
      {
 +
      std::cout << "Inserted " << i << std::endl;
 +
      }
 +
    else
 +
      {
 +
      std::cout << "Did not insert " << i << std::endl;
 +
      }
 +
    }
 +
   
 +
    ReturnType inserted = S.insert(3);
 +
    if(inserted.second)
 +
      {
 +
      std::cout << "Inserted " << 3 << std::endl;
 +
      }
 +
    else
 +
       {
 +
      std::cout << "Did not insert " << 3 << std::endl;
 +
      }
 
}
 
}
  
double RandomDouble()
+
</source>
{
+
  //produce a random double between 0 and 1
+
  return drand48();
+
}
+
  
void Output(std::set<double> &S)
+
==CMakeLists.txt==
{
+
<source lang="cmake">
  //for(list<double>::iterator it1 = L.begin(); it1 != L.end(); ++it1)
+
cmake_minimum_required(VERSION 2.6)
  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)
+
Project(Set)
{
+
  //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)
+
ADD_EXECUTABLE(Set Set.cpp)
{
+
  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>

Latest revision as of 17:15, 14 February 2011

Set.cpp

#include <iostream>
#include <set>
#include <vector>
#include <cstdlib>
 
#include <algorithm>
 
void TestSet();
void TestSetIsInserted();
 
int main(int argc, char* argv[])
{
  //TestSet();
  TestSetIsInserted();
 
 
  return 0;
}
 
void TestSet()
{
  // 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;
 
 
}
 
void TestSetIsInserted()
{
  // Create a set
  std::set<unsigned int> S;
 
  typedef std::pair<std::set<unsigned int>::iterator,bool> ReturnType;
 
  // Add 10 elements to the set
  for(unsigned int i = 0; i < 10; i++)
    {
    ReturnType inserted = S.insert(i);
    if(inserted.second)
      {
      std::cout << "Inserted " << i << std::endl;
      }
    else
      {
      std::cout << "Did not insert " << i << std::endl;
      }
    }
 
    ReturnType inserted = S.insert(3);
    if(inserted.second)
      {
      std::cout << "Inserted " << 3 << std::endl;
      }
    else
      {
      std::cout << "Did not insert " << 3 << std::endl;
      }
}

CMakeLists.txt

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