# Set of vectors in C++

In this post, you will understand the meaning and implementation of a set of vectors in C++.

Let us start by understanding the meaning of containers.

Containers: Containers are classes that store data and objects.

1. Sequence containers: array, vectors, list, etc.
2. Container adaptors: stack, queue, priority_queue, etc.
3. Associative containers: sets, map, multiset, multimap, etc

Set: It is a type of associative container in which each element is unique i.e. duplicates are not allowed. An element inside a set cannot be modified but an element can be deleted or inserted.

• Ordered Set: Follows an ordering pattern -> Descending(Default) or Ascending
• Unordered Set: Does not follow any order and the elements are randomly distributed.

Vectors: Vectors are similar to a dynamic array as they automatically get reshaped or resized along with insertion or deletion of elements. The elements of a vector are stored in contiguous memory units.

## Set of Vectors

Let us take a simple example of printing the set of vectors in C++.

### Printing set of vectors

To be able to use the sets, you have to include the function in your C++ program using :

`#include <set>`

Now let us define a function that will print our vectors :

```void print_vector(vector<int> v)
{
for(int i=0; i<(int)v.size(); i++)
std::cout << '\t' << v[i];
std::cout << "\n";
}```

Now let us dive into our main() function:

Declaration of the set of Vectors can be done in the following manner  :

`set< vector<data_type> > ;`

For our program, let us use:

`set <vector<int>>;`

Let us now define our vectors :

```vector<int> vec_1{ 12, 13, 25, 26 };
vector<int> vec_2{ 10, 11, 12 };
vector<int> vec_3{ 5, 22, 44, 12, 14, 11, 13 };
vector<int> vec_4{ 10, 11, 12 };
vector<int> vec_5{ 12, 13, 25, 26 };```
```set_of_vectors.insert(vec_1);
set_of_vectors.insert(vec_2);
set_of_vectors.insert(vec_3);
set_of_vectors.insert(vec_4);
set_of_vectors.insert(vec_5);```

This will insert our vectors into our set.

Now, let us print out our set of vectors:

```set< std::vector<int> >::iterator itr;
for(itr=set_of_vectors.begin(); itr!=set_of_vectors.end(); itr++)
print_vector(*itr);```

Let us take a look at the complete C++ program:

```#include <iostream>
#include <vector>
#include <set>

using namespace std;

void print_vector(vector<int> v)
{
for(int i=0; i<(int)v.size(); i++)
std::cout << '\t' << v[i];
std::cout << "\n";

}

int main()
{
set <vector<int>> set_of_vectors;
vector<int> vec_1{ 12, 13, 25, 26 };
vector<int> vec_2{ 10, 11, 12 };
vector<int> vec_3{ 5, 22, 44, 12, 14, 11, 13 };
vector<int> vec_4{ 10, 11, 12 };
vector<int> vec_5{ 12, 13, 25, 26 };
set_of_vectors.insert(vec_1);
set_of_vectors.insert(vec_2);
set_of_vectors.insert(vec_3);
set_of_vectors.insert(vec_4);
set_of_vectors.insert(vec_5);
set< std::vector<int> >::iterator itr;
for(itr=set_of_vectors.begin(); itr!=set_of_vectors.end(); itr++)
print_vector(*itr);

}```

Output:

```5    22    44    12    14    11    13

10    11    12

12    13    25    26```

As you can see only the unique vectors are printed out. Also, they are ordered in the ascending order based on the first elements of the vectors.