Open In App

How to Maintain the Order of a Vector in a Multiset in C++?

Last Updated : 18 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, multisets are associative containers that store elements in a sorted manner but unlike sets, multisets allow the users to store duplicate values as well and vectors are dynamic arrays that store data in contiguous memory locations. In this article, we will learn how to maintain the order of a vector in a multiset in C++.

Example:

Input:
myVector={1, 2, 4, 5, 3}

Output:
myMultiset = {1, 2, 4, 5, 3}

Maintaining the Order of a Vector in a Multiset in C++

Unfortunately, there is no way to preserve the order of elements of the vector when they are inserted into a multiset because a multiset can only store elements in some given order. So unless the vector is sorted, we cannot maintain the order of these elements directly.

But suppose we really need to store the vector elements in the multiset with the same order. In that case, we can use a custom data type with value of the vector element as one member and index of that element as other use the index value as the key in the comparator of the multiset.

Otherwise, we can use multimap with index as keys and the element at that index as values.

Syntax

multiset<customType, comparator>multiset_name;

where,

  • customType: custom data type
  • comparator: custom comparator for custom data type.

C++ Program to Convert a Vector to a Multiset Without Losing Element Order

The following program illustrates how we can convert a vector to a multiset without losing the order of the elements of the vector.

C++
// C++ program to convert a vector to a multiset without
// losing element order
#include <iostream>
#include <set>
#include <vector>
using namespace std;

// custom data type
class CustomType {
public:
    int value;
    int index;

    // constructor for easy initialization
    CustomType(int v, int i)
        : value(v)
        , index(i)
    {
    }
    CustomType(const CustomType& obj)
    {
        value = obj.value;
        index = obj.index;
    }
};

// comparator definition
struct Comp {
public:
    bool operator()(const CustomType& a,
                    const CustomType& b) const
    {
        return a.index < b.index;
    }
};

// driver code
int main()
{
    // Initializing a vector
    vector<int> myVector = { 1, 2, 3, 4, 5 };

    // printing the values in vector
    cout << "Values in the vector: ";
    for (auto i : myVector) {
        cout << i << " ";
    }
    cout << endl;

    // creating set of custom data type
    set<CustomType, Comp> mySet;

    // inserting values
    for (int i = 0; i < myVector.size(); i++) {
        mySet.insert({ myVector[i], i });
    }

    // printing the values
    cout << "Values in the set: ";
    for (auto i : mySet) {
        cout << i.value << " ";
    }

    return 0;
}

Output
Values in the vector: 1 2 3 4 5 
Values in the set: 1 2 3 4 5 

Time Complexity: O(N), where N is the size of the vector.
Auxiliary Space: O(N)





Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads