Vector of sets in C++

03 Aug, 2021
Prerequisite: Vectors in C++ STL
Vectors are known as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container automatically.
Sets are a type of associative containers in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element.
Vector of sets can be used to design complex and efficient data structures, in this article we are going to check one such instance where Vector of sets could be very useful.
Syntax:
vector<set<datatype>> v ;
Insertion in Vector of Sets
Elements can be inserted into a vector using the push_back() function of C++ STL. First insert elements into a set using insert(). Then insert that set into the vector using push_back().
Below example demonstrates the insertion operation in a vector of sets:
C++
// C++ program to demonstrate the// insertion into a vector of sets#include <bits/stdc++.h>using namespace std; // Defining the number of sets// in the vector and number of// elements in each set#define ROW 4#define COL 5 // Driver Codeint main(){    // Initialize vector of sets    vector<set<int> > v;     // Elements to insert    // in column    int num = 10;     // Inserting elements    // into vector    for (int i = 0; i < ROW; i++) {         // Stores the column elements        set<int> s;         for (int j = 0; j < COL; j++) {            s.insert(num);            num += 5;        }         // Push the set in the vector        v.push_back(s);    }     // Display the vector of sets    for (int i = 0; i < v.size(); i++) {         for (auto x : v[i])            cout << x << " ";        cout << endl;    }    return 0;}
10 15 20 25 30 
35 40 45 50 55 
60 65 70 75 80 
85 90 95 100 105

Removal or Deletion in a Vector of Sets
Sets can be removed from the end of a vector of sets using the pop_back() function of C++ STL.Below example demonstrates the removal of sets from the end of a vector of sets:C++C++// C++ program to demonstrate// the removal of sets from// the end of vector of sets#include <bits/stdc++.h>using namespace std; // Defining the number of sets// in the vector and number of// elements in each set#define ROW 4#define COL 5 // Driver Codeint main(){    // Initialize the    // vector of sets    vector<set<int> > v;     // Elements to insert    // in column    int num = 10;     // Inserting elements    // into vector    for (int i = 0; i < ROW; i++) {         // Vector to store        // column elements        set<int> s;         for (int j = 0; j < COL; j++) {            s.insert(num);            num += 5;        }         // Push the set        // into the vector        v.push_back(s);    }     // Display the vector of sets    // before removal of sets    cout << "Before Removal:" << endl;    for (int i = 0; i < v.size(); i++) {         for (auto x : v[i])            cout << x << " ";        cout << endl;    }     // Remove sets from last    // index of the vector    v.pop_back();    v.pop_back();     // Display the vector of sets    // after removal of sets    cout << endl         << "After Removal:" << endl;     for (int i = 0; i < v.size(); i++) {         for (auto x : v[i])            cout << x << " ";        cout << endl;    }    return 0;}Output:Before Removal:
10 15 20 25 30 
35 40 45 50 55 
60 65 70 75 80 
85 90 95 100 105 

After Removal:
10 15 20 25 30 
35 40 45 50 55
The value of the element cannot be modified once it is added to the set, though it is possible to remove the value of that element. erase() function is used to remove a particular element from a particular set of a vector of sets.Below example demonstrates the removal of a given set element from a particular set of a vector of sets:C++C++// C++ program to demonstrate// the removal of sets from// the end of vector of sets#include <bits/stdc++.h>using namespace std; // Defining the number of sets// in the vector and number of// elements in each set#define ROW 4#define COL 5 // Driver Codeint main(){    // Initialize vector of sets    vector<set<int> > v;     // Elements to insert    // in column    int num = 10;     // Inserting elements    // into vector    for (int i = 0; i < ROW; i++) {         // Vector to store        // column elements        set<int> s;         for (int j = 0; j < COL; j++) {            s.insert(num);            num += 5;        }         // Push the set        // into the vector        v.push_back(s);    }     // Display the vector of sets    // before removal of sets    cout << "Before Removal:" << endl;     for (int i = 0;         i < v.size(); i++) {         for (auto x : v[i])            cout << x << " ";        cout << endl;    }     // Erase 70 from 3rd set    v[2].erase(70);     // Erase 55 from 2nd set    v[1].erase(55);     // Display the vector of sets    // after removal of sets    cout << endl         << "After Removal:" << endl;     for (int i = 0; i < v.size(); i++) {         for (auto x : v[i])            cout << x << " ";        cout << endl;    }    return 0;}Output:Before Removal:
10 15 20 25 30 
35 40 45 50 55 
60 65 70 75 80 
85 90 95 100 105 

After Removal:
10 15 20 25 30 
35 40 45 50 
60 65 75 80 
85 90 95 100 105

Sets can be removed from the end of a vector of sets using the pop_back() function of C++ STL.Below example demonstrates the removal of sets from the end of a vector of sets:C++C++// C++ program to demonstrate// the removal of sets from// the end of vector of sets#include <bits/stdc++.h>using namespace std; // Defining the number of sets// in the vector and number of// elements in each set#define ROW 4#define COL 5 // Driver Codeint main(){    // Initialize the    // vector of sets    vector<set<int> > v;     // Elements to insert    // in column    int num = 10;     // Inserting elements    // into vector    for (int i = 0; i < ROW; i++) {         // Vector to store        // column elements        set<int> s;         for (int j = 0; j < COL; j++) {            s.insert(num);            num += 5;        }         // Push the set        // into the vector        v.push_back(s);    }     // Display the vector of sets    // before removal of sets    cout << "Before Removal:" << endl;    for (int i = 0; i < v.size(); i++) {         for (auto x : v[i])            cout << x << " ";        cout << endl;    }     // Remove sets from last    // index of the vector    v.pop_back();    v.pop_back();     // Display the vector of sets    // after removal of sets    cout << endl         << "After Removal:" << endl;     for (int i = 0; i < v.size(); i++) {         for (auto x : v[i])            cout << x << " ";        cout << endl;    }    return 0;}Output:Before Removal:
10 15 20 25 30 
35 40 45 50 55 
60 65 70 75 80 
85 90 95 100 105 

After Removal:
10 15 20 25 30 
35 40 45 50 55

Sets can be removed from the end of a vector of sets using the pop_back() function of C++ STL.
Below example demonstrates the removal of sets from the end of a vector of sets:
C++
// C++ program to demonstrate// the removal of sets from// the end of vector of sets#include <bits/stdc++.h>using namespace std; // Defining the number of sets// in the vector and number of// elements in each set#define ROW 4#define COL 5 // Driver Codeint main(){    // Initialize the    // vector of sets    vector<set<int> > v;     // Elements to insert    // in column    int num = 10;     // Inserting elements    // into vector    for (int i = 0; i < ROW; i++) {         // Vector to store        // column elements        set<int> s;         for (int j = 0; j < COL; j++) {            s.insert(num);            num += 5;        }         // Push the set        // into the vector        v.push_back(s);    }     // Display the vector of sets    // before removal of sets    cout << "Before Removal:" << endl;    for (int i = 0; i < v.size(); i++) {         for (auto x : v[i])            cout << x << " ";        cout << endl;    }     // Remove sets from last    // index of the vector    v.pop_back();    v.pop_back();     // Display the vector of sets    // after removal of sets    cout << endl         << "After Removal:" << endl;     for (int i = 0; i < v.size(); i++) {         for (auto x : v[i])            cout << x << " ";        cout << endl;    }    return 0;}
Before Removal:
10 15 20 25 30 
35 40 45 50 55 
60 65 70 75 80 
85 90 95 100 105 

After Removal:
10 15 20 25 30 
35 40 45 50 55

The value of the element cannot be modified once it is added to the set, though it is possible to remove the value of that element. erase() function is used to remove a particular element from a particular set of a vector of sets.Below example demonstrates the removal of a given set element from a particular set of a vector of sets:C++C++// C++ program to demonstrate// the removal of sets from// the end of vector of sets#include <bits/stdc++.h>using namespace std; // Defining the number of sets// in the vector and number of// elements in each set#define ROW 4#define COL 5 // Driver Codeint main(){    // Initialize vector of sets    vector<set<int> > v;     // Elements to insert    // in column    int num = 10;     // Inserting elements    // into vector    for (int i = 0; i < ROW; i++) {         // Vector to store        // column elements        set<int> s;         for (int j = 0; j < COL; j++) {            s.insert(num);            num += 5;        }         // Push the set        // into the vector        v.push_back(s);    }     // Display the vector of sets    // before removal of sets    cout << "Before Removal:" << endl;     for (int i = 0;         i < v.size(); i++) {         for (auto x : v[i])            cout << x << " ";        cout << endl;    }     // Erase 70 from 3rd set    v[2].erase(70);     // Erase 55 from 2nd set    v[1].erase(55);     // Display the vector of sets    // after removal of sets    cout << endl         << "After Removal:" << endl;     for (int i = 0; i < v.size(); i++) {         for (auto x : v[i])            cout << x << " ";        cout << endl;    }    return 0;}Output:Before Removal:
10 15 20 25 30 
35 40 45 50 55 
60 65 70 75 80 
85 90 95 100 105 

After Removal:
10 15 20 25 30 
35 40 45 50 
60 65 75 80 
85 90 95 100 105

The value of the element cannot be modified once it is added to the set, though it is possible to remove the value of that element. erase() function is used to remove a particular element from a particular set of a vector of sets.
Below example demonstrates the removal of a given set element from a particular set of a vector of sets:
C++
// C++ program to demonstrate// the removal of sets from// the end of vector of sets#include <bits/stdc++.h>using namespace std; // Defining the number of sets// in the vector and number of// elements in each set#define ROW 4#define COL 5 // Driver Codeint main(){    // Initialize vector of sets    vector<set<int> > v;     // Elements to insert    // in column    int num = 10;     // Inserting elements    // into vector    for (int i = 0; i < ROW; i++) {         // Vector to store        // column elements        set<int> s;         for (int j = 0; j < COL; j++) {            s.insert(num);            num += 5;        }         // Push the set        // into the vector        v.push_back(s);    }     // Display the vector of sets    // before removal of sets    cout << "Before Removal:" << endl;     for (int i = 0;         i < v.size(); i++) {         for (auto x : v[i])            cout << x << " ";        cout << endl;    }     // Erase 70 from 3rd set    v[2].erase(70);     // Erase 55 from 2nd set    v[1].erase(55);     // Display the vector of sets    // after removal of sets    cout << endl         << "After Removal:" << endl;     for (int i = 0; i < v.size(); i++) {         for (auto x : v[i])            cout << x << " ";        cout << endl;    }    return 0;}
Before Removal:
10 15 20 25 30 
35 40 45 50 55 
60 65 70 75 80 
85 90 95 100 105 

After Removal:
10 15 20 25 30 
35 40 45 50 
60 65 75 80 
85 90 95 100 105

The following example demonstrates the use of vector of sets:
Given a string S, the task is to separate the given string S into three different set of characters i.e., vowel, consonants, or a special character.
Below is the implementation of the above problem:
C++
// C++ program to implement vector of sets#include <bits/stdc++.h>using namespace std; // Function to print set// of different charactersvoid separateChar(string s){    // Vector of set    vector<set<char> > v(3);     // Insert data in vector of set    for (int i = 0;         i < s.length(); i++) {         if (s[i] >= 'a'            && s[i] <= 'z') {             // Insert vowels            if (s[i] == 'a' || s[i] == 'e'                || s[i] == 'i' || s[i] == 'o'                || s[i] == 'u')                v[0].insert(s[i]);             // Insert consonants            else                v[1].insert(s[i]);        }        // Insert special characters        else            v[2].insert(s[i]);    }     // Iterate over all the sets    for (int i = 0; i < 3; i++) {         cout << "Elements of set "             << i + 1 << " :";         // Print elements of each set        for (auto it : v[i]) {             cout << it << " ";        }        cout << endl;    }} // Driver Codeint main(){    string s = "geeks@for&geeks@";     // Function Call    separateChar(s);}
Elements of set 1 :e o 
Elements of set 2 :f g k r s 
Elements of set 3 :& @

sagar0719kumar
cpp-set
cpp-vector
STL
C++ Programs
Data Structures
Data Structures
STL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Passing a function as a parameter in C++
Const keyword in C++
Program to implement Singly Linked List in C++ using class
cout in C++
Different ways to print elements of vector
DSA Sheet by Love Babbar
SDE SHEET - A Complete Guide for SDE Preparation
Top 50 Array Coding Problems for Interviews
Introduction to Data Structures
Doubly Linked List | Set 1 (Introduction and Insertion)