C++ Programming Code Examples
Learn C++ Language
Multiset erase() Function in C++ Programming Language
Multiset erase() Function in C++
Erase elements. Removes elements from the multiset container. The multiset::erase() is the STL function in C++ removes the specified element from multiset.
This effectively reduces the container size by the number of elements removed, which are destroyed. The parameters determine the elements removed:
Syntax for Multiset erase() Function in C++
#include <set>
//(1)
iterator erase (const_iterator position);
//(2)
size_type erase (const value_type& val);
//(3)
iterator erase (const_iterator first, const_iterator last);
position
Iterator pointing to a single element to be removed from the multiset. Member types iterator and const_iterator are bidirectional iterator types that point to elements.
val
Value to be removed from the multiset. All elements with a value equivalent to this are removed from the container. Member type value_type is the type of the elements in the container, defined in multiset as an alias of its first template parameter (T).
first, last
Iterators specifying a range within the multiset container to be removed: [first,last). i.e., the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.
Member types iterator and const_iterator are bidirectional iterator types that point to elements.
The erase() function returns an iterator that point to the next element of the deleted element or returns the number of deleted elements.
For the value-based version (2), the function returns the number of elements erased.
Member type size_type is an unsigned integral type.
The other versions return an iterator to the element that follows the last element removed (or multiset::end, if the last element was removed).
Member type iterator is a bidirectional iterator type that points to elements.
Complexity
For the first version (erase(position)), amortized constant. For the second version (erase(val)), logarithmic in container size, plus linear in the number of elements removed. For the last version (erase(first,last)), linear in the distance between first and last.
Iterator validity
Iterators, pointers and references referring to elements removed by the function are invalidated. All other iterators, pointers and references keep their validity.
Data races
The container is modified. The elements removed are modified. Concurrently accessing other elements is safe, although iterating ranges in the container is not.
Exception safety
Unless the container's comparison object throws, this function never throws exceptions (no-throw guarantee).
Otherwise, if a single element is to be removed, there are no changes in the container in case of exception (strong guarantee).
Otherwise, the container is guaranteed to end in a valid state (basic guarantee).
If an invalid position or range is specified, it causes undefined behavior.
/* C++ Multiset erase() function is used to remove either a single element associated with given key or a range of elements ([first, last)) from the multiset container. Hence, the size will be reduced by the number of elements removed. */
/* Remove elements from the multiset container by multiset::erase function code example. */
#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
auto ir = 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';
(ir == multi_set.end())
? cout << "Return value is: multi_set.end()\n "
: cout
<< "Return value is not multi_set.end()\n";
return 0;
}
The array of integers indicating the marks of the students is given, U have to calculate the percentile of the students aaccording to this rule: the percentile of a student is the %of no