Related Articles

# Set of Vectors in C++ STL with Examples

• Difficulty Level : Medium
• Last Updated : 17 Mar, 2020

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 ``using` `namespace` `std;`` ` `set > 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;``}`
Output:
```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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up