Saturday, May 15, 2010

STL SET

STL SET

  • Sets are a kind of associative containers that stores unique elements, and in which the elements themselves are the keys.
  • Associative containers are containers especially designed to be efficient accessing its elements by their key (unlike sequence containers, which are more efficient accessing elements by their relative or absolute position). 
  • The elements in a set are always sorted from lower to higher following a specific strict weak ordering criterion set on container construction.
  • Sets are typically implemented as binary search trees.
  • This container class supports bidirectional iterators.
EXAMPLE
// set::find
# include < iostream >
# include < set >
using namespace std;

int main ()
{
  set< int > myset;
  set< int >::iterator it;

  // set some initial values:
  for (int i=1; i<=5; i++) myset.insert(i*10);    // set: 10 20 30 40 50

  it=myset.find(20);
  myset.erase (it);
  myset.erase (myset.find(40));

  cout << "myset contains:";
  for (it=myset.begin(); it!=myset.end(); it++)
    cout << " " << *it;
  cout << endl;

  return 0;
}

Output:
myset contains: 10 30 50

No comments:

Post a Comment