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:  

  • begin(): Returns an iterator to the first element in the multiset.
  • end(): Returns an iterator to the theoretical element that follows the last element in the multiset.
  • size(): Returns the number of elements in the multiset.
  • max_size(): Returns the maximum number of elements that the multiset can hold.
  • empty(): Returns whether the multiset is empty.

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:

  • begin(): Returns an iterator pointing to the first element in the vector.
  • end(): Returns an iterator pointing to the theoretical element that follows the last element in the vector.
  • rbegin(): Returns a reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first element.
  • rend(): Returns a reverse iterator pointing to the theoretical element preceding the first element in the vector (considered as reverse end).
  • cbegin(): Returns a constant iterator pointing to the first element in the vector.
  • cend(): Returns a constant iterator pointing to the theoretical element that follows the last element in the vector.
  • crbegin(): Returns a constant reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to the first element.
  • crend(): Returns a constant reverse iterator pointing to the theoretical element preceding the first element in the vector (considered as reverse end).

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++




// 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++




// 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
  {"w3wiki", "GFG"};
  vector<string> myVector2
  {"Python", "Swift", "R"};
  vector<string> myVector3
  {"C", "C++"};
  vector<string> myVector4
  {"w3wiki", "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++ ]
[ w3wiki GFG ]
[ w3wiki 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.



Contact Us