In this instructional exercise you will find out about STL Multiset holder in C++, for example, std:: multiset and all capacities material on it.
A multiset is an acquainted holder. Same like set this likewise pursue some particular request to store components.
Be that as it may, just contrast is these multisets permit copy esteems. Furthermore, some greater similitude to sets to multisets are the two has the property that, esteem put away and relating key both are same.
Components in multiset are consistent. We are incapable to change after addition of the component.
In the event that we need to refresh component, at that point we ought to erase that component and again embed with the refreshed component.
The components in the multiset are constantly arranged.
C++ STL Multiset Container – std::multiset
Iterators take a shot at multiset:
start(): returns iterator to the start.
end(): returns iterator as far as possible of the rundown.
rbegin(): returns invert iterator to switch starting.
tear(): returns turn around iterator to switch end.
cbegin(): Returns steady iterator to starting.
cend(): Returns steady iterator to end.
To work with multiset we have to incorporate set library.
#include<set>
How about we see a few tasks on multiset:
1) insert(element): This activity embeds a new component in the multiset. Here whatever the request we embed, those components consistently embedded in an arranged request to multiset.
2) size(): This profits the number which speaks to a current number of components in the multiset.
3) max_size(): This capacity returns what number of components in most extreme we can embed into multiset.
4) vacant(): This is a Boolean activity. Returns 1 is multiset is unfilled. Generally returns 0.
5) multiset newMultiset (oldMultiset.beginIterator(), oldMultiset.EndIterator()): This capacity duplicates all components of existing multiset from given iterator position to given iterator end position, to another multiset
For all the more unmistakably comprehension of above capacities/tasks, see the beneath program.
#include <iostream>
#include <set>
#include <iterator>
using namespace std;
int main()
{
multiset <int> mset; // declaring a multiset
multiset <int> ::iterator it;
for (int i=0; i<5; i++) {
mset.insert (i+1);
}
mset.insert (5); // here 5 is inserting again in the multiset. This is allowed here unlike set
// showing elements in multiset
cout << "The elements in the multiset are" << endl;
for(it= mset.begin(); it!= mset.end(); it++) {
cout << *it << " ";
}
cout << endl;
// size of multiset
cout << "Size of the multiset is " << mset.size() << endl;
// maximum size
cout << "Maximum size of the multiset is " << mset.max_size() << endl;
bool chk = mset.empty();
if (chk==1) {
cout << "Multiset is empty" << endl;
}
else{
cout << "Multiset is not empty" << endl;
}
// assigning elements of the mset to other multiset
multiset <int> mset2(mset.begin(), mset.end());
// showing elements of the copied multiset
cout << "Elements of new multiset after copying from mset" << endl;
for (it = mset2.begin(); it != mset2.end(); it++) {
cout << *it << " ";
}
cout << endl;
return 0;
}
Output
The elements in the multiset are
1 2 3 4 5 5
Size of the multiset is 6
Maximum size of the multiset is 461168601842738790
Multiset is not empty
Elements of new multiset after copying from mset
1 2 3 4 5 5
Some other altering tasks on multiset are:
1) erase(element): It expels the predetermined component from multiset. We can likewise erase the scope of components by utilizing iterators.
2) swap(): If there are two multisets m1 and m2. Both need not be the equivalent size. Swap activity swaps all components of m2 to m1. And furthermore from m1 to m2.
3) clear(): This expels all components from multiset. So results in multiset size 0.
Model program to comprehend above capacities:
#include <iostream>
#include <set>
#include <iterator>
using namespace std;
int main()
{
multiset <int> mset1;
multiset <int> mset2;
multiset <int> ::iterator it;
for (int i=0; i<5; i++) {
mset1.insert (i+1);
}
// erase operation
mset1.erase(5);
cout << "Elements after removing 5" << endl;
for (it=mset1.begin(); it!=mset1.end(); it++) {
cout << *it << " ";
}
cout << endl;
// inserting elements to mset2
for (int i=0;i<4;i++) {
mset2.insert(i+100);
}
cout << "elements of multiset2 are" << endl;
for (it=mset2.begin(); it!= mset2.end(); it++) {
cout << *it << " ";
}
cout << endl;
cout << "performing swap operation on entire sets..." << endl;
mset1.swap(mset2);
cout << "Elements of multiset1 after swap operation" << endl;
for (it=mset1.begin(); it!=mset1.end(); it++) {
cout << *it << " ";
}
cout << endl;
cout << "Elements of multiset2 after swap operation" << endl;
for (it=mset2.begin(); it!=mset2.end(); it++) {
cout << *it << " ";
}
cout << endl;
cout << "Applying clear operation on multiset2..." << endl;
mset2.clear();
bool chk= mset2.empty();
if (chk==1) {
cout << "Multiset2 is empty" << endl;
}
else {
cout << "Multiset2 is not empty" << endl;
}
return 0;
}
Output
Elements after removing 5
1 2 3 4
elements of multiset2 are
100 101 102 103
performing swap operation on entire sets…
Elements of multiset1 after swap operation
100 101 102 103
Elements of multiset2 after swap operation
1 2 3 4
Applying clear operation on multiset2…
Multiset2 is empty
Some more tasks are:
1) discover (component): This activity used to locate the specific component structure multiset. Subsequent to discovering utilizing iterator we can erase that component or we can print.
2) tally (component): This activity used to check what number of a number of times component happens in the multiset.
3) lower_bound (component): This profits iterator at the determined component position.
4) upper_bound (component): This profits iterator at the determined component position.
Utilizing upper bound and lower bound we can perform activities on the scope of components between them.
Model program to clarify the above activities:
#include <iostream>
#include <set>
#include <iterator>
using namespace std;
int main()
{
multiset <int> mset1;
multiset <int> ::iterator it;
for (int i=0; i<5; i++) {
mset1.insert (i+1);
}
it= mset1.find(3); // finding element 3
cout << "Element found is " << *it << endl;
// for count operation I am adding some more elements of 2
mset1.insert (2);
mset1.insert (2);
cout << "In multiset element 2 occured " << mset1.count(2) << " times" << endl;
multiset <int> :: iterator lob, upb;
lob= mset1.lower_bound (2);
upb= mset1.upper_bound (4);
cout << "Removing elements form lower bound to upper bound..." << endl;
mset1.erase (lob,upb);
cout << "After removing elements are" << endl;
for (it=mset1.begin(); it!=mset1.end(); it++) {
cout << *it << " ";
}
cout << endl;
return 0;
}
Output
Element found is 3
In multiset element 2 occured 3 times
Removing elements form lower bound to upper bound…
After removing elements are
1 5
Remark beneath in the event that you have any inquiries identified with above C++ stl multiset or std:: multiset instructional exercise.