Set in STL 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 in STL Vector is 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.
Set of Vectors in STL: Set of Vectors can be very efficient in designing complex data structures.
Syntax:
set<vector<datatype>> set_of_vector;
For example: Consider a simple problem where we have to print all the unique vectors.
// C++ program to demonstrate // use of set for vectors #include <bits/stdc++.h> using namespace std; set<vector<int> > set_of_vectors; // Print elements of Vector void Print_Vector(vector<int> Vec) { for (int i = 0; i < Vec.size(); i++) { cout << Vec[i] << " "; } cout << endl; return; } // Driver code int main() { // Initializing some vectors vector<int> data_1{ 10, 20, 30, 40 }; vector<int> data_2{ 5, 10, 15 }; vector<int> data_3{ 1, 3, 5, 7, 9, 11, 13 }; vector<int> data_4{ 5, 10, 15 }; vector<int> data_5{ 10, 20, 30, 40 }; // Inserting vectors into set set_of_vectors.insert(data_1); set_of_vectors.insert(data_2); set_of_vectors.insert(data_3); set_of_vectors.insert(data_4); set_of_vectors.insert(data_5); // printing all the unique vectors in set cout << "Set of Vectors: \n"; for (auto it = set_of_vectors.begin(); it != set_of_vectors.end(); it++) { Print_Vector(*it); } return 0; } |
Set of Vectors: 1 3 5 7 9 11 13 5 10 15 10 20 30 40
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Vector of Vectors in C++ STL with Examples
- Priority Queue of Vectors in C++ STL with Examples
- How to join two Vectors using STL in C++?
- Array of Vectors in C++ STL
- Working with Array and Vectors using STL in C++
- Implementation of file allocation methods using vectors
- How to flatten a Vector of Vectors or 2D Vector in C++
- Stack of Pair in C++ STL with Examples
- Queue of Pairs in C++ STL with Examples
- Bloom Filter in Java with Examples
- Count-min sketch in Java with Examples
- Static and Dynamic data structures in Java with Examples
- Maximize count of non-overlapping subarrays with sum K
- Check if an array contains only one distinct element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

