How to Delete an Element from a Multiset in C++? Last Updated : 26 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a multiset is a container that stores elements in a specific order. Multiple elements can have the same values. In this article, we will learn how to delete a specific element from a multiset. Example: Input: myMultiset = {5, 2, 8, 5, 8, 8}; Element to delete: 8 Output: myMultiset = {5, 2, 5, 8, 8}Delete an Element from a Multiset in C++To delete a specific element from a std::multiset in C++, we can use the std::multiset::erase() function. This function removes all elements with the given value from the multiset which is passed as an argument. If we want to remove only one occurrence of an element, we need to find an iterator to the element and then use the erase() function. C++ Program to Delete an Element from a Multiset C++ // C++ Program to delete an element from a multiset #include <iostream> #include <set> using namespace std; // Driver Code int main() { multiset<int> set = { 1, 2, 2, 3, 4, 4, 5, 4 }; int element = 4; // Find the occurrence of the element auto i = set.find(element); // Check if the element exists before erasing it if (i != set.end()) { // after erase the element set.erase(i); } else { cout << "Element " << element << " not found." << endl; } // Print after deletion for (const auto& e : set) { cout << e << " "; } cout << endl; return 0; } Output1 2 2 3 4 4 5 Time Complexity: O(logN)Space Complexity: O(1) Comment More infoAdvertise with us Next Article How to Delete an Element from a Multiset in C++? M mrstax Follow Improve Article Tags : C++ Programs C++ STL cpp-multiset CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Delete an Element from a Set in C++? A set in C++ is a container that stores unique elements in a sorted order. In this article, we will learn how to delete a specific element from a set. Example: Input: mySet = {5, 2, 8, 1, 4} Element to delete: 2 Output: mySet = {5, 1, 8, 4}Delete an Element from a Set in C++To delete a specific elem 2 min read How to Delete Multiple Elements from a Set in C++? In C++, sets are containers that store unique elements in some sorted order. In this article, we will learn how to delete multiple elements from a set using C++. Example: Input: set = {10, 20, 30, 40, 50, 60}elements_to_delete = {20, 30, 40}Output: Elements of set after deletion: 10 50 60Removing Mu 2 min read How to delete last element from a set in C++ Given a Set, the task is to remove the last element from this Set in C++. Examples: Input: set = [10 20 30 70 80 90 100 40 50 60] Output: 10 20 30 40 50 60 70 80 90 Input: set = [1 2 3 4 5] Output: 1 2 3 4 Sets are a type of associative containers in which each element has to be unique because the v 3 min read How to Delete the Last Element from a Multiset in C++? In C++, a multiset is a container that can store multiple elements, including duplicates. In this article, we will learn how to delete the last element from a multiset in C++. Example: Input:myMultiset = { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4 }Element to delete = 4Output: myMultiset = { 1, 2, 2, 3, 3, 3, 4, 2 min read How to Delete an Element from the Set in C++? In C++, the set container stores unique elements in the sorted order and the deletion operation should not disrupt this sorted order.C++ provides a built-in function set erase(), which can be used to easily delete the given element by passing its value to the function. Let's take a look at the simpl 1 min read How to Remove an Element from a Deque in C++? In C++ STL, a container called deque (known as a double-ended queue) allows us to insert and delete elements at both its beginning and its end. In this article, we will learn how to remove a specific element from a deque in C++ STL. Example: Input: myDeque= {4, 2, 3, 5, 2} Target = 4 Output: Deque A 2 min read How to Delete an Element from an Array in C++? In C++, arrays are data structures that allow users to store data of the same type in contiguous memory locations. In this article, we will learn how to delete an element from an array in C++. Example: Input:myarr = {20, 5, 1, 10, 15};Target: 1Output:Array after deletion: 20 5 10 15Remove an Element 3 min read How to Delete All Elements from a Vector in C++? In C++, you can delete all items from a vector to either clear its contents or free its memory. In this article, we will learn how to delete all items from a vector in C++.The recommended way to delete all items from a vector is by using the vector clear() function. Letâs take a look at a simple exa 2 min read How to Remove an Element from a Set in C++? In C++, sets are a type of associative container in which each element has to be unique. The values are stored in a specific sorted order i.e. either ascending or descending. In this article, we will see how to remove specific elements from a set in C++. Example Input: set = {100,120,12,56,78,9,32,4 2 min read Like