Vector of sets in C++
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; } |
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); } |
Elements of set 1 :e o Elements of set 2 :f g k r s Elements of set 3 :& @
Please Login to comment...