Friday 6 July 2018

multiset

 Multiset
(i) Stores elements in sorted order.
(ii) It allows storage of multiple elements.
(iii) We can erase more than 1 element by giving start iterator and end iterator.
Note:- All other properties similar to set.
// CPP program to demonstrate insert and
// delete in set
#include
using namespace std;
int main()
{
    // multiset declare
    multiset<int> s;
 
    // Elements added to set
    s.insert(12);
    s.insert(10);
    s.insert(2);
    s.insert(10); // duplicate added
    s.insert(90);
    s.insert(85);
    s.insert(45);
 
    // Iterator declared to traverse
    // set elements
    multiset<int>::iterator it, it1, it2;
    cout << "Multiset elements after sort\n";
    for (it = s.begin(); it != s.end(); it++)
        cout << *it << ' ';   
    cout << '\n';
 
    it1 = s.find(10);
    it2 = s.find(90);
 
    // elements from 10 to elements before 90
    // erased
    s.erase(it1, it2);
 
    cout << "Multiset Elements after erase:\n";
    for (it = s.begin(); it != s.end(); it++)
        cout << *it << ' ';   
 
    return 0;
}
OUTPUT:
Multiset elements after sort
2 10 10 12 45 85 90
Multiset Elements after erase:
2 90

No comments:

Post a Comment