Javatpoint Logo
Javatpoint Logo

C++ STL Multiset

Introduction to multiset

Multisets are part of the C++ STL (Standard Template Library). Multisets are the associative containers like Set that stores sorted values (the value is itself the key, of type T), but unlike Set which store only unique keys, multiset can have duplicate keys. By default it uses < operator to compare the keys.

The value of the elements in a multiset can be inserted or deleted but cannot be altered (The elements are always const).

Syntax

Parameters

T: Type of element stored in the container multiset.

Compare: A comparison class that takes two arguments of the same type bool and returns a value. This argument is optional and the binary predicate less is the default value.

Alloc: Type of the allocator object which is used to define the storage allocation model.

Example 1

Let's see an example to demonstrate C++ Multiset:

Output:

---------------------
Multiset Example

---------------------
1.Insert Number into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.First Element of the Multiset
8.Exit
Enter your Choice: 1
Enter value to be inserted: 100

---------------------
Multiset Example

---------------------
1.Insert Number into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.First Element of the Multiset
8.Exit
Enter your Choice: 1
Enter value to be inserted: 200

---------------------
Multiset Example

---------------------
1.Insert Number into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.First Element of the Multiset
8.Exit
Enter your Choice: 1
Enter value to be inserted: 300

---------------------
Multiset Example

---------------------
1.Insert Number into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.First Element of the Multiset
8.Exit
Enter your Choice: 2
Enter value to be deleted: 200

---------------------
Multiset Example

---------------------
1.Insert Number into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.First Element of the Multiset
8.Exit
Enter your Choice: 3
Enter element to find 100
Element found

---------------------
Multiset Example

---------------------
1.Insert Number into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.First Element of the Multiset
8.Exit
Enter your Choice: 4
Enter element to be counted: 100
100 appears 1 times.

---------------------
Multiset Example

---------------------
1.Insert Number into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.First Element of the Multiset
8.Exit
Enter your Choice: 5
Size of the Multiset: 2

---------------------
Multiset Example

---------------------
1.Insert Number into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.First Element of the Multiset
8.Exit
Enter your Choice: 6
Elements of the Multiset:  100  300  

---------------------
Multiset Example

---------------------
1.Insert Number into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.First Element of the Multiset
8.Exit
Enter your Choice: 7
The First Element of the Multiset is 100

---------------------
Multiset Example

---------------------
1.Insert Number into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.First Element of the Multiset
8.Exit
Enter your Choice: 8

Example 2

Let's see another example to demonstrate C++ Multiset:

Output:

Marks of ms1 class Room: 
  600  500  500  400  300  200  100

The Number of students in class Room after assigning Class Room students: 
  100  200  300  400  500  500  600

Highest marks in ms1 Class Room: 600
Highest marks in ms2 Class Room: 100

ms2 Class Room after removal of Students less than 300 marks:
   300  400  500  500  600

ms2.erase(500) : 2 removed 	  300  400  600

ms1.lower_bound(400) : 400
ms1.upper_bound(400) : 300
ms2.lower_bound(400) : 400
ms2.upper_bound(400) : 600

Member Functions

Below is the list of all member functions of multiset:

Constructor/Destructor

Functions Description
(constructor) Construct multiset
(destructor) Multiset destructor
operator= Copy elements of the multiset to another multiset.

Iterators

Functions Description
Begin Returns an iterator pointing to the first element in the multiset.
Cbegin Returns a const iterator pointing to the first element in the multiset.
End Returns an iterator pointing to the past-end.
cend Returns a constant iterator pointing to the past-end.
rbegin Returns a reverse iterator pointing to the end.
rend Returns a reverse iterator pointing to the beginning.
crbegin Returns a constant reverse iterator pointing to the end.
crend Returns a constant reverse iterator pointing to the beginning.

Capacity

Functions Description
empty Returns true if multiset is empty.
size Returns the number of elements in the multiset.
max_size Returns the maximum size of the multiset.

Modifiers

Functions Description
insert Insert element in the multiset.
erase Erase elements from the multiset.
swap Exchange the content of the multiset.
clear Delete all the elements of the multiset.
emplace Construct and insert the new elements into the multiset.
emplace_hint Construct and insert new elements into the multiset by hint.

Observers

Functions Description
key_comp Return a copy of key comparison object.
value_comp Return a copy of value comparison object.

Operations

Functions Description
find Search for an element with given key.
count Gets the number of elements matching with given key.
lower_bound Returns an iterator to lower bound.
upper_bound Returns an iterator to upper bound.
equal_range Returns the range of elements matches with given key.

Allocator

Functions Description
get_allocator Returns an allocator object that is used to construct the multiset.

Non-Member Overloaded Functions

Functions Description
operator== Checks whether the two multisets are equal or not.
operator!= Checks whether the two multisets are equal or not.
operator< Checks whether the first multiset is less than other or not.
operator<= Checks whether the first multiset is less than or equal to other or not.
operator> Checks whether the first multiset is greater than other or not.
operator>= Checks whether the first multiset is greater than equal to other or not.
swap() Exchanges the element of two multisets.


Next TopicC++ multiset




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials