Updated April 15, 2023
Introduction to C++ Multiset Functions
Multisets in C++ are the special type of containers which store elements with a specific order where more than two elements or multiple elements can possess some equivalent values. These values present within the multiset identifies the elements with its key value and pair value and they cannot be modified at any point of time once they are inserted within the multiset but one condition prevails which can get executed is that additional elements can be inserted and retrieved at any time. Some internal conditions are applied where these values within the element follows kind of ordering.
Top Functions of C++ Multiset
Below are the function and its Example of c++ multiset:
1. # find(const g)
It searches for the iterator passed with the argument; in case it is found in the multiset it will return the iterator else it returns the iterator at the end.
Example: This program illustrates the find(const g) function in the multiset.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset<int> k;
k.insert(6);
k.insert(7);
k.insert(3);
k.insert(2);
cout << "The _elements_ within_the _set_ are: ";
for (auto pr_ = k.begin(); pr_ != k.end(); pr_++)
cout << *pr_ << " ";
auto psn = k.find(6);
cout << "\n Set_of_elements after 7 are:";
for (auto pr_ = psn; pr_ != k.end(); pr_++)
cout << *pr_ << " ";
return 0;
}
Output:
2. begin()
The iterator present gets returned to the first element of the multiset array.
Example: This program illustrates the begin() function of the multiset array.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = {10, 16, 19, 13, 11};
multiset<int> o(arr, arr + 10);
cout << "first_element: " << *(o.begin()) << endl;
for (auto pl = o.begin(); pl != o.end(); pl++)
cout << *pl << " ";
return 0;
}
Output:
3. end()
Iterator gets returned when the last element in the multiset gets followed by the previous element.
Example: This program illustrates the end() function in the multiset.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 11, 7, 13, 12, 9, 2, 17, 13 };
multiset<int> b(arr, arr + 5);
for (auto kl = b.begin(); kl != b.end(); kl++)
cout << *kl << " ";
return 0;
}
Output:
4. size()
The size function returns the total number of elements present in the multiset.
Example: This program illustrates the size function of the multiset.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset<int> n;
n.insert(10);
n.insert(13);
cout << "Total_multiset_size: " << n.size();
n.insert(14);
n.insert(21);
cout << "\nTotal_multiset_size: " << n.size();
n.insert(32);
cout << "\nTotal_multiset_size: " << n.size();
return 0;
}
Output:
5. max_size()
It returns the maximum number of elements which a multiset can contain.
Example: This program illustrates the max_size() function.
Code:
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
unordered_multiset<int> jun;
unsigned int mx_elemnt;
mx_elemnt = jun.max_size();
cout << "No_Of-Elements "
<< "multiset can-consider_ "
<< mx_elemnt << endl;
return 0;
}
Output:
6. key_comp()
object returns the elements from the multiset in an order.
Example: This program illustrates the key_comp() function.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset<int> j;
multiset<int>::key_compare
comp
= j.key_comp();
j.insert(11);
j.insert(12);
j.insert(15);
j.insert(36);
cout << "Multiset_Elements\n";
int high_est = *j.rbegin();
multiset<int>::iterator tr = j.begin();
do {
cout << " " << *tr;
} while (comp(*tr++, high_est));
return 0;
}
Output:
7. empty()
Checks and returns for the empty multiset status.
Example: This program illustrates the empty() function.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 23, 12, 18, 6, 19, 8, 30, 10, 16 };
multiset<int> s(arr, arr + 8);
if (!s.empty())
cout << "Verify for multiset_not_Empty";
else
cout << "Verify for multiset_is_Empty";
return 0;
}
Output:
8. erase(const g)
This function erases the constant g passed as part of the argument.
Example: This program illustrates the erase(const g) function.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset<int> mul_ti_set;
multiset<int>::iterator ms_itr;
for (int i = 1; i < 10; i++) {
mul_ti_set.insert(i);
}
cout << "Actual_multiset: ";
for (ms_itr = mul_ti_set.begin();
ms_itr != mul_ti_set.end();
++ms_itr)
cout
<< ' ' << *ms_itr;
cout << '\n';
ms_itr = mul_ti_set.begin();
ms_itr++;
mul_ti_set.erase(ms_itr);
cout << "Transformed_multiset: ";
for (ms_itr = mul_ti_set.begin();
ms_itr != mul_ti_set.end();
++ms_itr)
cout << ' ' << *ms_itr;
cout << '\n';
return 0;
}
Output:
9. pair insert(const g)
This function adds a new element in the multiset.
Example: This program illustrates the function pair insert(const g).
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset<int> k;
k.insert(2);
k.insert(8);
k.insert(6);
k.insert(4);
k.insert(1);
cout << "Multiset_Elements are: ";
for (auto sd = k.begin(); sd != k.end(); sd++)
cout << *sd << " ";
return 0;
}
Output:
10. iterator insert (iterator position, const g)
The position pointed by the iterator gets added as a new element in the multiset.
Example: This program illustrates the iterator insert (iterator position, const g).
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset<int> d;
d.insert(8);
d.insert(4);
d.insert(6);
d.insert(0);
d.insert(1);
cout << "The elements in multiset are: ";
for (auto lp = d.begin(); lp != d.end(); lp++)
cout << *lp << " ";
return 0;
}
Output:
11. erase(iterator position)
The position pointer by the iterator removes the element present in the position.
Example: This program illustrates the erase(iterator position).
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset<int> mul_ti_set;
multiset<int>::iterator ms_itr;
for (int d = 1; d < 6; d++) {
mul_ti_set.insert(d);
}
cout << "Actual_Multiset: ";
for (ms_itr = mul_ti_set.begin();
ms_itr != mul_ti_set.end();
++ms_itr)
cout
<< ' ' << *ms_itr;
cout << '\n';
ms_itr = mul_ti_set.begin();
ms_itr++;
mul_ti_set.erase(ms_itr);
cout << "Modified multiset: ";
for (ms_itr = mul_ti_set.begin();
ms_itr != mul_ti_set.end();
++ms_itr)
cout << ' ' << *ms_itr;
cout << '\n';
return 0;
}
Output:
12. clear()
It simply removes all the elements present within the multiset.
Example: This program illustrates the clear() function.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = {4 , 12, 16, 19, 20 };
multiset<int> o(arr, arr + 5);
cout << "Multiset_Elements: ";
for (auto uo = o.begin(); uo != o.end(); uo++)
cout << *uo << " ";
cout << "\nAfter_clear_size is: ";
o.clear();
cout << o.size();
return 0;
}
Output:
13. upper_bound(const g)
This function works in a way where the element added either goes to the first element or equivalently goes to the end of the function.
Example: This program represents the upper_bound(const g) function.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset<int> o;
o.insert(1);
o.insert(3);
o.insert(3);
o.insert(5);
o.insert(4);
cout << "multiset_elements: ";
for (auto pq = o.begin(); pq != o.end(); pq++)
cout << *pq << " ";
auto pq = o.upper_bound(3);
cout << "\nThe_upper_bound of key 3 is ";
cout << (*pq) << endl;
pq = o.upper_bound(2);
cout << "The_upper_bound of key 2 is ";
cout << (*pq) << endl;
return 0;
}
Output:
14. lower_bound(const g)
It returns an iterator either to the first of the element or the last of the element equivalently.
Example: This program illustrates the lower_bound function in the multiset.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset<int> i;
i.insert(1);
i.insert(2);
i.insert(3);
cout << "Multiset_elements : ";
for (auto op = i.begin(); op != i.end(); op++)
cout << *op << " ";
auto op = i.lower_bound(2);
cout << "\nThe lower_bnd_of_ny_key : ";
cout << (*op) << endl;
}
Output:
15. count(const g)
Returns all the elements which gets matched with the passed element such as ‘g’.
Example: This program illustrates count(const g).
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 15, 10, 15, 11, 10, 18, 18, 20, 20 };
multiset<int> o(arr, arr + 9);
cout << "12 occrs " << o.count(10)
<< " 12th times of container occuring";
return 0;
}
Output:
16. multiset::emplace()
This function Is used to insert a new elements into the container.
Example: This program illustrates multiset::emplace().
Code:
#include <iostream>
#include <set>
using namespace std;
int main()
{
multiset<int> m_mul_ti_set{};
m_mul_ti_set.emplace(20);
m_mul_ti_set.emplace(46);
m_mul_ti_set.emplace(15);
m_mul_ti_set.emplace(10);
m_mul_ti_set.emplace(2);
m_mul_ti_set.emplace(87);
for (auto po = m_mul_ti_set.begin();
po != m_mul_ti_set.end(); ++po)
cout << ' ' << *po;
return 0;
}
Output:
17. multiset::operator=–
All the existing contents within the container gets replaced with the new contents.
Example: This program illustrates the multiset::operator=–
Code:
#include <iostream>
#include <set>
using namespace std;
int main()
{
multiset<int> my_multi_st1{ 1, 7, 4, 9, 0};
multiset<int> my_multi_st2{ 3, 4 };
my_multi_st1 = my_multi_st2;
cout << " = ";
for (auto ho = my_multi_st1.begin();
ho != my_multi_st2.end(); ++ho)
cout << ' ' << *ho;
return 0;
}
Output:
18. multiset::swap()
Set of elements with same size but different elements can be swapped using the function with two multisets into consideration.
Example: This program illustrates the multiset::swap() function.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset<int> multi_set_ini1{ 8, 7, 5, 9 };
multiset<int> multi_set_ini2{ 2, 4, 7, 6 };
multi_set_ini1.swap(multi_set_ini2);
cout << "multi_set_1 = ";
for (auto ui = multi_set_ini1.begin();
ui != multi_set_ini1.end(); ++ui)
cout << ' ' << *ui;
cout << endl
<< "multi_set_2 = ";
for (auto ui = multi_set_ini2.begin();
ui != multi_set_ini2.end(); ++ui)
cout << ' ' << *ui;
return 0;
}
Output:
19. multiset equal_range()
The iterator returns a pair where the pair refers to the range of all the elements included within the container equivalent to the passed parameter.
Example: This program illustrates the multiset equal_range().
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset<int> q;
q.insert(1);
q.insert(6);
q.insert(2);
cout << "The mul_ti_set elements are: ";
for (auto op = q.begin(); op != q.end(); op++)
cout << *op << " ";
auto op = q.equal_range(3);
cout << "\nThe lower_bound_of 3 is " << *op.first;
return 0;
}
Output:
20. multiset::crend()
This function returns a constant reverse iterator pointing towards the last element in the multiset.
Example: This program illustrates the multiset::crend() function.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 13, 10, 5, 11, 8, 12, 10, 11 };
multiset<int> q(arr, arr + 8);
for (auto mi = q.crbegin(); mi != q.crend(); mi++)
cout << *mi << " ";
return 0;
}
Output:
21. multiset::emplace_hint()
It inserts a new element in the multiset.
Example: This program illustrates the multiset::emplace_hint() function.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset<int> o;
auto ui = o.emplace_hint(o.begin(), 2);
ui = o.emplace_hint(ui, 3);
o.emplace_hint(ui, 5);
o.emplace_hint(ui, 4);
for (auto ui = o.begin(); ui != o.end(); ui++)
cout << *ui << " ";
return 0;
}
Output:
22. multiset::crbegin()
It returns a constant reverse iterator pointing towards last element in the container.
Example: This program illustrates the multiset::crbegin() function.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 18, 6, 11, 14, 10 };
multiset<int> e(arr, arr + 5);
cout << " Last_Element: " << *(e.crbegin()) << endl;
for (auto pr = e.crbegin(); pr != e.crend(); pr++)
cout << *pr << " ";
return 0;
}
Output:
23. multiset::rbegin()
Returns a reverse iterator which is pointing towards the last element in the multiset container.
Example: This program illustrates the multiset::rbegin() function.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 10, 11, 5, 8, 10, 9 };
multiset<int> y(arr, arr + 6);
multiset<int>::reverse_iterator rv_itr;
for (rv_itr = y.rbegin(); rv_itr != y.rend(); rv_itr++)
cout << *rv_itr << " ";
cout << "\nlast_element_multi_set " << *(y.rbegin());
return 0;
}
Output:
24. multiset::cend()
This function returns the constant reverse iterator pointing towards the position which have passed the last element in past.
Example: This program illustrates the cend function of the multiset.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 13,10,9,8,76,13,25,20 };
multiset<int> o(arr, arr + 4);
for (auto rs = o.cbegin(); rs != o.cend(); rs++)
cout << *rs << " ";
return 0;
}
Output:
25. multiset::cbegin()
This function returns a constant element pointing to the first element of the array.
Example: This program illustrates the multiset::cbegin().
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 12, 10, 9, 18, 6 };
multiset<int> j(arr, arr + 2);
cout << "First_elements : " << *(j.cbegin()) << endl;
return 0;
}
Output:
26. multiset::rend()
This returns the reverse iterator pointing just before the element which is placed before the first element in the multiset container.
Example: This program illustrates the multiset::rend().
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 10, 13, 12, 11, 10, 9 };
multiset<int> m(arr, arr + 3);
multiset<int>::reverse_iterator rmi;
for (rmi = m.rbegin(); rmi != m.rend(); rmi++)
cout << *rmi << " ";
return 0;
}
Output:
27. multiset::get_allocator()
The object associated with the multiset returns a copy of the allocator.
Example: This program illustrates the multiset::get_allocator().
Code:
#include <iostream>
#include <set>
using namespace std;
int main()
{
multiset<int> m_multi_set;
int* k;
unsigned int u;
k = m_multi_set
.get_allocator()
.allocate(5);
k[0] = 10;
k[1] = 10;
k[2] = 20;
cout << "Allocated_array_contains: ";
for (u = 0; u < 5; u++) {
cout << k[u] << " ";
}
cout << endl;
m_multi_set.get_allocator().deallocate(k, 5);
return 0;
}
Output:
Conclusion
C++ multiset is a standard library that contains build-in functionalities that is quite useful for the programmers in terms of implementation and gives the code base a robust and flexible usage. It makes the language versatile and helps the programmers to use different functionalities with ease and simplicity.
Recommended Article
This is a guide to the C++ Multiset. Here we discuss the Introduction of C++ Multiset Functions and its Syntax with Examples along with Code Implementation and Output. you can also go through our suggested articles to learn more –