Understanding Multiset in C++

Understanding Multiset in C++

5 mins read350 Views Comment
Updated on Oct 3, 2023 11:52 IST

Here you will understand the concept of Multiset in C++. We have covered how to create multiset and its member functions using relevant examples. Let’s understand.

2023_02_Multiset-in-C.jpg

Multiset in C++ is a powerful container that allows storing and manipulating multiple elements with the same value. With its efficient implementation and wide range of member functions, multiset is a great choice for many programming tasks that require manipulating sets of elements. 

This article will discuss the syntax, member functions, and examples of Multiset in C++. So, without further ado, let’s begin! 

We will be covering the following sections: 

Explore: C++ Certification courses

Introduction to Multiset in C++ 

The Standard Template Library (STL) defines multisets as associative containers. It stores values in a specific order and allows storage for duplicate items. The values in a multiset are considered constant, meaning they cannot be altered once added to the container. However, you can be add or remove them as needed.

Using a multiset in C++ lets you store multiple instances of an item without caring about their order. It is similar to a set but allows for duplicate values. The main advantage of using a multiset is that it provides efficient insertion, removal, and search operations, making it a convenient tool for many programming tasks.

The syntax for declaring a std::multiset in C++ is as follows: 

 
std::multiset<type> set_name;
Copy code

Where type is the data type of the elements that the multiset will store, and set_name is the name of the multiset object. 

Here’s an example of declaring a std::multiset of integers: 

 
std::multiset<int> myMultiset;
Copy code

Once the multiset is declared, you can add elements using the insert method.

For example: 

 
myMultiset.insert(10);
myMultiset.insert(20);
myMultiset.insert(30);
Copy code

In this example, three elements are added to the multiset: 10, 20, and 30. We can call the insert method multiple times to add more elements to the multiset. 

Note that a std::multiset can store multiple copies of the same value so that you can add the same value various times to the multiset. 

Must read: C++ Structure and Functions

How to Create a Multiset? 

To create a multiset in C++, you first need to include the <set> header file and then declare a std::multiset object. The type of elements the multiset will store must also be specified in angle brackets. 

Here’s an example of creating a multiset of integers: 

 
#include <set>
#include <iostream>
int main() {
std::multiset<int> myMultiset;
return 0;
}
Copy code

In this example, myMultiset is declared a std::multiset object that stores integers. 

Once you declare the multiset, you can add elements using the insert method. For example: 

 
#include <set>
#include <iostream>
int main() {
std::multiset<int> myMultiset;
myMultiset.insert(10);
myMultiset.insert(20);
myMultiset.insert(30);
return 0;
}
Copy code

This code declares a std::multiset of integers and inserts three elements into it: 10, 20, and 30. The insert method can be called multiple times to add more elements to the multiset. 

Also Read: The Key Difference Between C++ and Java

Member Functions in Multiset 

Here’s a table of the most commonly used member functions in std::multiset in C++, along with their uses: 

Member Function  Description 
insert  Adds an element to the multiset. Returns an iterator pointing to the newly inserted element. 
erase  Removes an element from the multiset. Returns the number of elements removed. 
clear  Removes all elements from the multiset. 
count  Returns the number of elements in the multiset that are equal to a given value. 
find  Returns an iterator pointing to the first element in the multiset that is equal to a given value. If the element is not found, the function returns end()
lower_bound  Returns an iterator pointing to the first element in the multiset that is not less than a given value. 
upper_bound  Returns an iterator pointing to the first element in the multiset that is greater than a given value. 
equal_range  Returns a pair of iterators that define the range of elements in the multiset that are equal to a given value. The first iterator points to the first element in the range, and the second iterator points to the first element that is not in the range. 
begin  Returns an iterator pointing to the first element in the multiset. 
end  Returns an iterator pointing to the element after the last element in the multiset. 

Note: The insert and erase member functions can take time proportional to the size of the multiset. So they may not be suitable for large multisets that must be manipulated frequently. In such cases, you might consider using other associative containers such as std::map or std::unordered_map

Constructors in C++ and its Types
Constructors in C++ and its Types
Constructors in C++ are the member functions that get invoked when an object of a class is created. There are mainly three types of constructors in C++, Default, Parameterized and...read more
Recursion in C++
Recursion in C++
Recursion in C++ is a method that calls itself repeatedly until a predetermined condition is satisfied. A recursive function has a base case and a recursive condition, and it repeatedly...read more
All About OOPs Concepts in C++
All About OOPs Concepts in C++
Have you ever wondered how complex software systems are so efficiently organized and managed? This is where the principles of Object-Oriented Programming (OOP) in C++ come into play. OOP in...read more

Now, let’s look at a few C++ examples that explain the commonly used member functions of std::multiset in C++: 

Example 1: Insert

 
#include <set>
#include <iostream>
int main() {
std::multiset<int> myMultiset;
myMultiset.insert(10);
myMultiset.insert(20);
myMultiset.insert(30);
for (auto it = myMultiset.begin(); it != myMultiset.end(); it++) {
std::cout << *it << " ";
}
std::cout << std::endl;
return 0;
}
Copy code

Output 1: 

10 20 30

In this example, the insert method adds three elements to the multiset. The elements are then printed using an iterator and the begin and end member functions. 

Example 2: Erase 

 
#include <set>
#include <iostream>
int main() {
std::multiset<int> myMultiset;
myMultiset.insert(10);
myMultiset.insert(20);
myMultiset.insert(30);
myMultiset.erase(20);
for (auto it = myMultiset.begin(); it != myMultiset.end(); it++) {
std::cout << *it << " ";
}
std::cout << std::endl;
return 0;
}
Copy code

Output 2: 

10 30

In this example, the erase method removes the element with the value 20 from the multiset. The elements are then printed, and the element with the value 20 is not present in the output. 

Read More: Difference between C and C++ 

Example 3: Erase 

 
#include <set>
#include <iostream>
int main() {
std::multiset<int> myMultiset;
myMultiset.insert(10);
myMultiset.insert(20);
myMultiset.insert(30);
myMultiset.insert(20);
std::cout << "Count of 20: " << myMultiset.count(20) << std::endl;
return 0;
}
Copy code

Output 3: 

Count of 20: 2

In this example, the insert method adds two elements with a value of 20 to the multiset. Then we use the count method to find the number of elements with a value of 20, and the result is printed. 

Endnotes 

In conclusion, std::multiset is an important part of the C++ Standard Template Library (STL). Whether you are an experienced programmer or a newbie, understanding a multiset’s use and syntax is essential for effectively using the C++ STL. 

If you want to learn more about C++ and solidify your basics, you can explore our articles on C++. 

Contributed By: Prerna Singh

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio