Multiset in C++ Standard Template Library (STL)
Multisets are a type of associative containers similar to set, with an exception that multiple elements can have same values. 元素可以由相同的值。
Some Basic Functions associated with multiset:
begin() – Returns an iterator to the first element in the multiset
end() – Returns an iterator to the theoretical element that follows last element in the multiset
size() – Returns the number of elements in the multiset
max_size() – Returns the maximum number of elements that the multiset can hold
empty() – Returns whether the multiset is empty
#include <iostream>
#include <set>
#include <iterator>
using namespace std;
int main()
{
// empty multiset container
multiset <int, greater <int> > gquiz1;
// insert elements in random order
gquiz1.insert(40);
gquiz1.insert(30);
gquiz1.insert(60);
gquiz1.insert(20);
gquiz1.insert(50);
gquiz1.insert(50); // 50 will be added again to the multiset unlike set
gquiz1.insert(10);
// printing multiset gquiz1
multiset <int, greater <int> > :: iterator itr;
cout << "\nThe multiset gquiz1 is : ";
for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr)
{
cout << '\t' << *itr;
}
cout << endl;
// assigning the elements from gquiz1 to gquiz2
multiset <int> gquiz2(gquiz1.begin(), gquiz1.end());
// print all elements of the multiset gquiz2
cout << "\nThe multiset gquiz2 after assign from gquiz1 is : ";
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr)
{
cout << '\t' << *itr;
}
cout << endl;
// remove all elements up to element with value 30 in gquiz2
cout << "\ngquiz2 after removal of elements less than 30 : ";
gquiz2.erase(gquiz2.begin(), gquiz2.find(30));
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr)
{
cout << '\t' << *itr;
}
// remove all elements with value 50 in gquiz2
int num;
num = gquiz2.erase(50);
cout << "\ngquiz2.erase(50) : ";
cout << num << " removed \t" ;
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr)
{
cout << '\t' << *itr;
}
cout << endl;
//lower bound and upper bound for multiset gquiz1
cout << "gquiz1.lower_bound(40) : "
<< *gquiz1.lower_bound(40) << endl;
cout << "gquiz1.upper_bound(40) : "
<< *gquiz1.upper_bound(40) << endl;
//lower bound and upper bound for multiset gquiz2
cout << "gquiz2.lower_bound(40) : "
<< *gquiz2.lower_bound(40) << endl;
cout << "gquiz2.upper_bound(40) : "
<< *gquiz2.upper_bound(40) << endl;
return 0;
}
The output of the above program is :
The multiset gquiz1 is : 60 50 50 40 30 20 10 The multiset gquiz2 after assign from gquiz1 is : 10 20 30 40 50 50 60 gquiz2 after removal of elements less than 30 : 30 40 50 50 60 gquiz2.erase(50) : 2 removed 30 40 60 gquiz1.lower_bound(40) : 40 gquiz1.upper_bound(40) : 30 gquiz2.lower_bound(40) : 40 gquiz2.upper_bound(40) : 60
List of functions of Multiset:
- begin() – Returns an iterator to the first element in the multiset.
- end() – Returns an iterator to the theoretical element that follows last element in the multiset.
- size() – Returns the number of elements in the multiset.
- max_size()– Returns the maximum number of elements that the multiset can hold.
- empty() – Returns whether the multiset is empty.
- pair insert(const g) – Adds a new element ‘g’ to the multiset.
- iterator insert (iterator position,const g) – Adds a new element ‘g’ at the position pointed by iterator.
- erase(iterator position)– Removes the element at the position pointed by the iterator.
- erase(const g)– Removes the value ‘g’ from the multiset.
- clear()– Removes all the elements from the multiset.
- key_comp() / value_comp()– Returns the object that determines how the elements in the multiset are ordered (‘<' by default).
- find(const g)– Returns an iterator to the element ‘g’ in the multiset if found, else returns the iterator to end.
- count(const g)– Returns the number of matches to element ‘g’ in the multiset.
- lower_bound(const g)– Returns an iterator to the first element that is equivalent to ‘g’ or definitely will not go before the element ‘g’ in the multiset.
- upper_bound(const g)– Returns an iterator to the first element that is equivalent to ‘g’ or definitely will go after the element ‘g’ in the multiset.
- multiset::swap()– This function is used to exchange the contents of two multisets but the sets must be of same type, although sizes may differ.
- multiset::operator=– This operator is used to assign new contents to the container by replacing the existing contents.
- multiset::emplace()– This function is used to insert a new element into the multiset container.
- multiset equal_range()– Returns an iterator of pairs. The pair refers to the range that includes all the elements in the container which have a key equivalent to k.
- multiset::emplace_hint() – Inserts a new element in the multiset.
- multiset::rbegin()– Returns a reverse iterator pointing to the last element in the multiset container.
- multiset::rend()– Returns a reverse iterator pointing to the theoretical element right before the first element in the multiset container.
- multiset::cbegin()– Returns a constant iterator pointing to the first element in the container.
- multiset::cend()– Returns a constant iterator pointing to the position past the last element in the container.
- multiset::crbegin()– Returns a constant reverse iterator pointing to the last element in the container.
- multiset::crend()– Returns a constant reverse iterator pointing to the position just before the first element in the container.
- multiset::get_allocator()– Returns a copy of the allocator object associated with the multiset.
multiset begin() and end() function in C++ STL
- The multiset::begin() is a built-in function in C++ STL which returns an iterator pointing to the first element in the multiset container. Since multiset always contains elements in an ordered way, begin() always points to the first element according to the sorting criterion.
Syntax:
iterator multiset_name.begin()
Parameters: The function does not accept any parameters.
Return value: The function returns an iterator pointing to the first element in the container.
Below program illustrate the above-mentioned function:
// CPP program to demonstrate the
// multiset::begin() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 14, 10, 15, 11, 10 };
// initializes the set from an array
multiset<int> s(arr, arr + 5);
// Print the first element
cout << "The first element is: " << *(s.begin()) << endl;
// prints all elements in set
for (auto it = s.begin(); it != s.end(); it++)
cout << *it << " ";
return 0;
}
Output:
The first element is: 10 10 10 11 14 15
- The multiset::end() is a built-in function in C++ STL which returns an iterator pointing to the position past the last element in the container.
Syntax:iterator multiset_name.end()
Parameters: The function does not accept any parameters.
Return value: The function returns an iterator pointing to the position past the last element in the container in the multiset container.
Below program illustrate the above-mentioned function:
// CPP program to demonstrate the
// multiset::end() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 14, 10, 15, 11, 10, 12, 17, 12 };
// initializes the set from an array
multiset<int> s(arr, arr + 8);
// prints all elements in set
for (auto it = s.begin(); it != s.end(); it++)
cout << *it << " ";
return 0;
}
Output:
10 10 11 12 12 14 15 17
multiset size() in C++ STL with Examples
The multiset::size() is a built-in function in C++ STL which returns the number of elements in the multiset container. 返回元素的个数。
Syntax:
multiset_name.size()
Parameters: The function does not accept any parameters.
Return Value: The function returns the number of elements in the multiset container.
Below programs illustrate the above function:
Program 1:
// CPP program to demonstrate the
// multiset::size() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset<int> s;
// Function to insert elements
// in the multiset container
s.insert(10);
s.insert(13);
cout << "The size of multiset: " << s.size();
s.insert(13);
s.insert(25);
cout << "\nThe size of multiset: " << s.size();
s.insert(24);
cout << "\nThe size of multiset: " << s.size();
return 0;
}
Output:
The size of multiset: 2 The size of multiset: 4 The size of multiset: 5
Program 2:
// CPP program to demonstrate the
// multiset::size() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset<int> s;
cout << "The size of multiset: " << s.size();
s.insert(2);
s.insert(3);
cout << "\nThe size of multiset: " << s.size();
s.insert(4);
cout << "\nThe size of multiset: " << s.size();
return 0;
}
Output:
The size of multiset: 0 The size of multiset: 2 The size of multiset: 3
All functions of std::multiset
multiset erase() in C++ STL
Prerequisite : multiset
The multiset::erase() is the STL function in C++ removes the specified element from multiset. 删除特定元素。
There are three versions of this method. These are:
- Syntax:
void erase (iterator position_of_iterator);
Parameters: This method accepts following parameters:
- position_of_iterator: It refers to the position of the specific element to be removed with the help of iterator.
Return value: This method returns the iterator following the removed element.
Below examples illustrate the working of multiset::erase() method:
// C++ program to demonstrate
// multiset::erase() method
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Initialise the multiset
multiset<int> multi_set;
multiset<int>::iterator ms_iterator;
// Add values to the multiset
for (int i = 1; i < 10; i++) {
multi_set.insert(i);
}
cout << "Original multiset: ";
for (ms_iterator = multi_set.begin();
ms_iterator != multi_set.end();
++ms_iterator)
cout
<< ' ' << *ms_iterator;
cout << '\n';
ms_iterator = multi_set.begin();
ms_iterator++;
// Passing the iterator for the position
// at which the value is to be erased
multi_set.erase(ms_iterator);
cout << "Modified multiset: ";
for (ms_iterator = multi_set.begin();
ms_iterator != multi_set.end();
++ms_iterator)
cout << ' ' << *ms_iterator;
cout << '\n';
return 0;
}
Output:
Original multiset: 1 2 3 4 5 6 7 8 9 Modified multiset: 1 3 4 5 6 7 8 9
- Syntax:
size_type erase (const value_type& contant_value);
Parameters: This method accepts following parameters:
Return value: This method returns the iterator following the removed element.
Below examples illustrate the working of multiset::erase() method:
- constant_value: It refers to the specific element to be removed from the multiset with the help of its value. It must be constant. All instances of this value is erased by this method.
// C++ program to demonstrate
// multiset::erase() method
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Initialise the multiset
multiset<int> multi_set;
multiset<int>::iterator ms_iterator;
// Add values to the multiset
for (int i = 1; i < 10; i++) {
multi_set.insert(i);
}
cout << "Original multiset: ";
for (ms_iterator = multi_set.begin();
ms_iterator != multi_set.end();
++ms_iterator)
cout << ' ' << *ms_iterator;
cout << '\n';
ms_iterator = multi_set.begin();
// Passing constant value to be erased
multi_set.erase(2);
cout << "Modified multiset: ";
for (ms_iterator = multi_set.begin();
ms_iterator != multi_set.end();
++ms_iterator)
cout << ' ' << *ms_iterator;
cout << '\n';
return 0;
}
Output:
Original multiset: 1 2 3 4 5 6 7 8 9 Modified multiset: 1 3 4 5 6 7 8 9
- Syntax:
void erase (iterator starting_iterator, iterator ending_iterator);
Parameters: This method accepts following parameters:
Return value: This method returns the number of removed elements.
Below examples illustrate the working of multiset::erase() method:
- starting_iterator: It refers to the starting iterator of the range of values to be removed from the multiset.
- ending_iterator: It refers to the ending iterator of the range of values to be removed from the multiset.
// C++ program to demonstrate
// multiset::erase() method
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Initialise the multiset
multiset<int> multi_set;
multiset<int>::iterator ms_iterator;
// Add values to the multiset
for (int i = 1; i < 10; i++) {
multi_set.insert(i);
}
cout << "Original multiset: ";
for (ms_iterator = multi_set.begin();
ms_iterator != multi_set.end();
++ms_iterator)
cout << ' ' << *ms_iterator;
cout << '\n';
ms_iterator = multi_set.begin();
ms_iterator++;
ms_iterator++;
// Passing the iterator range for the positions
// at which the values are to be erased
multi_set.erase(ms_iterator, multi_set.end());
cout << "Modified multiset: ";
for (ms_iterator = multi_set.begin();
ms_iterator != multi_set.end();
++ms_iterator)
cout << ' ' << *ms_iterator;
cout << '\n';
return 0;
}
Output:
Original multiset: 1 2 3 4 5 6 7 8 9 Modified multiset: 1 2
multiset clear() function in C++ STL
The multiset::clear() function is a built-in function in C++ STL which removes all elements from the multiset container. The final size of multimap container after removal is 0.
Syntax:
multiset_name.clear()
Parameters: The function does not accept any parameter.
Return Value: The function does not returns anything.
Below programs illustrates the multimap::clear() function:
Program 1:
// CPP program to demonstrate the
// multiset::clear() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 15, 10, 15, 11, 10 };
// initializes the set from an array
multiset<int> s(arr, arr + 5);
// prints all elements in set
cout << "The elements in multiset are: ";
for (auto it = s.begin(); it != s.end(); it++)
cout << *it << " ";
cout << "\nThe size after clear() is: ";
// erases all elements
s.clear();
cout << s.size();
return 0;
}
Output:
The elements in multiset are: 10 10 11 15 15 The size after clear() is: 0
Program 2:
// CPP program to demonstrate the
// multiset::clear() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 15, 10, 15, 11, 10, 18, 18, 20, 20 };
// initializes the set from an array
multiset<int> s(arr, arr + 9);
// prints all elements in set
cout << "The elements in multiset are: ";
for (auto it = s.begin(); it != s.end(); it++)
cout << *it << " ";
cout << "\nThe size after clear() is: ";
// erases all elements
s.clear();
cout << s.size();
return 0;
}
Output:
The elements in multiset are: 10 10 11 15 15 18 18 20 20 The size after clear() is: 0
multiset find() function in C++ STL
The multiset::find() is a built-in function in C++ STL which returns an iterator pointing to the lower_bound of the element which is searched in the multiset container. If the element is not found, then the iterator points to the position past the last element in the set.
Syntax:
multiset_name.find(element)
Parameters: The function accepts one mandatory parameter element which specifies the element to be searched in the multiset container.
Return Value: The function returns an iterator which points to the element which is searched in the multiset container. If the element is not found, then the iterator points to the position just after the last element in the multiset.
Below program illustrates the above function.
Program 1:
// CPP program to demonstrate the
// multiset::find() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Initialize multiset
multiset<int> s;
s.insert(1);
s.insert(4);
s.insert(2);
s.insert(5);
s.insert(3);
s.insert(3);
s.insert(3);
s.insert(5);
cout << "The set elements are: ";
for (auto it = s.begin(); it != s.end(); it++)
cout << *it << " ";
// iterator pointing to
// position where 2 is
auto pos = s.find(3);
// prints the set elements
cout << "\nThe set elements after 3 are: ";
for (auto it = pos; it != s.end(); it++)
cout << *it << " ";
return 0;
}
Output:
The set elements are: 1 2 3 3 3 4 5 5 The set elements after 3 are: 3 3 3 4 5 5
Program 2:
// CPP program to demonstrate the
// multiset::find() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Initialize multiset
multiset<char> s;
s.insert('a');
s.insert('a');
s.insert('a');
s.insert('b');
s.insert('c');
s.insert('a');
s.insert('a');
s.insert('c');
cout << "The set elements are: ";
for (auto it = s.begin(); it != s.end(); it++)
cout << *it << " ";
// iterator pointing to
// position where 2 is
auto pos = s.find('b');
// prints the set elements
cout << "\nThe set elements after b are: ";
for (auto it = pos; it != s.end(); it++)
cout << *it << " ";
return 0;
}
Output:
The set elements are: a a a a a b c c The set elements after b are: b c c
multiset count() function in C++ STL
The multiset::count() function is a built-in function in C++ STL which searches for a specific element in the multiset container and returns the number of occurrence of that element.返回元素的个数。
Syntax:
multiset_name.count(val)
Parameters: The function accepts a single parameter val which specifies the element to be searched in the multiset container.
Return Value: The function returns the count of elements which is equal to val in the multiset container.
Below programs illustrates the multimap::count() function:
Program 1:
// CPP program to demonstrate the
// multiset::count() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 15, 10, 15, 11, 10, 18, 18, 20, 20 };
// initializes the set from an array
multiset<int> s(arr, arr + 9);
cout << "15 occurs " << s.count(15)
<< " times in container";
return 0;
}
Output:
15 occurs 2 times in container
Program 2:
// CPP program to demonstrate the
// multiset::count() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 15, 10, 15, 11, 10, 18, 18, 18, 18 };
// initializes the set from an array
multiset<int> s(arr, arr + 9);
cout << "18 occurs " << s.count(18)
<< " times in container";
return 0;
}
Output:
18 occurs 4 times in container
multiset::emplace() in C++ STL
Multisets are a type of associative containers similar to set, with an exception that multiple elements can have same values.
multiset::emplace()
This function is used to insert a new element into the multiset container.
Syntax :
multisetname.emplace(value) Parameters : The element to be inserted into the multiset is passed as the parameter. Result : The parameter is added to the multiset.
Examples:
Input : mymultiset{1, 2, 3, 4, 5}; mymultiset.emplace(6); Output : mymultiset = 1, 2, 3, 4, 5, 6 Input : mymultiset{}; mymultiset.emplace("This"); mymultiset.emplace("is"); mymultiset.emplace("Geeksforgeeks"); Output : mymultiset = This, is, Geeksforgeeks
Errors and Exceptions
1. It has a strong exception guarantee, therefore, no changes are made if an exception is thrown
2. Parameter should be of same type as that of the container, otherwise an error is thrown
// INTEGER EXAMPLE
// CPP program to illustrate
// Implementation of emplace() function
#include <iostream>
#include <set>
using namespace std;
int main()
{
multiset<int> mymultiset{};
mymultiset.emplace(1);
mymultiset.emplace(56);
mymultiset.emplace(4);
mymultiset.emplace(9);
mymultiset.emplace(0);
// multi set becomes 1, 56, 4, 9, 0
// adding another element
mymultiset.emplace(87);
// printing the multiset
for (auto it = mymultiset.begin();
it != mymultiset.end(); ++it)
cout << ' ' << *it;
return 0;
}
Output:
1 56 4 9 0 87
// STRING EXAMPLE
// CPP program to illustrate
// Implementation of emplace() function
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main()
{
multiset<string> mymultiset{};
mymultiset.emplace("This");
mymultiset.emplace("is");
mymultiset.emplace("a");
mymultiset.emplace("computer science");
mymultiset.emplace("portal");
// multi set becomes This, a,
// computer science, is, portal
// adding element
mymultiset.emplace("GeeksForGeeks");
// printing the multiset
for (auto it = mymultiset.begin();
it != mymultiset.end(); ++it)
cout << ' ' << *it;
return 0;
}
Output:
GeeksForGeeks This a computer science is portal
Application
Input an empty multi set with the following numbers and order using emplace() function and find sum of elements. The advantage of emplace() over insert is, it avoids unnecessary copy of object.
Input : 7, 9, 4, 6, 2, 5, 3 Output : 36
// CPP program to illustrate
// Application of emplace() function
#include <iostream>
#include <set>
using namespace std;
int main()
{
// sum variable declaration
int sum = 0;
// multiset declaration
multiset<int> mymultiset{};
mymultiset.emplace(7);
mymultiset.emplace(9);
mymultiset.emplace(4);
mymultiset.emplace(6);
mymultiset.emplace(2);
mymultiset.emplace(5);
mymultiset.emplace(3);
// iterator declaration
set<int>::iterator it;
// finding sum of elements
while (!mymultiset.empty()) {
it = mymultiset.begin();
sum = sum + *it;
mymultiset.erase(it);
}
// printing the sum
cout << sum;
return 0;
}
Output :
36
Time Complexity : O(logn)
emplace() vs insert()
When we use insert, we create an object and then insert it into the multiset. With emplace(), the object is constructed in-place.
// C++ code to demonstrate difference between
// emplace and insert
#include<bits/stdc++.h>
using namespace std;
int main()
{
// declaring multiset of pairs
multiset<pair<char, int>> ms;
// using emplace() to insert pair in-place
ms.emplace('a', 24);
// Below line would not compile
// ms.insert('b', 25);
// using insert() to insert pair in-place
ms.insert(make_pair('b', 25));
// printing the multiset
for (auto it = ms.begin(); it != ms.end(); ++it)
cout << " " << (*it).first << " "
<< (*it).second << endl;
return 0;
}
Output :
a 24 b 25
multiset empty() function in C++ STL
The multiset::empty() function is a built-in function in C++ STL which checks if the multiset is empty or not. It returns true if the multiset is empty, else it returns false.
Syntax:
multiset_name.empty()
Parameters: The function does not accepts any parameter.
Return Value: The function returns true if the multiset is empty, else it returns false.
Below programs illustrates the multimap::empty() function:
Program 1:
// CPP program to demonstrate the
// multiset::empty() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 15, 10, 15, 11, 10, 18, 18, 20, 20 };
// initializes the set from an array
multiset<int> s(arr, arr + 9);
if (!s.empty())
cout << "The multiset is not empty";
else
cout << "The multiset is empty";
return 0;
}
Output:
The multiset is not empty
Program 2:
// CPP program to demonstrate the
// multiset::empty() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// declaration
multiset<int> s;
if (!s.empty())
cout << "The multiset is not empty";
else
cout << "The multiset is empty";
return 0;
}
Output:
The multiset is empty