Open In App
Related Articles

Set of Vectors in C++ STL with Examples

Improve Article
Save Article
Like Article

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.


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;
// 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
    // 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++) {
    return 0;


Set of Vectors: 
1 3 5 7 9 11 13 
5 10 15 
10 20 30 40

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 17 Mar, 2020
Like Article
Save Article
Similar Reads
Complete Tutorials