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++
#include <bits/stdc++.h>
using namespace std;
#define ROW 4
#define COL 5
int main()
{
vector<set< int > > v;
int num = 10;
for ( int i = 0; i < ROW; i++) {
set< int > s;
for ( int j = 0; j < COL; j++) {
s.insert(num);
num += 5;
}
v.push_back(s);
}
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++
#include <bits/stdc++.h>
using namespace std;
#define ROW 4
#define COL 5
int main()
{
vector<set< int > > v;
int num = 10;
for ( int i = 0; i < ROW; i++) {
set< int > s;
for ( int j = 0; j < COL; j++) {
s.insert(num);
num += 5;
}
v.push_back(s);
}
cout << "Before Removal:" << endl;
for ( int i = 0; i < v.size(); i++) {
for ( auto x : v[i])
cout << x << " " ;
cout << endl;
}
v.pop_back();
v.pop_back();
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++
#include <bits/stdc++.h>
using namespace std;
#define ROW 4
#define COL 5
int main()
{
vector<set< int > > v;
int num = 10;
for ( int i = 0; i < ROW; i++) {
set< int > s;
for ( int j = 0; j < COL; j++) {
s.insert(num);
num += 5;
}
v.push_back(s);
}
cout << "Before Removal:" << endl;
for ( int i = 0;
i < v.size(); i++) {
for ( auto x : v[i])
cout << x << " " ;
cout << endl;
}
v[2].erase(70);
v[1].erase(55);
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++
#include <bits/stdc++.h>
using namespace std;
void separateChar(string s)
{
vector<set< char > > v(3);
for ( int i = 0;
i < s.length(); i++) {
if (s[i] >= 'a'
&& s[i] <= 'z' ) {
if (s[i] == 'a' || s[i] == 'e'
|| s[i] == 'i' || s[i] == 'o'
|| s[i] == 'u' )
v[0].insert(s[i]);
else
v[1].insert(s[i]);
}
else
v[2].insert(s[i]);
}
for ( int i = 0; i < 3; i++) {
cout << "Elements of set "
<< i + 1 << " :" ;
for ( auto it : v[i]) {
cout << it << " " ;
}
cout << endl;
}
}
int main()
{
string s = "geeks@for&geeks@" ;
separateChar(s);
}
|
Output:
Elements of set 1 :e o
Elements of set 2 :f g k r s
Elements of set 3 :& @
Last Updated :
03 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...