Using set

set is a associative container. It is implemented as binary search tree. set allows you to add and retrieve values from the container quickly. In default, values stored in the set container are sorted in ascending order. If you want to sort the values in descending order, you need to use a comparator function object. The function comparator object less<T> allows you to sort the values in the container in ascending order. In contrast, the comparator function object greater<T> allows you to sort the values in descending order. set stores values uniquely. It does not allow duplicate values to be inserted in the set container. If you want to allow duplicates, you will use multiset class instead. set class has the following constructors:

-explicit set(const _Compare& __comp,  const allocator_type& __a) creates an empty set object.
-set(_InputIterator __first, _InputIterator __last) creates a set object consisting of copies of the elements from first to last.
-set(const set<_Key,_Compare,_Alloc>& __x) creates a set object that contains values copied from another set object.

The frequently used member functions of the set class are insert(), erase(), begin(), end(), size(), and find(). The insert function allows you to insert a new value to the set container. The erase() function can be used to remove a specific value from the container. The begin() reads the first item and the end() refers to the next position after the end of the container. The size() function return the capacity of the container. The find() function can be used to search for a specific value in the container.


#include <cstdlib>
#include <iostream>
#include<set>
#include <list>
#include <iterator>
using namespace std;

//function predicate used to specify sort order
bool com(double a,double b){
   if(a>=b) return true;
   else return false;

}
//function predicate used to match items less than 50
bool lessThan50(int a){
             if(a<50) return true;
             else return false;
   }
  

int main(int argc, char *argv[])
{
   //create an empty set container with descending sort order
   set<double,greater<double > > doubleSet;
   //insert values to the container
   doubleSet.insert(12.8);
   doubleSet.insert(10.8);
   doubleSet.insert(50.5);
   doubleSet.insert(11.4);
   doubleSet.insert(110.8);
   doubleSet.insert(510.9);
   //display all values in the set
   set<double>::iterator it=doubleSet.begin();
   while(it!=doubleSet.end()){
    cout<<*it<<" ";
    it++;
    }
   cout<<"\n";
   cout<<"Number of values in the set:"<<doubleSet.size()<<endl;
   //delete 11.4 from the container
   doubleSet.erase(11.4);
  //find 110.8 in the container
   set<double>::iterator fit=doubleSet.find(110.8);
   if(fit!=doubleSet.end())
     cout<<"110.8 is found.\n";
   else
     cout<<"110.8 is not found.\n";

   system("PAUSE");
   return EXIT_SUCCESS;
}

Using-set

Posted by: Dara | post date: 02-06-2014 | Subject: C++ Programming Language

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.