Open In App

Multiset of Vectors in C++ with Examples

What is Multiset?

A multiset in C++ is an associative container that can hold a number of elements in a specific order. Unlike a set, a multiset can hold multiple copies of the same element.



Functions associated with a multiset:  

What is Vector?



In C++, vectors are the same as dynamic arrays with their ability to resize themself whenever required. A vector can contain homogeneous elements only. In simple words, a vector may contain elements of a particular data type only and one need to specify the data type for a vector at the time of declaration.

Functions associated with a vector:

Multiset of Vectors

A multiset of vectors is a multiset in which each element is a vector itself. Two vectors are considered equal if the corresponding elements of two vectors are equal. A multiset can contain more than one occurrence of the same vector along with other vectors that too in sorted order. 

Syntax:

multiset<vector<dataType>> myMultiset;

Here,

dataType: A data type. It represents the type of values stored by a vector in myMultiset.

Example 1: In the below C++ program a multiset of vectorof integers is created.




// C++ program to demonstrate the
// working of multiset of vectors
#include <bits/stdc++.h>
using namespace std;
 
// Function to iterate over
// vector elements
void printVector(vector<int> myVector)
{
  cout << "[ ";
  for(auto element : myVector)
    cout << element << ' ';
  cout << "]\n";
}
 
// Function to iterate over multiset
// elements
void print(multiset<vector<int>> &multisetOfVectors)
{
  for (auto it = multisetOfVectors.begin();
       it != multisetOfVectors.end();
       it++)
  {
    // Each element is a vector
    printVector(*it);
  }
}
 
// Driver code
int main()
{
  // Declaring a multiset of vectors
  // A vector is of integer type
  multiset<vector<int>> multisetOfVectors;
 
  // Initializing vectors
  vector<int> myVector1 {3, 6, 9, 10};
  vector<int> myVector2 {5, 10, 11, 7};
  vector<int> myVector3 {3, 6, 9, 10};
  vector<int> myVector4 {5, 10, 15};
  vector<int> myVector5 {50, 20, 30, 40};
 
  // Inserting vectors into multiset
  multisetOfVectors.insert(myVector1);
  multisetOfVectors.insert(myVector2);
  multisetOfVectors.insert(myVector3);
  multisetOfVectors.insert(myVector4);
  multisetOfVectors.insert(myVector5);
 
  // Calling print function
  print(multisetOfVectors);
 
  return 0;
}

Output:

[ 3 6 9 10 ]
[ 3 6 9 10 ]
[ 5 10 11 7 ]
[ 5 10 15 ]
[ 50 20 30 40 ]

Explanation:

In the above output, “myVector1” and “myVector3” are the same. That is why two copies of the same vector can be seen in the output.

Example 2: In the below C++ program a multiset of vector of strings is created.




// C++ program to demonstrate the
// working of multiset of vectors
#include <bits/stdc++.h>
using namespace std;
 
// Function to iterate over vector elements
void printVector(vector<string> myVector)
{
  cout << "[ ";
  for(auto element : myVector)
    cout << element << ' ';
  cout << "]\n";
}
 
// Function to iterate over multiset
// elements
void print(multiset<vector<string>>
           &multisetOfVectors)
{
  for (auto it = multisetOfVectors.begin();
       it != multisetOfVectors.end();
       it++)
  {
    printVector(*it);
  }
}
 
// Driver code
int main()
{
  // Declaring a multiset of vectors
  // A vector is of string type
  multiset<vector<string>>
  multisetOfVectors;
 
  // Initializing vectors
  vector<string> myVector1
  {"GeeksforGeeks", "GFG"};
  vector<string> myVector2
  {"Python", "Swift", "R"};
  vector<string> myVector3
  {"C", "C++"};
  vector<string> myVector4
  {"GeeksforGeeks", "GFG"};
  vector<string> myVector5
  {"PHP", "HTML"};
 
  // Inserting vectors into multiset
  multisetOfVectors.insert(myVector1);
  multisetOfVectors.insert(myVector2);
  multisetOfVectors.insert(myVector3);
  multisetOfVectors.insert(myVector4);
  multisetOfVectors.insert(myVector5);
 
  // Calling print function
  print(multisetOfVectors);
 
  return 0;
}

Output:

[ C C++ ]
[ GeeksforGeeks GFG ]
[ GeeksforGeeks GFG ]
[ PHP HTML ]
[ Python Swift R ]

Explanation:

In the above output, “myVector1” and “myVector4” are the same. That is why two copies of the same vector can be seen in the output.


Article Tags :
C++