Set of Vectors in C++ STL with Examples

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.