STL 5: detailed usage of set/Multiset

Source: Internet
Author: User
Document directory
  • 1) constructor and destructor
  • 2) size, empty function judgment
  • 3) add and delete Functions
  • 4) traversal Function
  • 5) operation functions
Before using the set or Multiset set, you must add the header file <set>

Set and Multiset are both collection classes. The difference is that duplicate elements are not allowed in the Set, and repeated elements are allowed in Multiset.

Sets and Multiset internally implement a balanced binary tree


1. Common functions 1) constructor and destructor

Set C: creates an empty set that does not contain any elements.

Set C (OP): Uses OP as the sorting criterion to generate an empty set

Set C1 (C2): copy the elements in C2 to C1.

Set C (const value_type * First, const value_type * Last): Copies the elements between [first, last) to form a new set.

Set C (const value_type * First, const value_type * Last, OP): Uses OP as the sorting criterion and copies the elements between [first, last) to form a new set.

C .~ Set () destroys all elements and releases memory

Multiset MC: creates an empty set that does not contain any elements.

Multiset MC (OP): Uses OP as the sorting criterion to generate an empty set

Multiset C1 (C2): copy the elements in C2 to C1.

Multiset C (const value_type * First, const value_type * Last): Copies the elements between [first, last) to form a new set.

Multiset C (const value_type * First, const value_type * Last, OP): Uses OP as the sorting criterion and copies the elements between [first, last) to form a new set.

C .~ Set () destroys all elements and releases memory

// constructing sets#include <iostream>#include <set>bool fncomp (int lhs, int rhs) {return lhs<rhs;}struct classcomp {  bool operator() (const int& lhs, const int& rhs) const  {return lhs<rhs;}};int main (){  std::set<int> first;                           // empty set of ints  int myints[]= {10,20,30,40,50};  std::set<int> second (myints,myints+5);        // range  std::set<int> third (second);                  // a copy of second  std::set<int> fourth (second.begin(), second.end());  // iterator ctor.  std::set<int,classcomp> fifth;                 // class as Compare  bool(*fn_pt)(int,int) = fncomp;  std::set<int,bool(*)(int,int)> sixth (fn_pt);  // function pointer as Compare  return 0;}

2) size, empty function judgment

Int size () const: returns the number of elements in the container.
Bool empty () const: determines whether the container is empty. If true is returned, the container is empty.

3) add and delete Functions

Pair <iterator, bool> insert (x): insert element x

Iterator insert (iterator it, X): insert element x in it of the iterator

Void insert (const value_type * First, const value_type * Last): Insert elements between [first, last)

Iterator erase (iterator it): deletes the element at it in the iterator pointer.

Iterator erase (iterator first, iterator last): deletes elements between [first, last)

Size_type erase (const key & Key): deletes an element with an element value equal to the key.

#include <iostream>#include <set>int main (){  std::set<int> myset;  std::set<int>::iterator it;  std::pair<std::set<int>::iterator,bool> ret;  // set some initial values:  for (int i=1; i<=5; ++i) myset.insert(i*10);    // set: 10 20 30 40 50  ret = myset.insert(20);               // no new element inserted  if (ret.second==false) it=ret.first;  // "it" now points to element 20  myset.insert (it,25);                 // max efficiency inserting  myset.insert (it,24);                 // max efficiency inserting  myset.insert (it,26);                 // no max efficiency inserting  int myints[]= {5,10,15};              // 10 already in set, not inserted  myset.insert (myints,myints+3);  std::cout << "myset contains:";  for (it=myset.begin(); it!=myset.end(); ++it)    std::cout << ' ' << *it;  std::cout << '\n';  return 0;}

#include <iostream>#include <set>int main (){  std::set<int> myset;  std::set<int>::iterator it;  // insert some values:  for (int i=1; i<10; i++) myset.insert(i*10);  // 10 20 30 40 50 60 70 80 90  it = myset.begin();  ++it;                                         // "it" points now to 20  myset.erase (it);  myset.erase (40);  it = myset.find (60);  myset.erase (it, myset.end());  std::cout << "myset contains:";  for (it=myset.begin(); it!=myset.end(); ++it)    std::cout << ' ' << *it;  std::cout << '\n';  return 0;}

4) traversal Function

Iterator begin (): returns the iterator pointer to the first element.

Iterator end (): returns the iterator pointer to the end element.
Reverse_iterator rbegin (): returns the reverse iterator pointer of the last element.
Reverse_iterator rend (): returns the iterator pointer at the previous position of the first element.

#include <iostream>#include <set>int main (){  int myints[] = {75,23,65,42,13};  std::set<int> myset (myints,myints+5);  std::cout << "myset contains:";  for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)    std::cout << ' ' << *it;  std::cout << '\n';  return 0;}

5) operation functions

Const_iterator lower_bound (const key & Key): returns the iterator pointer larger than or equal to the key in the container.

Const_iterator upper_bound (const key & Key): returns the iterator pointer larger than the key in the container.

Int count (const key & Key) const: returns the number of elements in the container whose elements are equal to the key.
Pair <const_iterator, const_iterator> pai_range (const key & Key) const: return the iteration pointer of the element value in the container equal to the key [first, last)
Const_iterator find (const key & Key) const: search function, returns the iterator pointer with the element value equal to the key
Void swap (set & S): Exchange set Elements
Void swap (Multiset & S): swap multiple set Elements

#include <iostream>#include <set>int main (){  std::set<int> myset;  std::set<int>::iterator itlow,itup;  for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90  itlow=myset.lower_bound (30);                //       ^  itup=myset.upper_bound (60);                 //                   ^  myset.erase(itlow,itup);                     // 10 20 70 80 90  std::cout << "myset contains:";  for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)    std::cout << ' ' << *it;  std::cout << '\n';  return 0;}
#include "stdafx.h"#include <iostream>#include <set>using namespace std;int main (){set<int> myset;for (int i=1; i<=5; i++) myset.insert(i*10);   // myset: 10 20 30 40 50pair<set<int>::const_iterator,set<int>::const_iterator> ret;ret = myset.equal_range(30);cout << "the lower bound points to: " << *ret.first << '\n';cout << "the upper bound points to: " << *ret.second << '\n';return 0;}

#include "stdafx.h"#include <iostream>#include <set>using namespace std;int main (){int myints[]={12,75,10,32,20,25};set<int> first (myints,myints+3);     // 10,12,75set<int> second (myints+3,myints+6);  // 20,25,32first.swap(second);cout << "first contains:";for (set<int>::iterator it=first.begin(); it!=first.end(); ++it)cout << ' ' << *it;cout << '\n';cout << "second contains:";for (set<int>::iterator it=second.begin(); it!=second.end(); ++it)cout << ' ' << *it;cout << '\n';return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.