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 Code
int 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;
}
Output:
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++ 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 Code
int 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++ 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 Code
int 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 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 characters
void 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 Code
int main()
{
string s = "geeks@for&geeks@";
// Function Call
separateChar(s);
}
Output:
Elements of set 1 :e o
Elements of set 2 :f g k r s
Elements of set 3 :& @
Similar Reads
Sorting a Vector in C++
Sorting a vector means arranging the elements of vector in ascending order or in descending order or in desired defined order. In this article, we will learn about different ways to sort the vector in C++.The most efficient way to sort the vector is by using sort() method. Letâs take a look at an ex
3 min read
Array of Sets in C++ STL
An array is a collection of items stored at contiguous memory locations. It is to store multiple items of the same type together. This makes it easier to get access to the elements stored in it by the position of each element. Sets are a type of associative container in which each element has to be
5 min read
How to Create a Set of Vectors in C++?
In C++, a set is a type of associative container in which duplicate elements are not allowed and a vector is a dynamic array in which duplicate elements are allowed. In this article, we will learn how to create a set of vectors in C++. For Example, Input:vector<int> vec1={1, 2, 3};vector<in
2 min read
Default value of Vector in C++ STL
Vectors are the same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators.
2 min read
Sets of pairs in C++
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. Pair is a simple cont
3 min read
How to Find the Size of a Vector in C++?
The size of a vector means the number of elements currently stored in the vector container. In this article, we will learn how to find the size of vector in C++.The easiest way to find the size of vector is by using vector size() function. Letâs take a look at a simple example:C++#include <bits/s
2 min read
How to Find the Mode of Vector in C++?
In C++, a vector is a dynamic array that resizes itself when needed. It also allows random access to its elements. In this article, we will learn how to find the mode of a vector in C++. The mode of a vector is the element that highest number of occurrences in the vector. Example: Input: myVector =
2 min read
How to Create a Vector of Pairs in C++?
In C++, std::pair is the data type that stores the data as keys and values. On the other hand, std::vector is an STL container that stores the collection of data of similar type in the contiguous memory location. In this article, we will learn how to combine these two to create a vector of pairs in
5 min read
How to Create a Vector of Tuples in C++?
In C++, a tuple is an object that allows the users to store elements of various data types together while a vector is used to store elements of the same data types. In this article, we will learn how we can create a vector of tuples in C++. Example: Input: Tuple1 ={10,A,5.3} Tuple2= {20,B,6.5}Output
2 min read
How to Create a Stack of Vectors in C++?
In C++, a stack of vectors can be created using the Standard Template Library (STL). The stack is a container adapter that provides a Last-In-First-Out (LIFO) type of data structure, and a vector is a dynamic array that can grow and shrink in size. In this article, we will learn how to create a stac
2 min read