×

How is multiset implemented in C++

Similar to sets, multisets are an associative container type where several items may share the same values.

Associative containers implement instantly searchable sorted data structures with O(log n) complexity.

In a multiset, an element is also identified by its value (the value is itself the key, of type T). A multiset's elements can be added to or deleted from the container, but their values cannot be changed once they are inside the container (they are always const).

A multiset's internal comparison object always indicates the strict weak ordering criterion that must be followed while sorting the elements internally (of type Compare).

Multiset containers provide direct iteration on subsets depending on their order but are typically slower than unordered multiset containers to access individual pieces by their key.

The most common way to implement multisets is via binary search trees.

Associated multiset functions:

  • end() -  This function is used to return a theoretical element that follows the last element, which is present in the Multiset through an iterator in O(1)
  • begin() - This function is used to return the first element in the Multiset through an iterator -> O(1)
  • size() - Returns how many elements are in the multiset -> O(1)
  • max size() - Returns how many elements the multiset can hold -> O(1)
  • empty () - Checks to see if the multiset is empty and inserts in O(1) time (x) Removes all elements from the multiset -> O(n)
  • erase(x); - Inserts element x into the multiset -> O(log n)
  • clear (); - Removes all instances of x -> O (log n)

Implementation of Multi set

Example 1:

#include <iostream>
#include <set>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
    multiset<int> mse;
    multiset<int>::iterator it, it1;
    int ch, item;
    while (1)
    {
        cout<<"\n*************"<<endl;
        cout<<"Implementation of Multiset in STL"<<endl;
        cout<<"\n*************"<<endl;
        cout<<"1.Insert an Element into the Multiset"<<endl;
        cout<<"2.Delete an Element from the Multiset"<<endl;
        cout<<"3.Find out the Element in a Multiset"<<endl;
        cout<<"4.Count the Elements with a particular key"<<endl;
        cout<<"5.Size of the Multiset"<<endl;
        cout<<"6.Display Multiset"<<endl;
        cout<<"7.Exit"<<endl;
        cout<<"Enter your Choice: ";
        cin>>ch;
        switch(ch)
        {
        case 1:
            cout<<"Enter value to be inserted: ";
            cin>>item;
            if (mse.empty())
                it1 = mse.insert(item);
            else
                it1 = mse.insert(it1, item);
            break;
        case 2:
            cout<<"Enter value to be deleted: ";
            cin>>item;
            mse.erase(item);
            break;
        case 3:
            cout<<"Enter element to find ";
            cin>>item;
            it = mse.find(item);
            if (it != mse.end())
                cout<<"Element found"<<endl;
            else
                cout<<"Element not found"<<endl;
            break;
        case 4:
            cout<<"Enter element to be counted: ";
            cin>>item;
            cout<<item<<" appears "<<mse.count(item)<<" times."<<endl;
            break;
        case 5:
            cout<<"Size of the Multiset: "<<mse.size()<<endl;
            break;
        case 6:
            cout<<"Elements of the Multiset:  ";
            for (it = mse.begin(); it != mse.end(); it++)
                cout<<*it<<"  ";
            cout<<endl;
            break;
		case 7:
            exit(1);
	        break;
        default:
            cout<<"Wrong Choice Entered"<<endl;
        }
    }
    return 0;
}

Output:

How is multiset implemented in c++?

Example 2:

#include <iostream>
#include <bits/stdc++.h>


using namespace std;


void show(multiset<int> s)
{


    multiset<int>::iterator i;


    for (i = s.begin(); i != s.end(); i++)
    {
        cout << *i << "  ";
    }


    cout << endl;
}


int main()
{
    cout << "  Program to illustrate the working of a Multiset, in CPP  \n";


    cout << "*** Multisets are similar to set, with an exception that multiple elements can have same values. *** \n\n";


    //Set declaration (Set of integers)
    multiset<int> s;


    //Filling the elements by using the insert() method.
    cout << "\n\nFilling the Multiset with integers in random order."; //Multiset automatically stores them in order


    s.insert(5);
    s.insert(39);
    s.insert(5);
    s.insert(82);
    s.insert(39);
    s.insert(54);


    cout << "\n\nThe number of elements in the Multiset are: " << s.size();


    cout << "\n\nThe elements of the Multiset are: ";
    show(s);


    multiset<int>::iterator it;
    s.erase(s.begin(), s.find(54));


    cout << "\n\nAfter deleting all the elements which are less than 54, the Multiset become as : ";


    for (it = s.begin(); it != s.end(); it++)
    {
        cout << " " << *it;
    }


    cout << "\n\n\n";


    return 0;
}

Output:

How is multiset implemented in c++?

Example 3:

#include <iostream>
#include <bits/stdc++.h>
 
using namespace std;
 
int main()
{
    multiset<int, greater<int> > g1;
 
    // insert elements in random order
    g1.insert(40);
    g1.insert(30);
    g1.insert(60);
    g1.insert(20);
    g1.insert(50);
 
    // 50 will be added again to
    // the multiset unlike set
    g1.insert(50);
    g1.insert(10);
 
    // printing multiset g1
    multiset<int, greater<int> >::iterator itr;
    cout << "\nThe multiset g1 is : \n";
    for (itr = g1.begin(); itr != g1.end(); ++itr) {
        cout << *itr << " ";
    }
    cout << endl;
 
    // assigning the elements from g1 to g2
    multiset<int> g2(g1.begin(), g1.end());
 
    cout << "\nThe multiset g2 \n"
            "after assign from g1 is : \n";
    for (itr = g2.begin(); itr != g2.end(); ++itr) {
        cout << *itr << " ";
    }
    cout << endl;
 
    // remove all elements up to element
    // with value 30 in g2
    cout << "\ng2 after removal \n"
            "of elements less than 30 : \n";
    g2.erase(g2.begin(), g2.find(30));
    for (itr = g2.begin(); itr != g2.end(); ++itr) {
        cout << *itr << " ";
    }
 
    // remove all elements with value 50 in gquiz2
    int num;
    num = g2.erase(50);
    cout << "\ng2.erase(50) : \n";
    cout << num << " removed \n";
    for (itr = g2.begin(); itr != g2.end(); ++itr) {
        cout << *itr << " ";
    }
 
    cout << endl;
 
    // lower bound and upper bound for multiset g1
    cout << "\ng1.lower_bound(40) : \n"
         << *g1.lower_bound(40) << endl;
    cout << "g1.upper_bound(40) : \n"
         << *g1.upper_bound(40) << endl;
 
    // lower bound and upper bound for multiset g2
    cout << "g2.lower_bound(40) : \n"
         << *g2.lower_bound(40) << endl;
    cout << "g2.upper_bound(40) : \n"
         << *g2.upper_bound(40) << endl;
 
    return 0;
}

Output:

The multiset g1 is : 
60 50 50 40 30 20 10 


The multiset g2 
after assign from g1 is : 
10 20 30 40 50 50 60 


g2 after removal 
of elements less than 30 : 
30 40 50 50 60 
g2.erase(50) : 
2 removed 
30 40 60 


g1.lower_bound(40) : 
40
g1.upper_bound(40) : 
30
g2.lower_bound(40) : 
40
g2.upper_bound(40) : 
60

Related Topics

C++ Ternary Operator

In this tutorial, we'll learn about the C++ ternary operator and how to utilise it to manage the program's flow using examples. Ternary Operator: The if-else statement and the conditional operator use...

3 minutes read.

C++ Algorithms

There are plenty of programming paradigms that are closely associated with the implementations of code and simulate them into a proper functional one. This is done with the help of...

5 minutes read.

DES in C++

The popularity of the Data Encryption Standard (DES) is slightly declining as a result of the discovery that DES is susceptible to very strong attacks. Since DES is a block cypher,...

3 minutes read.

Assertions in C/C++

Assertions are the statements used to check presumptions which is made by the programmers. Example: Assertion is used to verify whether the malloc returned by the pointer is NULL or not. For...

4 minutes read.

C++ If-else-if

Introduction: If-else-if control statement is an if statement used with an optional else if control statement to check multiple conditions. In this control statement, when any one of the condition returns...

4 minutes read.

Method overriding in C++

What is method overriding? Using the same function in derived class as their base class is referred to as function/method overriding in c++. Method overriding is an example of polymorphism. With...

2 minutes read.

Preventing Object Copy in C++

C++ is an object-oriented programming language that provides the ability to create objects, define class and pass objects to functions. When passing an object to a function or returning an...

7 minutes read.

Smart pointers in C++

What are Pointers ? Pointers are often used to keep track of a variable's address. A null value can be assigned to a pointer. Pass by reference can be used to...

6 minutes read.

Const_cast in C++ Type Casting Operators

In this tutorial, we will learn enough about const cast in the most widely used programming language, C++, as well as type casting operations. C++ supports the four types of casting...

4 minutes read.

Virtual class in C++

Introduction to Virtual Base class Virtual base classes can be utilized in virtual inheritance as a mechanism for examining many "instances" of a certain class while searching through multiple inheritances in...

6 minutes read.

Difference between exit() and _Exit() in C++

Before understanding the difference between the exit() and _Exit(), one must know about exit() and _Exit() functions. The exit() function in C/C++ The exit() method in the C language kills the calling...

3 minutes read.

C++ Functions

In other programming languages, a function is referred to as a process or a subroutine. We can design functions to execute any task. A function can be used repeatedly. It...

5 minutes read.

C++ First Program

Let's write a simple basic program structure of C++, its compilation and its execution (how it runs). This program is compiled using GCC compiler. Open any editor to write C++ program. #include<iostream>   using namespace std;   int main(){       cout<<"Welcome to C++ program"<<endl;   } Output...

2 minutes read.

Advantage and disadvantage friend function C++

Friend Function: - A friend function in C++ is a function that can access the private, protected, and public members of a class. In C++, a friend function is a function that...

2 minutes read.

C++ Pointer

Pointer is a derived data type that stores the address of a variable. A pointer is used for memory management and dynamic memory allocation. Pointer works on the address of data rather than...

2 minutes read.

Similarities between C++ and Java

People who are preparing for software engineering roles must have come across either one of these languages because as all the companies do not accept python for hiring a fresher...

3 minutes read.

C++ Program For FCFS (First Come First Serve)

The most basic scheduling technique is FCFS, often known as "FIFO (First In, First Out)". In this procedure, the first one is utilized and executed first, while the second one...

4 minutes read.

How to create a button in C++

A button is an option through which a user can control to click a provided input to an application. There are several buttons, each with different styles, to maintain the...

3 minutes read.

Type conversion in C++

Type conversion is the process of changing the type of variables in a program. The ultimate goal of type conversions is to allow variables of a single data type operate with...

7 minutes read.

How to call a void function in C++

Generally, any function has two types: 1. Void function: It doesn't return any value. 2. Non-void function: It returns some value. Program to call a void function in C++ #include <iostream> using namespace std;  void check() {  ...

2 minutes read.