| by Arround The Web | No comments

C++ Multiset Functions

The multiset component of the C++ standard library is utilized for storage and access from a set where the values of the components involved are employed as the key values to dynamically organize the data even if they do not have to be unique. It is not easy to manually modify the key value of an item in a multiset. Rather, the components that have new values should be added and the old values should be eliminated from the components. The multisets are containers that are identical to set containers. They hold the values in the form of keys in a particular sequence, just like a set. The elements of a multiset are specified as keys just like the sets.

The primary distinction between a set and a multiset is that a set has separate keys, but a multiset allows for keys with a similar value. The binary search trees are implemented using multiset keys. Once the items are included in the multiset, these items cannot be modified; they could only be added or removed. The header file for #include <set> contains a multiset. The iterators can be used to retrieve the multiset’s components.

Methods Used on Multisets

Here are the following C++ multiset methods:

  • Begin() function: It gives an iterator to the initial item of the set.
  • End() function: It provides an iterator to the item after the final item of the set.
  • Size() function: It provides the multiset’s size information.
  • Insert() function: It adds an item to the multiset.
  • Erase() function: It removes the multiset’s components.
  • Find() function: If the item is located, this function gives an iterator referring to it; otherwise, it provides an iterator referring to the multiset’s end.
  • Clear() function: It removes all of the multiset’s items.
  • Empty() function: It informs us of the multiset’s status as being blank or not.

Let’s discuss the use of these functions.

Example 1:

The C++ STL’s built-in begin() method is specified in the <bits/stdc> header file. The iterator returned by this method points to the first item in the multiset container that it is related to. The begin() function refers to the component that is the container’s first item based on the sorting condition because the multiset containers retain the items in ascending order.

#include <bits/stdc++.h>

using namespace std;
int main() {
   int a[] = {22, 34, 51, 83, 68, 50, 96};
   multisetcheck(a, a + 7);
cout<<"The elements of the list are: ";
   for (auto x = check.begin(); x != check.end(); x++)
cout<< *x << " ";
cout<<"\nFirst element of the list: "<<*(check.begin());
   return 0;
}

First of all, we introduce the header file <bits/stdc++.h>. This header file is associated with the utilization of the multiset functions in C++ programs. Then, we use the standard namespace as std. Now, we invoke the main() method. Here, we initialize an array. This array contains 7 different values. These items of the array is saved in a variable “a”. Then, we utilize the multiset function. Here, we pass the array as the parameter of the function. Now, the “cout” statement is used to show all the elements of the required list. We apply the “for” loop. This loop iterates every value of the list until the condition is fulfilled.

We employ the begin() and end() methods within the “for” loop. The begin() function starts the list to be printed. Whereas the end() function terminates the list. Next, we increase the value of the loop variable. We use the “cout” statement again to print the first value of the specified list. We use the begin() method to obtain the first value of the list,. In the end, we enter the “return 0” command.

Example 2:

The end() function is a built-in part of the C++ STL and is found in the <bits/stdc> header file. This function gives back an iterator that points to the multiset container before the end location. The item that comes after the last item in a multiset container is known as the prior-to-end component. In other words, it doesn’t point to any particular multiset container component. This method does not take any arguments.

#include <bits/stdc++.h>

using namespace std;
int main() {
   int v[] = {24, 64, 19, 35, 82, 59, 46};
   multisetcheck(v, v + 7);
cout<<"The items of the list are: ";
   for (auto j = check.begin(); j != check.end(); j++)
cout<< *j << " ";
   return 0;
}

First, the header file “bits/stdc++.h” is added. This library is associated with how the multiset functions are used in the C++ programs. The standard namespace is now included and denoted as std. Now that we invoke the main () method, we execute the scripts in the function’s body. Here, an array is initialized. There are 7 different elements in this array. These array’s items is kept in the variable “v.” The multiset function is then called. The actual number of items in the specified array is also stated.

Now, the entire required list is displayed using the “cout” statement. We use the “for” loop. This loop repeats each value in the list until the requirement is met. We call the begin() and end() methods in the “for” loop. The list to be displayed is initiated by the begin() method, and it is completed with the help of the end() function. The value of “j” is increased in the last segment of the “for” loop. We employ the “cout” statement once more. The “return 0” statement is executed at the end.

Example 3:

The header file “bits/stdc++.h” contains the built-in count() method of the C++ Standard Template Library. With the help of this function, we determine how many items have a certain key. This function can be applied to count the number of times. The same item is used in a multiset because a multiset may include many values for a particular member.  In the entire container, count() looks for the key, and then it returns the outcomes. The method returns 0 if there aren’t any of the elements that we’re searching for in the set. The related multiset container must be examined for the method’s one value as an argument, which it takes.

#include <bits/stdc++.h>

using namespace std;
int main() {
   int u[] = {78, 23, 56, 78, 91, 16, 78, 62};
   multisetcheck(u, u + 8);
cout<<"The values of the list are: ";
   for (auto l = check.begin(); l != check.end(); l++)
cout<< *l << " ";
cout<< "\n78 is present: "<<check.count(78)<<" times";
cout<< "\n56 is present: "<<check.count(56)<<" times";
   return 0;
}

At the start, we include the <bits/stdc++.h> library. In the next step, we make use of the default namespace. The main() method is now called. Here, an array is initialized. Eight different integers are specified in this array. These array members are kept in a variable called “u.” Next, we use the multiset method. Here, we provide the array as a parameter to the multiset function. We also indicate the overall element count of that array. The values of the required list are then displayed using the “cout” statement. The “for” loop is used here. Until the condition is met, this loop iterates through each item in the array.

We use the begin() and end() functions with the “for” loop. The list starts printing when the begin() method is used. But the list is ended by the end() method. The value of the “I” is increased inside the “for” loop. Once more, the “cout” command is applied to print the probability with which the number 78 appears in the defined list. The “count()” method is called to find the probability. We also want to know how frequently the value “56” exists in the list. So, we use the count() function yet again. To display the answer, we make use of the “cout” statement. The “return 0” statement is typed at the termination of the code.

Example 4:

The value given as the point of a parameter is removed by the multiset method, erase(). Execute the following program to see how the erase() method performs:

#include <bits/stdc++.h>

using namespace std;
int main()
{
multisetset;
multiset::iterator itr;
for (int k = 1; k < 20; k++) {
set.insert(k);
}
cout<< "Original set: ";
for (itr = set.begin();
itr != set.end();
++itr)
cout
<< ' ' << *itr;
cout<< '\n';
itr = set.begin();
itr++;
set.erase(itr);
cout<< "Updated set: ";
for (itr = set.begin();
itr != set.end();
++itr)
cout<< ' ' << *itr;
cout<< '\n';
return 0;
}

Here, we incorporate the library <bits/stdc++.h>. This header file deals with the use of the multiset functions. Then, we use the standard namespace. Along with this, we call the main() function. We utilize the multiset function two times. For the first multiset method, the set is passed as an attribute. And for the second multiset method, the iterator “itr” is provided as the argument.

Furthermore, we apply the “for” loop. We initialize a loop variable first. Then, the loop iterates over the value below 20. Next, do an increment in the loop variable. We use the “cout” statement to show the actual set of elements. Let’s use the “for” loop once again. Here, we call the begin() method. Along with this, we invoke the end() function. We apply these functions on the value of the iterator “itr”. Then, the value of the iterator is increased by 1. We want to remove the second value from the required set, so we use the erase() method.

After this, the “cout” statement is applied to print the elements of the set after the removal of the second value. To display the elements of the set in a sequence, we utilize the “for” loop again. Within the “for” loop, the begin() and end() functions are employed to start and terminate the set. In the end, we utilize the “return 0” command.

Example 5:

In this illustration, we insert the values in the set using the insert() multiset function. Then, we delete all the items from the set using the multiset clear() method.

#include<iostream>

#include<set>

using namespace std;

int main() {
  multiset a1;
  multiset <int, greater >a2;
  for (int j = 0; j < 15; j++) {
    a1.insert(j + 1);
  }
  for (int j = 0; j < 15; j++) {
    a1.insert(j + 1);
  }
  for (int j = 0; j < 15; j++) {
    a2.insert((j + 1) * 10);
  }
  for (int j = 0; j < 15; j++) {
    a2.insert((j + 1) * 10);
  }
  set  ::iteratoriter;
  for (iter = a1.begin(); iter != a1.end(); iter++)
cout<< * iter<< " ";
cout<< '\n';
  for (iter = a2.begin(); iter != a2.end(); iter++)
cout<< * iter<< " ";
cout<< '\n';

We include two header files <iostream> and <set> at the commencement of the code. Along with this, we utilize a standard namespace. Then, we call the main() function. Let’s start coding in the body of the main() function. First, we call the multiset function two times. We initialize two different variables named “a1” and “a2”. Then, we apply the “for” loop.

After initializing the loop variable, applying the condition on the loop variable, and incrementing the value of the loop variable, we use the insert() method to add the elements. Moreover, we invoke the set function. Here, we declare a variable “iter” for the iterator. We increase the value of the “iter”. The “for” loop is used for both variables “a1” and “a2”, respectively.

a1.erase(6);
  a2.erase(a2.begin(), a2.find(12));
cout<< "After removing the element from the required set, now the size will be: " << a1.size() << '\n';
  int v = 9;
  if (a1.find(v) != a1.end())
cout<< "The defined set has: " << v << " values" <<endl;
  else
cout<< "The defined set does not have: " << v <<endl;
cout<< "The new values of the set: ";
  for (iter = a1.begin(); iter != a1.end(); iter++)
cout<< * iter<< " ";
cout<< '\n';

  a1.clear();

  if (a1.empty() == true) {
cout<< "The set has no element";
  }
  return 0;
}

Furthermore, we use the erase() method. Next, we use the find() method to get the “12” element from the set. We use the “cout” statement to show the set after removing the “6” element. Then, we initialize a variable “v”. We call the find() and end() functions within the “if” condition which are associated with the first set. We apply the “if” condition on the element provided to the find() function. If that condition is fulfilled, “cout” prints the elements of the set. Otherwise, the “cout” statement prints the message that shows that the required value is not present in the set.

Once again, we employ the “for” loop, begin() and end() functions. In the end, we apply the clear() method and the “if” condition. We define the condition in the body of the “if” condition. If the set is empty, the “cout” statement shows the text “The set has no element”. After all this, the “return0” command is entered.

Conclusion

First, we talked about what a C++ multiset is. Then, we observed several C++ functions applied to multisets. In this article, we run various codes that contain a variety of multiset methods. In the first illustration, we used the begin() function and demonstrated its functionality. An iterator referring to the very first member of the multiset linked to this method is the function’s return value. The second example showed how to employ the end() multiset function. The output of this method is an iterator that is pointing past the multiset’s last component. The count() method is used in the next instance to examine what happens in the code. This function totals all the integers that are available and share the same key. Then, we run the program using the erase() function. The element is removed from the set using this approach. To add the item to the set, we ultimately utilized the insert() technique.

Share Button

Source: linuxhint.com

Leave a Reply